-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
569 lines (515 loc) · 17.9 KB
/
index.js
File metadata and controls
569 lines (515 loc) · 17.9 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/**
* refocus-client entry point
*/
const fs = require('fs');
const req = require('./lib/requestWrapper');
const mapSeries = require('promise-map-series');
const globalTunnel = require('global-tunnel');
/* Set up proxy using the "http_proxy" environment variable. */
globalTunnel.initialize();
/**
* RefocusClient wraps some basic Refocus API functionality.
*/
class RefocusClient {
/**
* Creates and initializes a RefocusClient.
*
* @constructor
* @param {string} url - Refocus server http(s) url.
* @param {string} version - Refocus server API version, e.g. "v1".
* @param {string} token - Refocus API token.
*/
constructor(url, version, token) {
this.url = url;
this.version = version;
this.token = token;
} // constructor
// --------------------------------------------------------------------------
// Functions for working with Subjects...
// --------------------------------------------------------------------------
/**
* Retrieve a subject hierarchy.
*
* @param {String} absolutePath - The absolutePath of the root subject of the
* hierarhcy to retrieve.
* @returns {Promise} a Bluebird Promise which resolves to the hierarchy
* json.
*/
getHierarchy(absolutePath) {
return req.get(this.token,
`${this.url}/${this.version}/subjects/${absolutePath}/hierarchy`);
} // getHierarchy
/**
* Retrieve all subjects, optional query params for filter/sort/limit/offset.
*
* @param {String} queryParams - Query params (no leading "?").
* @returns {Promise} a Bluebird Promise which resolves to an array of
* subjects.
*/
getSubjects(queryParams) {
let u = `${this.url}/${this.version}/subjects`;
if (queryParams) {
u += `?${queryParams}`;
}
return req.get(this.token, u);
} // getSubjects
/**
* Retrieve the specified subject. Limit the fields returned by providing an
* optional array of field names.
*
* @param {String} absolutePath - The absolutePath of the subject to
* retrieve.
* @param {Array} fields - Optional array of field names to return.
* @returns {Promise} a Bluebird Promise which resolves to the specified
* subject.
*/
getSubject(absolutePath, fields = []) {
let u = `${this.url}/${this.version}/subjects/${absolutePath}`;
if (Array.isArray(fields) && fields.length > 0) {
u += `?fields=${fields.join()}`;
}
return req.get(this.token, u);
} // getSubject
/**
* Create a new Subject as a child of the specified parent Subject.
*
* @param {String} parentAbsolutePath - The absolutePath of the new Subject's
* parent.
* @param {Object} newSubject - The new Subject to create.
* @returns {Promise} A Bluebird Promise which resolves to the newly created
* Subject.
*/
addChildSubject(parentAbsolutePath, newSubject) {
return req.post(this.token,
`${this.url}/${this.version}/subjects/${parentAbsolutePath}/child`,
newSubject);
} // addChildSubject
/**
* For each element in the childrenToAdd array, create a new Subject as a
* child of the specified parent Subject.
*
* @param {Array} childrenToAdd - An array of objects where each object has
* a "parentAbsolutePath" attribute and a "subject" attribute (the
* subject being added).
* @returns {Promise} A Bluebird Promise which resolves to an array of
* the new Subjects.
*/
addChildSubjects(childrenToAdd) {
return mapSeries(childrenToAdd,
(i) => this.addChildSubject(i.parentAbsolutePath, i.subject),
this);
} // addChildSubjects
/**
* Create a new root Subject.
*
* @param {String} newSubject - The new Subject to create.
* @returns {Promise} A Bluebird Promise which resolves to the newly created
* Subject.
*/
addRootSubject(newSubject) {
return req.post(this.token, `${this.url}/${this.version}/subjects`,
newSubject);
} // addRootSubject
/**
* Create new root Subjects.
*
* @param {Array} rootSubjectsToAdd - The new Subjects to create.
* @returns {Promise} A Bluebird Promise which resolves to an array of the
* newly created Subjects.
*/
addRootSubjects(rootSubjectsToAdd) {
return mapSeries(rootSubjectsToAdd, this.addRootSubject, this);
} // addRootSubjects
/**
* Update a subject, modifying only the attributes you provide.
*
* @param {String} absolutePath - The absolutePath of the Subject to patch.
* @param {Object} subject - An object containing the attributes you want to
* update.
* @returns {Promise} A Bluebird Promise which resolves to the patched
* Subject.
*/
patchSubject(absolutePath, subject) {
return req.patch(this.token,
`${this.url}/${this.version}/subjects/${absolutePath}`, subject);
} // patchSubject
/**
* Updates multiple subjects sequentially, modifying only the attributes you
* provide.
*
* @param {Array} toPatch - An array of objects where each object has an
* "absolutePath" attribute (which subject to patch) and a "subject"
* attribute (the attributes to patch for that subject).
* @returns {Promise} A Bluebird Promise which resolves to an array of
* the patched Subjects.
*/
patchSubjects(toPatch) {
return mapSeries(toPatch,
(i) => this.patchSubject(i.absolutePath, i.subject),
this);
} // patchSubjects
/**
* Delete the specified subject.
*
* @param {String} absolutePath - The absolutePath of the Subject to delete.
* @returns {Promise} A Bluebird Promise which resolves to the deleted
* Subject.
*/
deleteSubject(absolutePath) {
return req.delete(this.token,
`${this.url}/${this.version}/subjects/${absolutePath}`);
} // deleteSubject
/**
* Deletes multiple subjects sequentially.
*
* @param {Array} toDelete - An array of absolutePaths.
* @returns {Promise} A Bluebird Promise which resolves to an array of
* the deleted Subjects.
*/
deleteSubjects(toDelete) {
return mapSeries(toDelete, this.deleteSubject, this);
} // deleteSubjects
/**
* Deletes the specified tag from the specified subject.
*
* @param {String} absolutePath - The absolutePath of the Subject.
* @param {String} tag - The tag to delete.
* @returns {Promise} A Bluebird Promise which resolves to the updated
* Subject.
*/
deleteSubjectTag(absolutePath, tag) {
return req.delete(this.token,
`${this.url}/${this.version}/subjects/${absolutePath}/tags/${tag}`);
} // deleteSubjectTag
/**
* Deletes all tags from the specified subject.
*
* @param {String} absolutePath - The absolutePath of the Subject.
* @returns {Promise} A Bluebird Promise which resolves to the updated
* Subject.
*/
deleteSubjectTags(absolutePath) {
return req.delete(this.token,
`${this.url}/${this.version}/subjects/${absolutePath}/tags`);
} // deleteSubjectTags
// --------------------------------------------------------------------------
// Functions for working with Aspects...
// --------------------------------------------------------------------------
/**
* Retrieve all Aspects.
*
* @returns {Promise} A Bluebird promise which resolves to an array of
* Aspects.
*/
getAspects() {
return req.get(this.token, `${this.url}/${this.version}/aspects`);
} // getAspects
/**
* Retrieve the specified Aspect.
*
* @param {String} name - The name of the Aspect to retrieve.
* @returns {Promise} A Bluebird Promise which resolves to the specified
* Aspect.
*/
getAspect(name) {
return req.get(this.token, `${this.url}/${this.version}/aspects/${name}`);
} // getAspect
/**
* Create a new Aspect.
*
* @param {Object} aspect - The aspect to create.
* @returns {Promise} A Bluebird Promise which resolves to the newly created
* Aspect.
*/
addAspect(aspect) {
return req.post(this.token, `${this.url}/${this.version}/aspects`, aspect);
} // addAspect
/**
* Create new Aspects.
*
* @param {Array} aspects - The aspects to create.
* @returns {Promise} A Bluebird Promise which resolves to an array of the
* the newly created Aspects.
*/
addAspects(aspects) {
return mapSeries(aspects, this.addAspect, this);
} // addAspects
/**
* Update an Aspect, modifying only the attributes you provide.
*
* @param {String} name - The name of the Aspect to patch.
* @param {Object} aspect - An object containing the attributes you want to
* update.
* @returns {Promise} A Bluebird Promise which resolves to the patched
* Aspect.
*/
patchAspect(name, aspect) {
return req.patch(this.token, `${this.url}/${this.version}/aspects/${name}`,
aspect);
} // patchAspect
/**
* Updates multiple Aspects sequentially, modifying only the attributes you
* provide.
*
* @param {Array} toPatch - An array of objects where each object has a
* "name" attribute (which aspect to patch) and an "aspect" attribute (the
* attributes to patch for that aspect).
* @returns {Promise} A Bluebird Promise which resolves to an array of
* the patched Aspects.
*/
patchAspects(toPatch) {
return mapSeries(toPatch,
(i) => this.patchAspect(i.name, i.aspect),
this);
} // patchAspects
/**
* Delete the specified Aspect.
*
* @param {String} name - The name of the Aspect to delete.
* @returns {Promise} A Bluebird Promise which resolves to the deleted
* Aspect.
*/
deleteAspect(name) {
return req.delete(this.token, `${this.url}/${this.version}/aspects/${name}`);
} // deleteAspect
/**
* Deletes multiple Aspects sequentially.
*
* @param {Array} toDelete - An array of names.
* @returns {Promise} A Bluebird Promise which resolves to an array of
* the deleted Aspects.
*/
deleteAspects(toDelete) {
return mapSeries(toDelete, this.deleteAspect, this);
} // deleteAspects
// --------------------------------------------------------------------------
// Functions for working with Samples...
// --------------------------------------------------------------------------
/**
* Insert or update an array of Samples asynchronously. If a sample's "value"
* attribute is not type string, converts it to string.
*
* @param {Array} arr - The array of Samples to upsert.
* @returns {Promise} A Bluebird Promise which resolves to the status OK.
*/
bulkUpsertSamples(arr) {
arr.forEach((sample) => {
if (typeof sample.value !== 'string') {
sample.value = sample.value.toString();
}
});
return req.post(this.token,
`${this.url}/${this.version}/samples/upsert/bulk`,
arr);
} // bulkUpsertSamples
/**
* Retrieve the specified sample.
*
* @param {String} name - The name of the sample to retrieve.
* @returns {Promise} a Bluebird Promise which resolves to the specified
* sample.
*/
getSample(name) {
return req.get(this.token, `${this.url}/${this.version}/samples/${name}`);
} // getSample
// --------------------------------------------------------------------------
// Functions for working with Perspectives...
// --------------------------------------------------------------------------
/**
* Retrieve all Perspectives.
*
* @returns {Promise} A Bluebird promise which resolves to an array of
* Perspectives.
*/
getPerspectives() {
return req.get(this.token, `${this.url}/${this.version}/perspectives`);
} // getPerspectives
/**
* Retrieve the specified Perspective.
*
* @param {String} name - The name of the Perspective to retrieve.
* @returns {Promise} A Bluebird Promise which resolves to the specified
* Perspective.
*/
getPerspective(name) {
return req.get(this.token,
`${this.url}/${this.version}/perspectives/${name}`);
} // getPerspective
/**
* Create a new Perspective.
*
* @param {Object} perspective - The Perspective to create.
* @returns {Promise} A Bluebird Promise which resolves to the newly created
* Aspect.
*/
addPerspective(perspective) {
return req.post(this.token, `${this.url}/${this.version}/perspectives`,
perspective);
} // addPerspective
/**
* Create new Perspectives.
*
* @param {Array} perspectives - The Perspectives to create.
* @returns {Promise} A Bluebird Promise which resolves to the newly created
* Perspectives.
*/
addPerspectives(perspectives) {
return mapSeries(perspectives, this.addPerspective, this);
} // addPerspectives
/**
* Update a Perspective, modifying only the attributes you provide.
*
* @param {String} name - The name of the Perspective to patch.
* @param {Object} perspective - An object containing the attributes you want
* to update.
* @returns {Promise} A Bluebird Promise which resolves to the patched
* Perspective.
*/
patchPerspective(name, perspective) {
return req.patch(this.token,
`${this.url}/${this.version}/perspectives/${name}`, perspective);
} // patchPerspective
/**
* Updates multiple Perspectives sequentially, modifying only the attributes
* you provide.
*
* @param {Array} toPatch - An array of objects where each object has a
* "name" attribute (which perspective to patch) and a "perspective"
* attribute (the attributes to patch for that perspective).
* @returns {Promise} A Bluebird Promise which resolves to an array of
* the patched Perspectives.
*/
patchPerspectives(toPatch) {
return mapSeries(toPatch,
(i) => this.patchPerspective(i.name, i.perspective),
this);
} // patchAspects
/**
* Delete the specified Perspective.
*
* @param {String} name - The name of the Perspective to delete.
* @returns {Promise} A Bluebird Promise which resolves to the deleted
* Perspective.
*/
deletePerspective(name) {
return req.delete(this.token,
`${this.url}/${this.version}/perspectives/${name}`);
} // deletePerspective
/**
* Deletes multiple Perspectives sequentially.
*
* @param {Array} toDelete - An array of names.
* @returns {Promise} A Bluebird Promise which resolves to an array of
* the deleted Perspectives.
*/
deletePerspectives(toDelete) {
return mapSeries(toDelete, this.deletePerspective, this);
} // deleteAspects
// --------------------------------------------------------------------------
// Functions for working with Lenses...
// --------------------------------------------------------------------------
/**
* Retrieve all Lenses.
*
* @returns {Promise} A Bluebird promise which resolves to an array of
* Lenses.
*/
getLenses() {
return req.get(this.token, `${this.url}/${this.version}/lenses`);
} // getAspects
/**
* Retrieve the specified Lens.
*
* @param {String} name - The name of the Lens to retrieve.
* @returns {Promise} A Bluebird Promise which resolves to the specified
* Lens.
*/
getLens(name) {
return req.get(this.token, `${this.url}/${this.version}/lenses/${name}`);
} // getAspect
/**
* Create a new Lens.
*
* @param {String} pathToLibrary - Path to the library zip file.
* @param {Object} lens - Optional object to override what the library
* specifies.
* @returns {Promise} A Bluebird Promise which resolves to the newly created
* Lens.
*/
addLens(pathToLibrary, lens) {
const lensToPost = lens || {};
lensToPost.library = fs.createReadStream(pathToLibrary);
// Need this to resolve https://github.com/request/request/issues/1761
lensToPost.isPublished = new Boolean(lensToPost.isPublished).toString();
return req.postMultipart(this.token, `${this.url}/${this.version}/lenses`,
lensToPost);
} // addLens
/**
* Delete the specified Lens.
*
* @param {String} name - The name of the Lens to delete.
* @returns {Promise} A Bluebird Promise which resolves to the deleted
* Lens.
*/
deleteLens(name) {
return req.delete(this.token, `${this.url}/${this.version}/lenses/${name}`);
} // deleteLens
// --------------------------------------------------------------------------
// Functions for working with Global Config...
// --------------------------------------------------------------------------
/**
* Retrieve all Global Config items.
*
* @returns {Promise} A Bluebird promise which resolves to an array of
* Global Config items.
*/
getGlobalConfigItems() {
return req.get(this.token, `${this.url}/${this.version}/globalconfig`);
} // getGlobalConfigItems
/**
* Retrieve the specified Global Config item.
*
* @param {String} key - The key of the Global Config item to retrieve.
* @returns {Promise} A Bluebird Promise which resolves to the specified
* Global Config item.
*/
getGlobalConfigItem(key) {
return req.get(this.token,
`${this.url}/${this.version}/globalconfig/${key}`);
} // getGlobalConfigItem
/**
* Create a new Global Config item.
*
* @param {Object} globalConfig - The Global Config item to create.
* @returns {Promise} A Bluebird Promise which resolves to the newly created
* Global Config item.
*/
addGlobalConfigItem(globalConfig) {
return req.post(this.token, `${this.url}/${this.version}/globalconfig`,
globalConfig);
} // addGlobalConfigItem
/**
* Update the value of a Global Config item.
*
* @param {String} key - The key of the Global Config item to patch.
* @param {Object} value - The new value you want to assign.
* @returns {Promise} A Bluebird Promise which resolves to the patched
* Global Config item.
*/
patchGlobalConfigItem(key, value) {
return req.patch(this.token,
`${this.url}/${this.version}/globalconfig/${key}`, { value });
} // patchGlobalConfigItem
/**
* Delete the specified Global Config item.
*
* @param {String} key - The key of the Global Config item to delete.
* @returns {Promise} A Bluebird Promise which resolves to the deleted
* Global Config item.
*/
deleteGlobalConfigItem(key) {
return req.delete(this.token,
`${this.url}/${this.version}/globalconfig/${key}`);
} // deleteGlobalConfigItem
} // RefocusClient
module.exports = RefocusClient;