Created
February 15, 2016 09:25
-
-
Save pascalvree/a039d28161439b297f11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var httpsClient = require('https'); | |
var isEmpty = function(obj) { return !obj || obj == undefined || obj == null; } | |
var notEmpty = function(obj) { return !isEmpty(obj); } | |
var assert = function(expression, message) { if(!expression) { throw message; } } | |
var performRequest = function (runtimeConfig) { | |
try { | |
assert(notEmpty(runtimeConfig.callback), "runtimeConfig.callback must be provided"); | |
assert(notEmpty(runtimeConfig.options), "runtimeConfig.options must be provided"); | |
} catch(err) { | |
throw err; | |
return false; | |
} | |
var request = httpsClient.request(runtimeConfig.options).on('response', function(response) { | |
var data = ''; | |
response.setEncoding(runtimeConfig.responseEncoding); | |
response.on('error', function(err) { throw err; }); | |
response.on('data', function(chunk) { data += chunk; }); | |
response.on('end', function() { runtimeConfig.callback(data); }); | |
}); | |
if(notEmpty(runtimeConfig.body)) { request.write(runtimeConfig.body); } | |
request.end(); | |
} | |
module.exports = { | |
performRequest: performRequest | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('es6-promise').Promise; | |
var builtUri = function(configuration) { | |
var method = configuration.method; | |
Object.keys(configuration.parameters).forEach(function(key) { method = method.replace('{' + key + '}', configuration.parameters[key]); }); | |
return configuration.endPoint + method; | |
} | |
var builtHttpHeaders = function(defaultHeaders, customHeaders) { | |
var httpHeaders = { }; | |
Object.keys(defaultHeaders).forEach(function(key) { httpHeaders[key] = defaultHeaders[key]; }); | |
Object.keys(customHeaders).forEach(function(key) { httpHeaders[key] = customHeaders[key]; }); | |
return httpHeaders; | |
} | |
var builtRequest = function(request, transport, adapter) { | |
var callback = function(fulfill, reject) { | |
var settings = { | |
options: { | |
encoding: request.encoding, | |
host: request.host, | |
path: request.path, | |
proto: request.protocol, | |
method: request.httpMethod, | |
headers: request.httpHeaders, | |
body: request.body | |
}, | |
callback: function(response) { | |
try { | |
return fulfill(adapter.Adapt(response)); | |
} catch(err) { | |
return reject({ err: err, response: response }); | |
} | |
} | |
}; | |
transport.performRequest(settings); | |
} | |
return new Promise(callback); | |
} | |
var Load = function(request, transport, adapter) { | |
return builtRequest( | |
{ | |
path: request.path, | |
host: request.host, | |
protocol: request.protocol, | |
transport: request.transport, | |
httpMethod: request.httpMethod, | |
httpHeaders: request.httpHeaders, | |
body: request.body | |
}, | |
transport, | |
adapter | |
); | |
} | |
module.exports = { | |
Load: Load, | |
BuiltUri: builtUri, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment