Skip to content

Commit

Permalink
Merge pull request gomfunkel#4 from davidwood/sts-form-encoded
Browse files Browse the repository at this point in the history
Updated STS execute function to submit POST as form encoded
  • Loading branch information
gomfunkel committed Aug 16, 2011
2 parents 116128b + c66ee93 commit dea9d82
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
60 changes: 47 additions & 13 deletions lib/mailchimp/MailChimpSTSAPI_v1_0.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,40 @@ function MailChimpSTSAPI_v1_0 (apiKey, options) {

module.exports = MailChimpSTSAPI_v1_0;

/***
* Recursively encode an object as application/x-www-form-urlencoded
*
* @param value Value to encode
* @param key Key to encode (not required for top-level objects)
*/
function serialize(value, key) {
var output;
key || (key = '');
if (Array.isArray(value)) {
output = [];
value.forEach(function(val, index) {
if (key != '') index = key + '[' + index + ']';
output.push(serialize(val, index));
}, this);
return output.join('&');
} else if (typeof(value) == 'object') {
output = [];
for (var name in value) {
if (value[name] && value.hasOwnProperty(name)) {
output.push(serialize(value[name], key != '' ? key + '[' + name + ']' : name));
}
}
return output.join('&');
} else {
return key + '=' + encodeURIComponent(value);
}
}

/**
* Sends a given request as a JSON object to the MailChimp STS API and finally
* calls the given callback function with the resulting JSON object. This
* method should not be called directly but will be used internally by all API
* methods defined.
* Sends a given request as a HTTP POST (application/x-www-form-urlencoded) to
* the MailChimp STS API and finally calls the given callback function with the
* resulting JSON object. This method should not be called directly but will be
* used internally by all API methods defined.
*
* @param method MailChimp API method to call
* @param availableParams Parameters available for the specified API method
Expand All @@ -41,19 +70,24 @@ module.exports = MailChimpSTSAPI_v1_0;
*/
MailChimpSTSAPI_v1_0.prototype.execute = function (method, availableParams, givenParams, callback) {

var finalParams = { apikey : this.apiKey };
var finalParams = [ 'apikey=' + encodeURIComponent(this.apiKey) ];

for (var i = 0; i < availableParams.length; i++) {
currentParam = availableParams[i];
if (typeof givenParams[currentParam] !== 'undefined')
finalParams[currentParam] = givenParams[currentParam];
if (typeof givenParams[currentParam] !== 'undefined') {
var serialized = serialize(givenParams[currentParam], currentParam);
if (serialized != '') finalParams.push(serialized);
}
}

request({
uri : this.httpUri+'/'+this.version+'/'+method+'/',
uri : this.httpUri+'/'+this.version+'/'+method,
method: 'POST',
headers : { 'User-Agent' : 'node-mailchimp/'+this.packageInfo['version'] },
body : JSON.stringify(finalParams)
headers : {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent' : 'node-mailchimp/'+this.packageInfo['version']
},
body : finalParams.join('&')
}, function (error, response, body) {
var parsedResponse;
if (error) {
Expand Down Expand Up @@ -209,4 +243,4 @@ MailChimpSTSAPI_v1_0.prototype.GetSendStatistics = function (params, callback) {
if (typeof params == 'function') callback = params, params = {};
this.execute('GetSendStatistics', [
], params, callback);
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "mailchimp",
"version" : "0.7.1",
"version" : "0.7.2",
"description" : "A node.js wrapper for the MailChimp API.",
"author" : "Daniel Leinich <[email protected]>",
"main" : "./lib/mailchimp",
Expand All @@ -11,4 +11,4 @@
"dependencies": {
"request": ">= 0.10.0"
}
}
}

0 comments on commit dea9d82

Please sign in to comment.