Skip to content

Instantly share code, notes, and snippets.

@JasonSanford
Created April 17, 2014 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JasonSanford/11005163 to your computer and use it in GitHub Desktop.
Save JasonSanford/11005163 to your computer and use it in GitHub Desktop.
var https = require("https");
module.exports = function(method, url, token, message, callback) {
var json = JSON.stringify(message);
var request = https.request({
hostname: "api.github.com",
port: 443,
path: url,
method: method,
headers: {
"Accept": "application/vnd.github.v3+json",
"Authorization": "token " + token,
"User-Agent": "mbostock/gistup",
"Content-Type": "application/json;charset=utf8",
"Content-Length": Buffer.byteLength(json, "utf8")
}
}, function(response) {
var chunks = [];
response.setEncoding("utf8");
response.on("data", function(chunk) { chunks.push(chunk); });
response.on("end", function() {
var response, id = null, error;
try { response = JSON.parse(chunks.join("")); }
catch (e) { error = e; }
if (!error && !response) error = new Error("empty API response");
if (error) console.warn(os.EOL + chunks.join(""));
callback(error, response);
});
});
request.on("error", callback);
request.write(json);
request.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment