|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var utils = require('./utils'); |
| 4 | +var http = require('http'); |
| 5 | +var https = require('https'); |
| 6 | +var querystring = require('querystring'); |
| 7 | +var configuration = require('./configure'); |
| 8 | + |
| 9 | +var configure = exports.configure = function configure(options) { |
| 10 | + if (options !== undefined && typeof options === 'object') { |
| 11 | + configuration.default_options = utils.merge(configuration.default_options, options); |
| 12 | + } |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * Wraps the http client, handles request parameters, populates request headers, handles response |
| 17 | + * |
| 18 | + * @type {exports.executeHttp} |
| 19 | + */ |
| 20 | +var executeHttp = exports.executeHttp = function executeHttp(http_method, path, data, http_options, cb) { |
| 21 | + if (typeof http_options === "function") { |
| 22 | + cb = http_options; |
| 23 | + http_options = null; |
| 24 | + } |
| 25 | + if (!http_options) { |
| 26 | + http_options = configuration.default_options; |
| 27 | + } else { |
| 28 | + http_options = utils.merge(http_options, configuration.default_options, true); |
| 29 | + } |
| 30 | + |
| 31 | + //Get host endpoint using mode |
| 32 | + http_options.host = utils.getDefaultApiEndpoint(http_options.mode) || http_options.host; |
| 33 | + |
| 34 | + var client = (http_options.schema === 'http') ? http : https; |
| 35 | + |
| 36 | + var request_data = data; |
| 37 | + |
| 38 | + if (http_method === 'GET' || http_method === 'DELETE') { |
| 39 | + //format object parameters into GET request query string |
| 40 | + if (typeof request_data !== 'string') { |
| 41 | + request_data = querystring.stringify(request_data); |
| 42 | + } |
| 43 | + if (request_data) { |
| 44 | + path = path + "?" + request_data + "&apiKey=" + http_options.api_key; |
| 45 | + request_data = ""; |
| 46 | + } else { |
| 47 | + // add api key to request |
| 48 | + path += "?apiKey=" + http_options.api_key; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + } else if (typeof request_data !== 'string') { |
| 53 | + // add api key to request |
| 54 | + request_data.apiKey = http_options.api_key; |
| 55 | + request_data = JSON.stringify(request_data); |
| 56 | + } |
| 57 | + |
| 58 | + if (http_options) { |
| 59 | + |
| 60 | + http_options = JSON.parse(JSON.stringify(http_options)); |
| 61 | + |
| 62 | + if (!http_options.headers) { |
| 63 | + http_options.headers = {}; |
| 64 | + } |
| 65 | + http_options.path = path; |
| 66 | + http_options.method = http_method; |
| 67 | + if (request_data) { |
| 68 | + http_options.headers['Content-Length'] = Buffer.byteLength(request_data, 'utf-8'); |
| 69 | + } |
| 70 | + |
| 71 | + if (!http_options.headers.Accept) { |
| 72 | + http_options.headers.Accept = 'application/json'; |
| 73 | + } |
| 74 | + |
| 75 | + if (!http_options.headers['Content-Type']) { |
| 76 | + http_options.headers['Content-Type'] = 'application/json'; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Enable full request response logging in development/non-production environment only |
| 81 | + if (configuration.default_options.mode !== 'live' && process.env.NODE_ENV === 'development') { |
| 82 | + console.dir(JSON.stringify(http_options.headers)); |
| 83 | + console.dir(request_data); |
| 84 | + } |
| 85 | + |
| 86 | + // client is http or https library |
| 87 | + var req = client.request(http_options); |
| 88 | + req.on('error', function (e) { |
| 89 | + console.log('problem with request: ' + e.message); |
| 90 | + cb(e, null); |
| 91 | + }); |
| 92 | + |
| 93 | + req.on('response', function (res) { |
| 94 | + var response = ''; |
| 95 | + |
| 96 | + //do not setEndcoding with browserify |
| 97 | + if (res.setEncoding) { |
| 98 | + res.setEncoding('utf8'); |
| 99 | + } |
| 100 | + |
| 101 | + res.on('data', function (chunk) { |
| 102 | + response += chunk; |
| 103 | + }); |
| 104 | + |
| 105 | + res.on('end', function () { |
| 106 | + var err = null; |
| 107 | + |
| 108 | + try { |
| 109 | + if (process.env.NODE_ENV === 'development') { |
| 110 | + console.dir("Development request log:"); |
| 111 | + console.dir(JSON.stringify(res.headers)); |
| 112 | + console.dir(response); |
| 113 | + } |
| 114 | + |
| 115 | + //Set response to be parsed JSON object if data received is json |
| 116 | + //expect that content-type header has application/json when it |
| 117 | + //returns data |
| 118 | + if (typeof res.headers['content-type'] === "string" && res.headers['content-type'].match(/^application\/json(?:;.*)?$/) !== null) { |
| 119 | + response = JSON.parse(response); |
| 120 | + } |
| 121 | + //Set response to an empty object if no data was received |
| 122 | + if (response === '') { |
| 123 | + response = {}; |
| 124 | + } |
| 125 | + response.httpStatusCode = res.statusCode; |
| 126 | + } catch (e) { |
| 127 | + err = new Error('Invalid JSON Response Received.'); |
| 128 | + err.error = { |
| 129 | + name: 'Invalid JSON Response Received, JSON Parse Error.' |
| 130 | + }; |
| 131 | + err.response = response; |
| 132 | + err.httpStatusCode = res.statusCode; |
| 133 | + response = null; |
| 134 | + } |
| 135 | + |
| 136 | + if (!err && (res.statusCode < 200 || res.statusCode >= 300)) { |
| 137 | + err = new Error('Response Status : ' + res.statusCode); |
| 138 | + err.response = response; |
| 139 | + if (process.env.NODE_ENV === 'development') { |
| 140 | + err.response_stringified = JSON.stringify(response); |
| 141 | + } |
| 142 | + err.httpStatusCode = res.statusCode; |
| 143 | + response = null; |
| 144 | + } |
| 145 | + cb(err, response); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + if (request_data) { |
| 150 | + req.write(request_data); |
| 151 | + } |
| 152 | + req.end(); |
| 153 | +}; |
0 commit comments