Skip to content
Browse files

no need to be so verbose with those checks there

  • Loading branch information...
1 parent d890bff commit 773af189c3fc189a2fbac1d5612f2bc37008062a @pib pib committed Jan 8, 2011
Showing with 18 additions and 18 deletions.
  1. +18 −18 lib/aws.js
View
36 lib/aws.js
@@ -8,9 +8,9 @@ var xml2js = require("./xml2js")
// Creates an EC2 API client
exports.createEC2Client = function (accessKeyId, secretAccessKey, options) {
- if (options === undefined) options = {};
-
- var host = options.host ? options.host : "ec2.amazonaws.com";
+ options = options || {};
+
+ var host = options.host || "ec2.amazonaws.com";
var path = "/";
var client = ec2Client({
host: host,
@@ -25,8 +25,8 @@ exports.createEC2Client = function (accessKeyId, secretAccessKey, options) {
//creates an Amazon Product Advertising API Client
exports.createProdAdvClient = function (accessKeyId, secretAccessKey, associateTag, options) {
- if (options === undefined) options = {};
-
+ options = options || {};
+
var host = "ecs.amazonaws.de";
var path = "/onca/xml";
var client = prodAdvClient({
@@ -49,7 +49,7 @@ var ec2Client = function(obj) {
});
obj.call = function(action, query, callback) {
query["Action"] = action
- query["Version"] = obj.version === undefined ? '2009-11-30' : obj.version;
+ query["Version"] = obj.version || '2009-11-30'
query["SignatureMethod"] = "HmacSHA256"
query["SignatureVersion"] = "2"
return aws.call(action, query, callback);
@@ -67,7 +67,7 @@ var prodAdvClient = function(obj) {
obj.call = function(action, query, callback) {
query["Operation"] = action
query["Service"] = "AWSECommerceService"
- query["Version"] = obj.version === undefined ? '2009-10-01' : obj.version;
+ query["Version"] = obj.version || '2009-10-01'
query["AssociateTag"] = obj.associateTag;
query["Region"] = "DE"
return aws.call(action, query, callback);
@@ -78,29 +78,29 @@ var prodAdvClient = function(obj) {
// a generic AWS API Client which handles the general parts
var genericAWSClient = function(obj) {
var creds = crypto.createCredentials({});
- if (obj.secure === undefined) obj.secure = true;
+ if (!obj.secure) obj.secure = true;
obj.connection = http.createClient(obj.secure ? 443 : 80, obj.host, obj.secure, creds);
obj.call = function (action, query, callback) {
if (obj.secretAccessKey == null || obj.accessKeyId == null) {
throw("secretAccessKey and accessKeyId must be set")
}
-
+
var now = new Date()
var ts = now.toISOString()
-
+
// add the standard parameters required by all AWS APIs
query["Timestamp"] = ts;
query["AWSAccessKeyId"] = obj.accessKeyId;
- query["Signature"] = obj.sign(query)
+ query["Signature"] = obj.sign(query)
var body = qs.stringify(query)
var req = obj.connection.request("POST", obj.path, {
"Host": obj.host,
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": body.length
})
-
+
//the listener that retrieves the response
req.addListener('response', function (res) {
var data = ''
@@ -116,31 +116,31 @@ var genericAWSClient = function(obj) {
parser.parseString(data);
})
})
-
+
req.write(body)
req.end()
}
-
+
/*
Calculate HMAC signature of the query
*/
obj.sign = function (query) {
var hash = crypto.createHmac("sha256", obj.secretAccessKey)
var keys = []
var sorted = {}
-
+
for(var key in query)
keys.push(key)
-
+
keys = keys.sort()
-
+
for(n in keys) {
var key = keys[n]
sorted[key] = query[key]
}
var stringToSign = ["POST", obj.host, obj.path, qs.stringify(sorted)].join("\n");
return hash.update(stringToSign).digest("base64")
}
-
+
return obj;
}

0 comments on commit 773af18

Please sign in to comment.
Something went wrong with that request. Please try again.