-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
378 lines (339 loc) · 11.6 KB
/
index.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
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
/**
* refocus-client entry point
*/
const fs = require('fs');
const req = require('./lib/requestWrapper');
/**
* 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 all subjects.
*
* @returns {Promise} a Bluebird Promise which resolves to an array of
* subjects.
*/
getSubjects() {
return req.get(this.token, `${this.url}/${this.version}/subjects`);
} // getSubjects
/**
* Retrieve the specified subject.
*
* @param {String} absolutePath - The absolutePath of the subject to
* retrieve.
* @returns {Promise} a Bluebird Promise which resolves to the specified
* subject.
*/
getSubject(absolutePath) {
return req.get(this.token,
`${this.url}/${this.version}/subjects/${absolutePath}`);
} // 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
/**
* 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
/**
* 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
/**
* 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
// --------------------------------------------------------------------------
// 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
/**
* 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
/**
* 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
// --------------------------------------------------------------------------
// Functions for working with Samples...
// --------------------------------------------------------------------------
/**
* Insert or update an array of Samples asynchronously.
*
* @param {Array} arr - The array of Samples to upsert.
* @returns {Promise} A Bluebird Promise which resolves to the status OK.
*/
bulkUpsertSamples(arr) {
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
/**
* 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
/**
* 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
// --------------------------------------------------------------------------
// 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;