Skip to content

Instantly share code, notes, and snippets.

@ryanlelek
Last active August 29, 2015 13:57
Show Gist options
  • Save ryanlelek/9403080 to your computer and use it in GitHub Desktop.
Save ryanlelek/9403080 to your computer and use it in GitHub Desktop.
'use strict';
// Modules
var request = require('request');
// Variables
var settings = {
host : 'http://localhost:8443',
version : 'v0',
verifySSL : false,
http : {
username : process.env.API_USER,
password : process.env.API_KEY
}
};
// Get List
module.exports.go = function (method, endpoint, parameters, callback, include_version) {
var host = settings.host;
// Include API version by default
if (include_version !== false) {
include_version = true;
}
// Add version to host, if requested
if (include_version === true) {
host += '/' + settings.version;
}
// Execute
request({
url : host + endpoint,
method : method,
headers : {
'Content-Type' : 'application/json'
},
qs : parameters.qs,
json : parameters.json,
// #####
strictSSL : settings.verifySSL,
followRedirect : false,
auth : {
username: settings.http.username,
password: settings.http.password,
sendImmediately: true
}
}, function (error, response, body) {
// Parse JSON body, if not done automatically
if ((typeof body === 'string') && (body.substring(0, 1) === '{')) {
body = JSON.parse(body);
}
// Return
return callback(error, response, body);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment