Permalink
Browse files

Change coding standards

  • Loading branch information...
1 parent 6395e2d commit 6ff4f3297764c20ca3d3e9819921b4391777338c @gmarty gmarty committed Oct 19, 2012
@@ -2,16 +2,16 @@ var sys = require('sys'),
OperationHelper = require('apac').OperationHelper;
var opHelper = new OperationHelper({
- awsId: '[YOUR AWS ID HERE]',
- awsSecret: '[YOUR AWS SECRET HERE]',
- assocId: '[YOUR ASSOCIATE TAG HERE]',
+ awsId: '[YOUR AWS ID HERE]',
+ awsSecret: '[YOUR AWS SECRET HERE]',
+ assocId: '[YOUR ASSOCIATE TAG HERE]'
});
opHelper.execute('ItemSearch', {
- 'SearchIndex': 'Books',
- 'Keywords': 'harry potter',
- 'ResponseGroup': 'ItemAttributes,Offers'
+ 'SearchIndex': 'Books',
+ 'Keywords': 'harry potter',
+ 'ResponseGroup': 'ItemAttributes,Offers'
}, function(error, results) {
- if (error) { sys.print('Error: ' + error + "\n") }
- sys.print("Results:\n" + sys.inspect(results) + "\n");
+ if (error) { sys.print('Error: ' + error + '\n') }
+ sys.print('Results:\n' + sys.inspect(results) + '\n');
});
View
@@ -3,7 +3,7 @@ var RSH = require('./RequestSignatureHelper').RequestSignatureHelper,
xml2js = require('xml2js');
var OperationHelper = function(params) {
- this.init(params);
+ this.init(params);
};
OperationHelper.version = '2011-08-01';
@@ -12,76 +12,76 @@ OperationHelper.defaultEndPoint = 'ecs.amazonaws.com';
OperationHelper.defaultBaseUri = '/onca/xml';
OperationHelper.prototype.init = function(params) {
- params = params || {};
+ params = params || {};
- // check requried params
- if (typeof(params.awsId) === 'undefined') { throw 'Missing AWS Id param' }
- if (typeof(params.awsSecret) === 'undefined') { throw 'Missing AWS Secret param' }
- if (typeof(params.assocId) === 'undefined') { throw 'Missing Associate Id param' }
+ // check requried params
+ if (typeof(params.awsId) === 'undefined') { throw 'Missing AWS Id param' }
+ if (typeof(params.awsSecret) === 'undefined') { throw 'Missing AWS Secret param' }
+ if (typeof(params.assocId) === 'undefined') { throw 'Missing Associate Id param' }
- // set instance variables from params
- this.awsId = params.awsId;
- this.awsSecret = params.awsSecret;
- this.assocId = params.assocId;
- this.endPoint = params.endPoint || OperationHelper.defaultEndPoint;
- this.baseUri = params.baseUri || OperationHelper.defaultBaseUri;
- this.parser = new xml2js.Parser();
+ // set instance variables from params
+ this.awsId = params.awsId;
+ this.awsSecret = params.awsSecret;
+ this.assocId = params.assocId;
+ this.endPoint = params.endPoint || OperationHelper.defaultEndPoint;
+ this.baseUri = params.baseUri || OperationHelper.defaultBaseUri;
+ this.parser = new xml2js.Parser();
};
OperationHelper.prototype.getSignatureHelper = function() {
- if (typeof(this.signatureHelper) === 'undefined') {
- var params = {};
- params[RSH.kAWSAccessKeyId] = this.awsId;
- params[RSH.kAWSSecretKey] = this.awsSecret;
- params[RSH.kEndPoint] = this.endPoint;
- this.signatureHelper = new RSH(params);
- }
- return this.signatureHelper;
+ if (typeof(this.signatureHelper) === 'undefined') {
+ var params = {};
+ params[RSH.kAWSAccessKeyId] = this.awsId;
+ params[RSH.kAWSSecretKey] = this.awsSecret;
+ params[RSH.kEndPoint] = this.endPoint;
+ this.signatureHelper = new RSH(params);
+ }
+ return this.signatureHelper;
};
OperationHelper.prototype.generateParams = function(operation, params) {
- params.Service = OperationHelper.service;
- params.Version = OperationHelper.version;
- params.Operation = operation;
- params.AWSAccessKeyId = this.awsId;
- params.AssociateTag = this.assocId;
- return params;
+ params.Service = OperationHelper.service;
+ params.Version = OperationHelper.version;
+ params.Operation = operation;
+ params.AWSAccessKeyId = this.awsId;
+ params.AssociateTag = this.assocId;
+ return params;
};
OperationHelper.prototype.generateUri = function(operation, params) {
- params = this.generateParams(operation, params);
- var helper = this.getSignatureHelper();
- params = helper.sign(params);
- var parser = this.parser;
- var queryString = helper.canonicalize(params);
- var uri = this.baseUri + '?' + queryString;
- return uri;
-}
+ params = this.generateParams(operation, params);
+ var helper = this.getSignatureHelper();
+ params = helper.sign(params);
+ var parser = this.parser;
+ var queryString = helper.canonicalize(params);
+ var uri = this.baseUri + '?' + queryString;
+ return uri;
+};
OperationHelper.prototype.execute = function(operation, params, callback) {
- if (typeof(operation) === 'undefined') { throw 'Missing operation parameter' }
- if (typeof(params) === 'undefined') { params = {} }
- var uri = this.generateUri(operation, params);
- var host = this.endPoint;
- var parser = this.parser;
- var request = http.request({host:host,path:uri},function(response) {
- var responseBody = '';
- response.addListener('data', function(chunk) {
- responseBody += chunk;
- });
- response.addListener('end', function() {
- var statusCode = response.statusCode == 200 ? null : response.statusCode;
- parser.addListener('end', function(result) {
- callback(statusCode, result);
+ if (typeof(operation) === 'undefined') { throw 'Missing operation parameter' }
+ if (typeof(params) === 'undefined') { params = {} }
+ var uri = this.generateUri(operation, params);
+ var host = this.endPoint;
+ var parser = this.parser;
+ var request = http.request({host: host, path: uri},function(response) {
+ var responseBody = '';
+ response.addListener('data', function(chunk) {
+ responseBody += chunk;
+ });
+ response.addListener('end', function() {
+ var statusCode = response.statusCode == 200 ? null : response.statusCode;
+ parser.addListener('end', function(result) {
+ callback(statusCode, result);
- // so that next time this is called the result isn't concatenated
- parser.removeAllListeners('end');
- });
- parser.parseString(responseBody);
- });
- response.setEncoding('utf8');
+ // so that next time this is called the result isn't concatenated
+ parser.removeAllListeners('end');
+ });
+ parser.parseString(responseBody);
});
- request.end();
+ response.setEncoding('utf8');
+ });
+ request.end();
};
exports.OperationHelper = OperationHelper;
@@ -1,89 +1,90 @@
var crypto = require('crypto');
var RSH = function(params) {
- this.init(params);
+ this.init(params);
};
RSH.kAWSAccessKeyId = 'AWSAccessKeyId';
-RSH.kAWSSecretKey = 'AWSSecretKey';
-RSH.kEndPoint = 'EndPoint';
-RSH.kRequestMethod = 'RequestMethod';
-RSH.kRequestUri = 'RequestUri';
+RSH.kAWSSecretKey = 'AWSSecretKey';
+RSH.kEndPoint = 'EndPoint';
+RSH.kRequestMethod = 'RequestMethod';
+RSH.kRequestUri = 'RequestUri';
RSH.kTimestampParam = 'Timestamp';
RSH.kSignatureParam = 'Signature';
RSH.prototype.init = function(params) {
- // enforce required params
- if (typeof(params[RSH.kAWSAccessKeyId]) === 'undefined') { throw 'Need access key id argument' }
- if (typeof(params[RSH.kAWSSecretKey]) === 'undefined') { throw 'Need secret key argument' }
- if (typeof(params[RSH.kEndPoint]) === 'undefined') { throw 'Need end point argument' }
+ // enforce required params
+ if (typeof(params[RSH.kAWSAccessKeyId]) === 'undefined') { throw 'Need access key id argument' }
+ if (typeof(params[RSH.kAWSSecretKey]) === 'undefined') { throw 'Need secret key argument' }
+ if (typeof(params[RSH.kEndPoint]) === 'undefined') { throw 'Need end point argument' }
- // set params
- this[RSH.kAWSAccessKeyId] = params[RSH.kAWSAccessKeyId];
- this[RSH.kAWSSecretKey] = params[RSH.kAWSSecretKey];
- this[RSH.kEndPoint] = params[RSH.kEndPoint].toLowerCase();
- this[RSH.kRequestMethod] = params[RSH.kRequestMethod] || 'GET';
- this[RSH.kRequestUri] = params[RSH.kRequestUri] || '/onca/xml';
+ // set params
+ this[RSH.kAWSAccessKeyId] = params[RSH.kAWSAccessKeyId];
+ this[RSH.kAWSSecretKey] = params[RSH.kAWSSecretKey];
+ this[RSH.kEndPoint] = params[RSH.kEndPoint].toLowerCase();
+ this[RSH.kRequestMethod] = params[RSH.kRequestMethod] || 'GET';
+ this[RSH.kRequestUri] = params[RSH.kRequestUri] || '/onca/xml';
};
RSH.prototype.sign = function(params) {
- var self = this;
- // append params
- params[RSH.kTimestampParam] = this.generateTimestamp();
- params[RSH.kAWSAccessKeyId] = this[RSH.kAWSAccessKeyId] ;
- // generate signature
- var canonical = this.canonicalize(params);
- var stringToSign = [
- this[RSH.kRequestMethod],
- this[RSH.kEndPoint],
- this[RSH.kRequestUri],
- canonical
- ].join("\n");
- params[RSH.kSignatureParam] = this.digest(stringToSign);
+ var self = this;
+ // append params
+ params[RSH.kTimestampParam] = this.generateTimestamp();
+ params[RSH.kAWSAccessKeyId] = this[RSH.kAWSAccessKeyId];
+ // generate signature
+ var canonical = this.canonicalize(params);
+ var stringToSign = [
+ this[RSH.kRequestMethod],
+ this[RSH.kEndPoint],
+ this[RSH.kRequestUri],
+ canonical
+ ].join('\n');
+ params[RSH.kSignatureParam] = this.digest(stringToSign);
- return params;
+ return params;
};
RSH.prototype.zeroPad = function(num, length) {
- num = num + '';
- while (num.length < length) {
- num = '0' + num;
- }
- return num;
+ num = num + '';
+ while (num.length < length) {
+ num = '0' + num;
+ }
+ return num;
};
RSH.prototype.generateTimestamp = function() {
- var now = new Date(),
- year = now.getUTCFullYear(),
- month = this.zeroPad(now.getUTCMonth() + 1, 2),
- day = this.zeroPad(now.getUTCDate(), 2),
- hours = this.zeroPad(now.getUTCHours(), 2),
- mins = this.zeroPad(now.getUTCMinutes(), 2),
- secs = this.zeroPad(now.getUTCSeconds(), 2);
- return [year, month, day].join('-') + 'T' +
- [hours, mins, secs].join(':') + 'Z';
+ var now = new Date(),
+ year = now.getUTCFullYear(),
+ month = this.zeroPad(now.getUTCMonth() + 1, 2),
+ day = this.zeroPad(now.getUTCDate(), 2),
+ hours = this.zeroPad(now.getUTCHours(), 2),
+ mins = this.zeroPad(now.getUTCMinutes(), 2),
+ secs = this.zeroPad(now.getUTCSeconds(), 2);
+ return [year, month, day].join('-') + 'T' +
+ [hours, mins, secs].join(':') + 'Z';
};
+
/**
* Port of PHP rawurlencode().
*/
RSH.prototype.escape = function(x) {
- return encodeURIComponent(x).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
+ return encodeURIComponent(x).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
};
RSH.prototype.digest = function(x) {
- var secretKey = this[RSH.kAWSSecretKey];
- var hmac = crypto.createHmac('sha256', secretKey);
- hmac.update(x);
- return hmac.digest('base64');
+ var secretKey = this[RSH.kAWSSecretKey];
+ var hmac = crypto.createHmac('sha256', secretKey);
+ hmac.update(x);
+ return hmac.digest('base64');
};
RSH.prototype.canonicalize = function(params) {
- var parts = [];
- for (var key in params) {
- parts.push([this.escape(key), this.escape(params[key])].join('='));
- }
- return parts.sort().join('&');
+ var parts = [];
+ for (var key in params) {
+ parts.push([this.escape(key), this.escape(params[key])].join('='));
+ }
+ return parts.sort().join('&');
};
exports.RequestSignatureHelper = RSH;
Oops, something went wrong.

0 comments on commit 6ff4f32

Please sign in to comment.