<commit_before>/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Filename: api.jake
Date: 3/26/17
Author: <NAME> 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 = url + "guitars/";
		//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('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 = "";

  if(!apiConfig) throw Error("apiConfig " + apiConfig + ". Please review .env.");
  
	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
}
<commit_msg>Added ability to accept API Keys<commit_after>/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Filename: api.jake
Date: 3/26/17
Author: <NAME> 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,
  key:         process.env.API_KEY
}

namespace('api', function () {
  desc('GET All Orders. Requires API_KEY. 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 = url + "guitars/";
    //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('GET item detail based on id. Requires API_KEY. Ex: jake api:getItemDetail[dev,0000]');
  task('getItemDetail', { async: true }, function(env, id) {
    //A. Create URL
    var url  = getURLPath(env);
    var path = url + "guitars/" + id;
    
    //B. Use NPM Request to construct a GET request
    if(!apiConfig.key) throw Error("Error: API_KEY required within .env.")      
    request.get({ 
      headers: {'x-api-key' : apiConfig.key },
      url: path, 
      json: true 
    }, function(err, res){
      if(err) throw err
        console.log(res.body)
    });
  });

  desc('POST order');
  task('getOrder', { async: true }, function(env, id) {    
    var id = id
    //B. Use NPM Request to construct a POST request
    if(!apiConfig.key) throw Error("Error: API_KEY required within .env.");
    request.post({
      headers: {'x-api-key' : apiConfig.key },
      url:     getURLPath(env) + 'order/',
      body:    JSON.stringify({ orderId: id})
    }, 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 = "";

  if(!apiConfig) throw Error("apiConfig " + apiConfig + ". Please review .env.");
  
  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
}

{'fraction_non_alphanumeric': 0.1646433990895296, 'fraction_numerical': 0.004362670713201821, 'mean_word_length': 2.9886535552193645, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2243118', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1731, 'n_words': 525}