Skip to content

Commit

Permalink
Possible mechanism for returning a signed request object.
Browse files Browse the repository at this point in the history
if oauth.get/post is called without a callback parameter it will return
a valid request object that can have custom listeners etc. attached to it.

Don't forget to call 'end' on it :)
  • Loading branch information
ciaranj committed Aug 26, 2010
1 parent b79bce5 commit 3b94a77
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,34 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
else path= parsedUrl.pathname;

var request = oauthProvider.request(method, path , headers);
var data="";
var self= this;
request.addListener('response', function (response) {
response.setEncoding('utf8');
response.addListener('data', function (chunk) {
data+=chunk;
if( callback ) {
var data="";
var self= this;
request.addListener('response', function (response) {
response.setEncoding('utf8');
response.addListener('data', function (chunk) {
data+=chunk;
});
response.addListener('end', function () {
if( response.statusCode != 200 ) {
callback({ statusCode: response.statusCode, data: data });
} else {
callback(null, data, response);
}
});
});
response.addListener('end', function () {
if( response.statusCode != 200 ) {
callback({ statusCode: response.statusCode, data: data });
} else {
callback(null, data, response);
}
});
});

request.socket.addListener("error",callback);
if( method == "POST" && post_body != null && post_body != "" ) {
request.write(post_body);
request.socket.addListener("error",callback);
if( method == "POST" && post_body != null && post_body != "" ) {
request.write(post_body);
}
request.end();
}
request.end();
else {
return request;
}

return;
}

exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback) {
Expand Down Expand Up @@ -274,7 +281,7 @@ exports.OAuth.prototype.getProtectedResource= function(url, method, oauth_token,
}

exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, callback) {
this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback );
return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback );
}

exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
Expand All @@ -288,7 +295,7 @@ exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, pos
extra_params= post_body;
post_body= querystring.stringify(post_body);
}
this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, extra_params, post_body, post_content_type, callback );
return this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, extra_params, post_body, post_content_type, callback );
}

exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) {
Expand Down

0 comments on commit 3b94a77

Please sign in to comment.