Skip to content

Commit

Permalink
remove request depencency
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Feb 25, 2013
1 parent 3852c16 commit dd966d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
62 changes: 50 additions & 12 deletions index.js
@@ -1,7 +1,9 @@
var requestlib = require("request"),
Stream = require("stream").Stream,
var Stream = require("stream").Stream,
utillib = require("util"),
querystring = require("querystring");
querystring = require("querystring"),
http = require("http"),
https = require("https"),
urllib = require("url");

/**
* Wrapper for new XOAuth2Generator.
Expand Down Expand Up @@ -95,15 +97,7 @@ XOAuth2Generator.prototype.generateToken = function(callback){
},
payload = querystring.stringify(urlOptions);

requestlib({
method: "POST",
url: this.options.accessUrl,
body: payload,
headers: {
"Content-Type" : "application/x-www-form-urlencoded",
"Content-Length" : Buffer.byteLength(payload)
}
}, (function(error, response, body){
postRequest(this.options.accessUrl, payload, (function(error, response, body){
var data;

if(error){
Expand All @@ -128,6 +122,7 @@ XOAuth2Generator.prototype.generateToken = function(callback){
return callback(null, this.token, this.accessToken);
}

return callback(new Error("No access token"));
}).bind(this));
};

Expand All @@ -145,3 +140,46 @@ XOAuth2Generator.prototype.buildXOAuth2Token = function(accessToken){
""];
return new Buffer(authData.join("\x01"), "utf-8").toString("base64");
};


function postRequest(url, payload, callback){
var options = urllib.parse(url),
finished = false;

options.method = "POST";

var req = (options.protocol=="https:"?https:http).request(options, function(res) {
var data = [];

res.on('data', function (chunk) {
data.push(chunk);
});

res.on("end", function(){
if(finished){return;}
finished = true
return callback(null, res, Buffer.concat(data));
});

res.on("error", function(err) {
if(finished){return;}
finished = true
callback(err);
});
});

req.on("error", function(err) {
if(finished){return;}
finished = true
callback(err);
});

if(payload){
req.setHeader("Content-Type", "application/x-www-form-urlencoded");
req.setHeader("Content-Length", typeof payload == "string" ? Buffer.byteLength(payload) : payload.length);
}

req.write(payload);
req.end();

}
5 changes: 1 addition & 4 deletions package.json
Expand Up @@ -18,8 +18,5 @@
"IMAP"
],
"author": "Andris Reinman",
"license": "MIT",
"dependencies":{
"request": "*"
}
"license": "MIT"
}

0 comments on commit dd966d0

Please sign in to comment.