-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrestApi.js
136 lines (111 loc) · 3.85 KB
/
restApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"use strict";
var utils = require('./utils');
var configuration = require('./configure');
var request = require('requestretry');
var qs = require('querystring');
exports.get = function executeGetRequest(path, http_options, cb) {
execute('GET', path, null, http_options, cb);
};
exports.post = function executePostRequest(path, data, http_options, cb) {
execute('POST', path, data, http_options, cb);
};
exports.delete = function executeDeleteRequest(path, http_options, cb) {
execute('DELETE', path, null, http_options, cb);
};
exports.patch = function executePostRequest(path, data, http_options, cb) {
execute('PATCH', path, data, http_options, cb);
};
function setRetryPolicy() {
request.defaults({
json: true,
maxAttempts: configuration.default_options.maxAttempts,
retryDelay: configuration.default_options.retryDelay,
retryStrategy: configuration.default_options.retryStrategy
});
}
function getHost() {
return configuration.default_options.host;
}
function getBasicHttpOptions(http_options) {
var apiKey = configuration.default_options.api_key;
configuration.default_http_options.headers['Authorization'] = 'GenieKey ' + apiKey;
if (!http_options) {
http_options = utils.merge({}, configuration.default_http_options, true);
} else {
http_options = utils.merge(http_options, configuration.default_http_options, true);
}
http_options.host = getHost();
return http_options;
}
function getPathWithIdentifier(path, params, actionPath) {
var pathParams;
var actPath = actionPath ? actionPath : "";
if (typeof params !== 'string') {
pathParams = params["identifier"] + actPath;
if (params["identifierType"] != null) {
pathParams += "?identifierType=" + params["identifierType"];
}
} else {
pathParams = params;
}
return params ? (path + pathParams) : (path);
}
exports.getPath = function (path, params, actionPath) {
return getPathWithIdentifier(path, params, actionPath)
};
exports.getPathWithData = function (path, params, actionPath, data) {
var pathWithData = getPathWithIdentifier(path, params, actionPath);
var dataParam = "";
if (typeof data !== 'string') {
dataParam = qs.stringify(data);
} else {
dataParam = data;
}
return data ? (pathWithData + "&" + dataParam) : pathWithData;
};
exports.getPathWithListParams = function (path, params) {
if (typeof params !== 'string') {
params = qs.stringify(params);
}
return params ? (path + "?" + params) : path;
};
function execute(method, path, data, http_options, cb) {
setRetryPolicy();
if (typeof http_options === "function") {
cb = http_options;
http_options = null;
}
http_options = getBasicHttpOptions(http_options);
http_options.method = method;
if (method === 'GET' || method === 'DELETE') {
http_options.uri = http_options.host + path;
http_options.json = true
} else {
http_options.uri = http_options.host + path;
http_options.body = data;
http_options.json = true
}
request(http_options, function (error, response, body) {
handleResponse(error, response, body, cb)
});
}
function handleResponse(error, response, body, cb) {
if (error) {
console.log('Problem with request: ', error);
cb(error, null);
return;
}
var err = null, res = null;
if (response.statusCode < 200 || response.statusCode >= 300) {
err = JSON.parse(JSON.stringify(body));
if (process.env.NODE_ENV === 'development') {
err.response_stringified = JSON.stringify(response);
}
err.httpStatusCode = response.statusCode;
response = null;
} else {
var bodyJson = JSON.stringify(body);
res = JSON.parse(bodyJson);
}
cb(err, res);
}