-
Notifications
You must be signed in to change notification settings - Fork 2
/
samanage-api.js
353 lines (343 loc) · 11 KB
/
samanage-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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
var cross_fetch = require('cross-fetch')
var urlx = require('url')
var path = require('path')
var promiseRetry = require('promise-retry')
var isFunction = (obj) => (!!(obj && obj.constructor && obj.call && obj.apply))
var functionProto = (name, func) => (name + func.toString().match(/\(.*\)/)[0])
function describeObject(obj) {
var ret = []
var consts = []
Object.keys(obj).forEach(function(attr) {
if (obj.hasOwnProperty(attr)) {
if (isFunction(obj[attr])) {
ret.push(functionProto(attr, obj[attr]))
} else {
consts.push(attr)
}
}
})
if (consts.length > 0) ret.push({constants: consts})
return ret
}
var SamanageAPI = {
getPaginationInfo: function (response_headers) {
return {
per_page: response_headers.get('x-per-page'),
current_page: response_headers.get('x-current-page'),
total_count: response_headers.get('x-total-count'),
total_pages: response_headers.get('x-total-pages')
}
},
Filters: function() { this.attrs={} },
validateParams: function(func, all, conds) {
if (!SamanageAPI.debug) return
conds.forEach(function(cond, index) {
var type = typeof all[index]
if (!type) throw func + ': parameter ' + index + ' is missing'
if (!type.match(cond)) throw func + ': parameter ' + index + ' must match ' + cond
})
},
export: function(object_type, scope) {
var action = function(filters) {
SamanageAPI.validateParams('get(filters)', arguments, [/object/])
return {
object_type: object_type,
scope: scope,
path: path.join(scope || '', object_type + 's.csv?export=true&format=csv&') + filters.to_query(),
method: 'GET',
responseHandler: ()=>[]
}
}
action.log = SamanageAPI.log
action.object_type = object_type
action.scope = scope
return action
},
get: function(object_type, scope) {
var action = function(filters) {
SamanageAPI.validateParams('get(filters)', arguments, [/object/])
return {
object_type: object_type,
scope: scope,
path: path.join(scope || '', object_type + 's.json?') + filters.to_query(),
method: 'GET'
}
}
action.log = SamanageAPI.log
action.object_type = object_type
action.scope = scope
return action
},
create: function(object_type, scope) {
var action = function(object) {
SamanageAPI.validateParams('create(object)', arguments, [/object/])
var wrapper = {}
wrapper[object_type] = object
return {
object_type: object_type,
scope: scope,
path: path.join(scope || '', object_type +'s.json'),
body: JSON.stringify(wrapper),
method: 'POST'
}
}
action.log = SamanageAPI.log
action.object_type = object_type
action.scope = scope
return action
},
update: function(object_type, scope) {
var action = function(object_id, object) {
SamanageAPI.validateParams('update(object_id, object)', arguments, [/string|number/, /object/])
var wrapper = {}
wrapper[object_type] = object
return {
object_type: object_type,
scope: scope,
path: path.join(scope || '', object_type + 's/' + object_id + '.json'),
body: JSON.stringify(wrapper),
method: 'PUT'
}
}
action.log = SamanageAPI.log
action.object_type = object_type
action.scope = scope
return action
},
Connection: function(token, origin = 'https://api.samanage.com') {
if (!(origin && origin.match(/^https|localhost/))) throw "origin must start with 'https://'"
this.debug = false
this.connection = this
this.origin = origin
this.headers = {
'X-Samanage-Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
//'Accept': 'application/vnd.samanage.v2.1+json'
'Accept': '*/*'
}
this.valid_request_opts = ['timeout', 'mode', 'cache', 'credentials', 'referrer', 'follow', 'agent']
this.request_opts = {
}
this.retry_codes = [408, 409, 429, 503, 504]
this.retry_opts = {
retries: 2,
factor: 2,
minTimeout: 1 * 1000,
maxTimeout: 60 * 100,
randomize: false
}
},
debug: false,
log: function() {
// eslint-disable-next-line no-console
if (SamanageAPI.debug) console.log('DEBUG', ...arguments)
}
}
function getterAddData({data, ref, pagination_info}) {
var state = ref
//console.log('add_data: ', data, 'state: ', state)
var connection = state.connection
var log = state.log
var filters = state.filters
var current_filters = filters ? filters.clone() : new SamanageAPI.Filters()
var transaction_ref = 'GETTER REQ:' + connection.origin + '/' + state.action(current_filters).path
if (data.length > 0) {
log(transaction_ref + ': recieved ' + data.length + ' new items')
data.forEach(function(item) {
state.data[item.id] = item
})
var next_page_filters = current_filters.clone().next_page()
state.filters = next_page_filters
if (pagination_info.total_pages >= next_page_filters.attrs.page) {
log(transaction_ref + ': next page:' + next_page_filters.attrs.page)
connection.callSamanageAPI(state.action(next_page_filters), state).then(
getterAddData
).catch(function (err) {
log(transaction_ref + ': REJECTED: ' + err)
state.reject(err)
})
}
else {
log(transaction_ref + ': RESOLVED (no pagination)')
state.resolve(state.data)
}
}
else {
log(transaction_ref + ': RESOLVED (empty page)')
state.resolve(state.data)
}
}
Object.assign(SamanageAPI.Connection, {
HTTP_ERROR: 'HTTP Error',
NON_HTTP_ERROR: 'Non HTTP Error',
GET_BODY_ERROR: 'Error getting response body',
INVALID_JSON: 'Invalid JSON response data'
})
SamanageAPI.Connection.prototype = {
getter: function(object_type, filters, scope, getter_log) {
var connection = this
var promise = new Promise(function(res, rej) {
var state = {
data: {},
action: SamanageAPI.get(object_type, scope),
filters: filters,
connection: connection,
resolve: res,
reject: rej
}
var action = state.action(filters || new SamanageAPI.Filters())
state.log = getter_log || SamanageAPI.log
connection.callSamanageAPI(action, state).then(getterAddData).catch(rej)
})
return promise
},
callSamanageAPI: function(request, ref, retry_opts) {
var connection = this
var retry_setup = (retry_opts || request.retry_opts || connection.retry_opts)
return promiseRetry(
function (retry, number) {
//console.log('attempt number', number);
return connection.singleSamanageAPI(request, ref).catch(function(err) {
//console.log(err, connection.retry_codes)
err.attempts = number
if ((err.error == SamanageAPI.Connection.HTTP_ERROR) && connection.retry_codes.includes(err.httpStatus) && retry(err)) return
throw err
})
},
retry_setup
)
},
processResponse: function(request, response, resolve, reject, ref) {
var log = request.log || SamanageAPI.log
response.text().then(function(body) {
try {
log('callSamanageAPI ok:', {ref: ref, body: body.substring(0,100), headers: response.headers})
resolve({
data: (request.responseHandler || JSON.parse)(body),
ref: ref,
pagination_info: SamanageAPI.getPaginationInfo(response.headers)
})
} catch(e) {
log('callSamanageAPI exception:', ref, e)
reject({
error: SamanageAPI.Connection.INVALID_JSON,
info: body,
ref: ref,
exception: e
})
}
}).catch(function(error) {
log('callSamanageAPI error:', ref, error)
reject({
error: SamanageAPI.Connection.GET_BODY_ERROR,
info: error,
ref: ref
})
}) // response.text().then(...).catch(...)
},
singleSamanageAPI: function(request, ref) {
var connection = this
var log = request.log || SamanageAPI.log
return new Promise(function(resolve, reject) {
var options = {
redirect: 'follow',
headers: connection.headers,
method: request.method
}
var url=urlx.resolve(connection.origin, request.path)
connection.valid_request_opts.forEach((opt) => {
if (typeof connection.request_opts[opt] !== "undefined") {
options[opt] = connection.request_opts[opt]
}
})
ref = ref || url
if (request.body) options['body'] = request.body
log('callSamanageAPI:', { ref: ref, options: options, request: request })
cross_fetch(url, options).then(function(response) {
if ((response.status < 200) || (response.status >= 300)) {
log('callSamanageAPI HTTP error:', ref, response.status)
reject({
error: SamanageAPI.Connection.HTTP_ERROR,
httpStatus: response.status,
info: response.statusText,
ref: ref
})
} else {
connection.processResponse(request, response, resolve, reject, ref)
}
}).catch(function (error) {
log('callSamanageAPI error:', ref, error)
reject({
error: SamanageAPI.Connection.NON_HTTP_ERROR,
info: error,
ref: ref
})
}) // fetch.then(...).catch(...)
}) // new Promise(...)
}
}
SamanageAPI.Filters.prototype = {
DESC: false,
ASC: true,
add: function(filters_hash) {
var obj = this.attrs
Object.keys(filters_hash).forEach(function(key) {
var value = filters_hash[key]
if ((typeof value == 'object') && key.match(/created|updated/)){ // Date Range
obj[key + '_custom_gte[]'] = value[0]
obj[key + '_custom_lte[]'] = value[1]
} else {
obj[key] = value
}
})
return this
},
between_dates: function(column, date1, date2) {
this.attrs[column + '_custom_gte[]'] = date1
this.attrs[column + '_custom_lte[]'] = date2
return this
},
sort_by: function(column) {
this.attrs['sort_by'] = column
return this
},
sort_order: function(order) {
this.attrs['sort_order'] = (order?'ASC':'DESC')
return this
},
page: function(page) {
this.attrs['page'] = page
return this
},
next_page: function() {
this.attrs['page'] = (this.attrs['page'] || 1) + 1
return this
},
per_page: function(per_page) {
this.attrs['per_page'] = per_page
return this
},
department: function(department_id) {
this.attrs['department'] = department_id
return this
},
title: function(title) {
this.attrs['title'] = title
return this
},
to_query: function() {
var obj = this.attrs
return Object.keys(obj).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])
}).join('&')
},
clone: function() {
var obj = new SamanageAPI.Filters()
Object.assign(obj.attrs, this.attrs)
return obj
}
}
SamanageAPI.help = describeObject(SamanageAPI)
SamanageAPI.Connection.help = describeObject(SamanageAPI.Connection.prototype)
SamanageAPI.Filters.help = describeObject(SamanageAPI.Filters.prototype)
module.exports = SamanageAPI