/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Filename: api.jake
Date: 3/26/17
Author: Chris Mendez http://chrisjmendez.com
Description: API requests made to an AWS API Gateway.
It is assumed that within your AWS API Gateway, you 
might have a single function written for three stages: 
dev, staging, and prod.

Example: 

//Submit a CURL request to the Productin API
jake api:getAll[prod]

* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
var util    = require('util')
var request = require('request')
var apiConfig = {
	development: process.env.API_DEV,
	staging:     process.env.API_STAGING,
	production:  process.env.API_PROD
}

namespace('api', function () {
	desc('GET All Orders. Ex: jake api:getAll[prod]');
	task('getAll', { async: true }, function(env) {
		//A. Get the URL path based on your dev environment: dev, stating, prod
		var url  = getURLPath(env);
		var path = "guitars/"
		//B. We are using curl to show how to do this manually
		var cmds = [ util.format('curl %s%s', url, path) ];
		console.log(cmds)
		jake.exec(cmds, { printStdout: true, printStderr: true }, function(){
			complete();
		})
	});
	
	desc('GET item detail based on orderId.');
	task('getItemDetail', { async: true }, function(env) {
		//A. Create URL
		var url  = getURLPath(env);
		var orderId = "123456";
		var path = url + "guitars/" + orderId;
		//B. Use NPM Request to construct a GET request
		request.get({ url: path, json: true }, function(err, res){
			if(err) throw err
			console.log(res.body)
		});
	});
	
	desc('POST order');
	task('getOrder', { async: true }, function(env) {		
		//A. Create URL
		var orderId = "abcd123";
		//B. Use NPM Request to construct a POST request
		request.post({
			//headers: {'content-type' : 'application/x-www-form-urlencoded'},
			url:     getURLPath(env) + 'order/',
			body:    JSON.stringify({ orderId: orderId})
		}, function(err, res, data){
			if(err) throw err;
			if( data ){
				console.log(JSON.parse(data))
			}
		});
	});
});

//Return the URL based on the staging environment
function getURLPath(env){
	var url = ""
	switch(env){
	case "stag":
	case "staging":
		url = apiConfig.staging
		break;
	case "prod":
	case "production":
		url = apiConfig.production
		break;
	case "dev":
	case "development":
	default:
		url = apiConfig.development
	}
	console.log("Environment:", env, " URL:", url );
	return url
}

@@ -893,16 +893,22 @@
 r path =
+ url +
  %22guitar
@@ -910,24 +910,25 @@
 uitars/%22
+;
 %0A%09%09//B. 
 We are u
@@ -923,223 +923,168 @@
 /B. 
-We are using curl to show how to do this manually%0A%09%09var cmds = %5B util.format('curl %25s%25s', url, path) %5D;%0A%09%09console.log(cmds)%0A%09%09jake.exec(cmds, %7B printStdout: true, printStderr: true %7D, function()%7B%0A%09%09%09complete();%0A%09%09%7D)
+Use NPM Request to construct a GET request%0A%09%09request.get(%7B url: path, json: true %7D, function(err, res)%7B%0A%09%09%09if(err) throw err%0A%09%09%09console.log(res.body)%0A%09%09%7D);%0A    
 %0A%09%7D)
@@ -1466,26 +1466,25 @@
 %0A%09%09%7D);%0A%09%7D);%0A
-%09
 %0A
+
 %09desc('POST 
@@ -2028,16 +2028,103 @@
 url = %22%22
+;%0A%0A  if(!apiConfig) throw Error(%22apiConfig %22 + apiConfig + %22. Please review .env.%22);%0A  
 %0A%09switch
@@ -2407,10 +2407,11 @@
 urn url%0A
-
 %7D%0A
+%0A

Replace curl with request