Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

rename fromFile arg to useFile

export method getToFile to store payload of request directly in local file
  • Loading branch information...
commit efd44ca65ed5404950c1f85cba32bf6525ce3f4d 1 parent 59b57fa
@xmilliard authored
Showing with 27 additions and 6 deletions.
  1. +27 −6 lib/oauth.js
View
33 lib/oauth.js
@@ -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 ) {
@@ -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;
@@ -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);
@@ -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 () {
@@ -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 {
@@ -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" ) {
Please sign in to comment.
Something went wrong with that request. Please try again.