-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm_api.js
299 lines (233 loc) · 6.36 KB
/
tm_api.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"use strict"
require('http').globalAgent.maxSockets = 5
require('https').globalAgent.maxSockets = 5
var rest = require('rest'),
mime = require('rest/interceptor/mime'),
moment = require('moment'),
crypto = require('crypto'),
util = require('util'),
R = require('ramda')
var client = rest.wrap(mime, { mime: 'application/json' })
var config = require('./tm3_api.json')
// API offset limit
var limit = 100, query_limit = 1000
// TM3 Authorization header
function getHeaders(client) {
var key, auth_scheme, algorithm, timestamp, payload, hmac, signature, auth_header
key = client.key
auth_scheme = 'TM-HMAC-SHA256'
algorithm = 'sha256'
timestamp = moment().utc().format('YYYY-MM-DDTHH:mm:ss')
payload = key + client.shortname + timestamp
hmac = crypto.createHmac(algorithm, client.secret)
hmac.setEncoding('hex')
hmac.write(payload)
hmac.end()
signature = hmac.read()
auth_header = util.format('%s key=%s ts=%s sign=%s', auth_scheme, key, timestamp, signature)
return {'Authorization': auth_header}
}
function getURL(client, type, endpoint, id) {
if(!(R.contains(type, ["getList", "get", "post", "put", "delete"]))) {
return false
}
if(!(endpoint in config.endpoints)) {
return false
}
var url_template = config.schema + "://" + config.host + config.path + config.endpoints[endpoint]
if(type == "get" || type == "put" || type == "delete") {
url_template += "/%d"
}
var url
if(id) {
if(typeof id == "object") {
url = util.format(url_template, client.shortname, id[0], id[1])
}
else {
url = util.format(url_template, client.shortname, id)
}
}
else {
url = util.format(url_template, client.shortname)
}
return url
}
function getParams(payload) {
if(typeof payload === "undefined") {
return {}
}
var params = {}
for(var key in payload) {
// Skip non-allowed optional attributes
if (R.contains(key, config.params_optional)) {
console.log("Attribute skipped: %s", key)
continue
}
params[key] = payload[key]
}
return params
}
function request(options) {
return client(options).then(function(data) {
return new Promise(function(resolve, reject) {
if (data.status.code == 200) {
resolve(data.entity)
}
else {
console.log({message: "API request failed", options: options, response_code: data.status.code, response: data.entity})
var message
if (data.entity.message) {
message = data.entity.message
}
else {
message = "Onbekende fout in API"
}
reject(new Error(message))
}
})
})
}
/**
* Recursively loop through API results using offset
*/
exports.getListAll = function(client, endpoint, payload) {
return getRecursively(client, [], endpoint, payload)
}
function getListRecursively(client, data, endpoint, payload) {
if (typeof payload == "undefined") {
payload = {}
}
return getList(client, endpoint, payload)
.then(function(result) {
if (!result) {
return
}
// More details on "spread": http://stackoverflow.com/a/30734348/3744180
data.push(...result.data)
if (!(result.data) || result.data.length < limit) {
return Promise.resolve(data)
}
if (!("offset" in payload)) {
payload.offset = limit
payload.limit = limit
}
else {
payload.offset += limit
payload.limit = limit
}
return getRecursively(client, data, endpoint, payload)
})
}
exports.getList = function(client, endpoint, payload) {
var url = getURL(client, "getList", endpoint)
if (!url) {
return Promise.reject(new Error('Unknown getList: ' + endpoint))
}
var params = getParams(payload)
var options = { method: 'GET', path: url, params: params }
var headers = getHeaders(client)
if(headers) {
options['headers'] = headers
}
return request(options)
}
exports.get = function(client, endpoint, id, payload) {
var url = getURL(client, "get", endpoint, id)
if (!url) {
return Promise.reject(new Error('Unknown get ' + endpoint))
}
var params = getParams(payload)
var options = { method: 'GET', path: url, params: params }
var headers = getHeaders(client)
if(headers) {
options['headers'] = headers
}
return request(options)
}
exports.put = function(client, endpoint, id, payload) {
var url = getURL(client, "put", endpoint, id)
if (!url) {
return Promise.reject(new Error('Unknown put ' + endpoint))
}
if(Object.keys(payload).length == 0) {
return Promise.resolve()
}
var entity = payload
var options = { method: 'PUT', path: url, params: {}, entity: entity }
var headers = getHeaders(client)
if(headers) {
options['headers'] = headers
}
return request(options)
}
var post = function(client, endpoint, id, payload) {
var url = getURL(client, "post", endpoint, id)
if (!url) {
return Promise.reject(new Error('Unknown post ' + endpoint))
}
var entity = payload
var options = { method: 'POST', path: url, params: {}, entity: entity }
var headers = getHeaders(client)
if(headers) {
options['headers'] = headers
}
return request(options)
}
exports.post = post
var del = function(client, endpoint, id, payload) {
var url = getURL(client, "delete", endpoint, id)
if (!url) {
return Promise.reject(new Error('Unknown delete ' + endpoint))
}
var entity = payload
var options = { method: 'DELETE', path: url, params: {}, entity: entity }
var headers = getHeaders(client)
if(headers) {
options['headers'] = headers
}
return request(options)
}
exports.del = del
/**
* Recursively loop through API results using offset
*/
exports.queryAll = function(client, sql) {
var payload = {
query: sql,
limit: query_limit
}
return queryRecursively(client, [], payload)
}
const queryRecursively = (client, data, payload) => {
if (typeof payload == "undefined") {
payload = {}
}
return _query(client, payload)
.then(function(result) {
if (!result.results) {
return
}
// More details on "spread": http://stackoverflow.com/a/30734348/3744180
data.push(...result.results)
if (!(result.results) || result.results.length < query_limit) {
return Promise.resolve(data)
}
if (!("offset" in payload)) {
payload.offset = query_limit
payload.limit = query_limit
}
else {
payload.offset += query_limit
payload.limit = query_limit
}
return queryRecursively(client, data, payload)
})
}
const _query = (client, payload) => post(client, "queries", null, payload)
exports.query = function(client, sql) {
var payload = {
query: sql
}
return _query(client, payload)
.then(res => res.results)
}