Skip to content

Commit

Permalink
rename fromFile arg to useFile
Browse files Browse the repository at this point in the history
export method getToFile to store payload of request directly in local file
  • Loading branch information
xmilliard committed Sep 7, 2011
1 parent 59b57fa commit efd44ca
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions lib/oauth.js
Expand Up @@ -292,7 +292,7 @@ exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_s
}


exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback, fromFile ) {
exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback, useFile ) {
var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, extra_params);

if( !post_content_type ) {
Expand Down Expand Up @@ -330,7 +330,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
post_body= querystring.stringify(extra_params);
}

if(!fromFile) {
if(!useFile) {
headers["Content-length"]= post_body ? Buffer.byteLength(post_body) : 0;
}
headers["Content-Type"]= post_content_type;
Expand Down Expand Up @@ -364,7 +364,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
} else {
// Follow 301 or 302 redirects with Location HTTP header
if((response.statusCode == 301 || response.statusCode == 302) && response.headers && response.headers.location) {
self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback, fromFile);
self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback, useFile);
}
else {
callback({ statusCode: response.statusCode, data: data }, data, response);
Expand All @@ -373,12 +373,27 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
}
}

request.on('response', function (response) {

var targetFile;
if(method == "GET" && useFile) {
targetFile = fs.createWriteStream(post_body);
}
else {
response.setEncoding('utf8');
}
request.on('response', function (response) {
response.on('data', function (chunk) {
data+=chunk;
if(targetFile) {
targetFile.write(chunk);
}
else {
data+=chunk;
}
});
response.on('end', function () {
if(targetFile) {
targetFile.destroySoon();
}
passBackControl( response );
});
response.on('close', function () {
Expand All @@ -393,7 +408,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
callback( err )
});
if( (method == "POST" || method =="PUT") && post_body != null && post_body != "" ) {
if(fromFile) {
if(useFile) {
var rs = fs.createReadStream(post_body);
rs.pipe(request);
}else {
Expand Down Expand Up @@ -465,6 +480,12 @@ exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, call
return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback );
}

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



exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback, fromFile) {
var extra_params= null;
if( typeof post_content_type == "function" ) {
Expand Down

0 comments on commit efd44ca

Please sign in to comment.