fiftyone.pipeline.cloudrequestengine 4.4.7 → 4.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cloudEngine.js CHANGED
@@ -99,23 +99,22 @@ class CloudEngine extends Engine {
99
99
  const engine = this;
100
100
 
101
101
  return engine.ready().then(function () {
102
-
103
102
  let cloudData = flowData.get('cloud').get('cloud');
104
103
 
105
104
  cloudData = JSON.parse(cloudData);
106
105
  const result = {};
107
106
 
108
107
  if (cloudData && cloudData[engine.dataKey] === null) {
109
- flowData.pipeline.log('error', engine.dataKey + " not populated. " +
110
- cloudData[engine.dataKey + 'nullreason'] !== null ? cloudData[engine.dataKey + 'nullreason'] : "" +
111
- "\n" + "This may be because the provided API key is not authorised for " + engine.dataKey + " queries.");
112
- }
113
- else {
114
-
108
+ flowData.pipeline.log('error', engine.dataKey + ' not populated. ' +
109
+ cloudData[engine.dataKey + 'nullreason'] !== null
110
+ ? cloudData[engine.dataKey + 'nullreason']
111
+ : '' +
112
+ '\n' + 'This may be because the provided API key is not authorised for ' + engine.dataKey + ' queries.');
113
+ } else {
115
114
  // Loop over cloudData.device properties to check if they have a value
116
115
  Object.entries(cloudData[engine.dataKey]).forEach(function ([key, value]) {
117
116
  result[key] = new AspectPropertyValue();
118
-
117
+
119
118
  if (cloudData[engine.dataKey][key + 'nullreason']) {
120
119
  result[key].noValueMessage = cloudData[engine.dataKey][key + 'nullreason'];
121
120
  } else {
@@ -20,13 +20,6 @@
20
20
  * such notice(s) shall fulfill the requirements of that article.
21
21
  * ********************************************************************* */
22
22
 
23
- const require51 = (requestedPackage) => {
24
- try {
25
- return require(__dirname + '/../' + requestedPackage);
26
- } catch (e) {
27
- return require(requestedPackage);
28
- }
29
- };
30
23
  const util = require('util');
31
24
  const Engine = require('fiftyone.pipeline.engines').Engine;
32
25
  const AspectDataDictionary = require('fiftyone.pipeline.engines')
@@ -53,12 +46,13 @@ class CloudRequestEngine extends Engine {
53
46
  * @param {string} options.licenseKey licensekey for cloud service
54
47
  * @param {string} options.baseURL url the cloud service is located at
55
48
  * if overriding default
56
- * @param {string} options.cloudRequestOrigin The value to set for the Origin
49
+ * @param {string} options.cloudRequestOrigin The value to set for the Origin
57
50
  * header when making requests to the cloud service.
58
- * This is used by the cloud service to check that the request is being
51
+ * This is used by the cloud service to check that the request is being
59
52
  * made from a origin matching those allowed by the resource key.
60
- * For more detail, see the 'Request Headers' section in the
53
+ * For more detail, see the 'Request Headers' section in the
61
54
  * <a href="https://cloud.51degrees.com/api-docs/index.html">cloud documentation</a>.
55
+ * @param options.requestClient
62
56
  */
63
57
  constructor (
64
58
  {
@@ -78,11 +72,10 @@ class CloudRequestEngine extends Engine {
78
72
 
79
73
  this.resourceKey = resourceKey;
80
74
  this.licenseKey = licenseKey;
81
- this.cloudRequestOrigin = cloudRequestOrigin
75
+ this.cloudRequestOrigin = cloudRequestOrigin;
82
76
  if (requestClient !== undefined) {
83
77
  this.requestClient = requestClient;
84
- }
85
- else {
78
+ } else {
86
79
  this.requestClient = new RequestClient();
87
80
  }
88
81
 
@@ -170,49 +163,52 @@ class CloudRequestEngine extends Engine {
170
163
  * Typically, cloud will return errors as JSON.
171
164
  * However, transport level errors or other failures can result in
172
165
  * responses that are plain text. This function handles these cases.
173
- *
174
- * @param {String} response the response data to process
166
+ *
167
+ * @param {string} response the response data to process
168
+ * @param responseBody
175
169
  * @returns {Array} The error messages
176
170
  */
177
- getErrorMessages(responseBody){
171
+ getErrorMessages (responseBody) {
178
172
  let errors = [];
179
173
  try {
180
- errors = JSON.parse(responseBody).errors;
174
+ errors = JSON.parse(responseBody).errors;
181
175
  } catch (parseError) {
182
- errors = [ 'Error parsing response - ' + responseBody ];
176
+ errors = ['Error parsing response - ' + responseBody];
183
177
  }
184
- if(responseBody.length == 0) {
185
- errors = [ 'No data in response from cloud service' ];
178
+ if (responseBody.length === 0) {
179
+ errors = ['No data in response from cloud service'];
186
180
  }
187
181
  return errors;
188
182
  }
189
183
 
190
184
  /**
191
185
  * Used to handle errors from http requests
186
+ *
187
+ * @param response
192
188
  */
193
- getErrorsFromResponse(response) {
189
+ getErrorsFromResponse (response) {
194
190
  let content = response;
195
- if(response.content) {
191
+ if (response.content) {
196
192
  content = response.content;
197
193
  }
198
194
 
199
- let errors = this.getErrorMessages(content);
200
- let cloudErrors = [];
201
- errors.forEach(function(errorText) {
195
+ const errors = this.getErrorMessages(content);
196
+ const cloudErrors = [];
197
+ errors.forEach(function (errorText) {
202
198
  cloudErrors.push(new CloudRequestError(
203
- errorText,
204
- response.headers,
205
- response.statusCode));
199
+ errorText,
200
+ response.headers,
201
+ response.statusCode));
206
202
  });
207
203
 
208
- if(cloudErrors.length == 0 &&
204
+ if (cloudErrors.length === 0 &&
209
205
  response.statusCode > 299) {
210
- let message = 'Cloud service returned status code ' +
206
+ const message = 'Cloud service returned status code ' +
211
207
  response.statusCode + ' with content ' + content + '.';
212
- cloudErrors.push(new CloudRequestError(
213
- message,
214
- response.headers,
215
- response.statusCode));
208
+ cloudErrors.push(new CloudRequestError(
209
+ message,
210
+ response.headers,
211
+ response.statusCode));
216
212
  }
217
213
 
218
214
  return cloudErrors;
@@ -238,29 +234,29 @@ class CloudRequestEngine extends Engine {
238
234
  }
239
235
 
240
236
  engine.requestClient.get(url, engine.cloudRequestOrigin)
241
- .then(function (properties) {
242
- const propertiesOutput = {};
237
+ .then(function (properties) {
238
+ const propertiesOutput = {};
243
239
 
244
- properties = JSON.parse(properties);
240
+ properties = JSON.parse(properties);
245
241
 
246
- const products = properties.Products;
242
+ const products = properties.Products;
247
243
 
248
- for (const product in products) {
249
- propertiesOutput[product] = engine.propertiesTransform(
250
- products[product].Properties);
251
- }
244
+ for (const product in products) {
245
+ propertiesOutput[product] = engine.propertiesTransform(
246
+ products[product].Properties);
247
+ }
252
248
 
253
- engine.flowElementProperties = propertiesOutput;
254
- resolve(propertiesOutput);
255
- }).catch(function(response) {
256
- reject(engine.getErrorsFromResponse(response));
257
- });
249
+ engine.flowElementProperties = propertiesOutput;
250
+ resolve(propertiesOutput);
251
+ }).catch(function (response) {
252
+ reject(engine.getErrorsFromResponse(response));
253
+ });
258
254
  });
259
255
  }
260
256
 
261
- propertiesTransform(properties) {
262
- let result = {};
263
- let self = this;
257
+ propertiesTransform (properties) {
258
+ const result = {};
259
+ const self = this;
264
260
  properties
265
261
  .forEach(function (property) {
266
262
  result[property
@@ -274,18 +270,18 @@ class CloudRequestEngine extends Engine {
274
270
  property[metaKey]);
275
271
  }
276
272
  });
277
- return result;
273
+ return result;
278
274
  }
279
275
 
280
- metaPropertyTransform(key, value) {
276
+ metaPropertyTransform (key, value) {
281
277
  switch (key) {
282
- case "itemproperties":
278
+ case 'itemproperties':
283
279
  return this.propertiesTransform(value);
284
280
  default:
285
281
  return value;
286
-
287
282
  }
288
283
  }
284
+
289
285
  /**
290
286
  * Internal function to get data from cloud service
291
287
  *
@@ -308,11 +304,10 @@ class CloudRequestEngine extends Engine {
308
304
  const self = this;
309
305
 
310
306
  return new Promise(function (resolve, reject) {
311
-
312
307
  engine.requestClient.post(
313
- url,
314
- engine.getContent(flowData),
315
- engine.cloudRequestOrigin)
308
+ url,
309
+ engine.getContent(flowData),
310
+ engine.cloudRequestOrigin)
316
311
  .then(function (body) {
317
312
  const data = new AspectDataDictionary({
318
313
  flowElement: engine,
@@ -325,8 +320,8 @@ class CloudRequestEngine extends Engine {
325
320
  flowData.setElementData(data);
326
321
 
327
322
  resolve();
328
- }).catch(function(response) {
329
- self.errors = engine.getErrorsFromResponse(response)
323
+ }).catch(function (response) {
324
+ self.errors = engine.getErrorsFromResponse(response);
330
325
  reject(self.errors);
331
326
  });
332
327
  });
@@ -342,79 +337,80 @@ class CloudRequestEngine extends Engine {
342
337
  const url = this.baseURL + 'evidencekeys';
343
338
  return new Promise(function (resolve, reject) {
344
339
  engine.requestClient.get(url, engine.cloudRequestOrigin)
345
- .then(function (body) {
346
- engine.evidenceKeyFilter = new BasicListEvidenceKeyFilter(
347
- JSON.parse(body)
348
- );
349
-
350
- resolve();
351
- }).catch(function(response) {
352
- reject(engine.getErrorsFromResponse(response));
353
- });
340
+ .then(function (body) {
341
+ engine.evidenceKeyFilter = new BasicListEvidenceKeyFilter(
342
+ JSON.parse(body)
343
+ );
344
+
345
+ resolve();
346
+ }).catch(function (response) {
347
+ reject(engine.getErrorsFromResponse(response));
348
+ });
354
349
  });
355
350
  }
356
351
 
357
352
  /**
358
353
  * Generate the Content to send in the POST request. The evidence keys
359
354
  * e.g. 'query.' and 'header.' have an order of precedence. These are
360
- * added to the evidence in reverse order, if there is conflict then
361
- * the queryData value is overwritten.
355
+ * added to the evidence in reverse order, if there is conflict then
356
+ * the queryData value is overwritten.
362
357
  * 'query.' evidence should take precedence over all other evidence.
363
358
  * If there are evidence keys other than 'query.' that conflict then
364
359
  * this is unexpected so a warning will be logged.
360
+ *
365
361
  * @param {FlowData} flowData
366
362
  * @returns {Evidence} Evidence Dictionary
367
363
  */
368
- getContent(flowData) {
369
- let queryData = {};
364
+ getContent (flowData) {
365
+ const queryData = {};
370
366
 
371
367
  const evidence = flowData.evidence.getAll();
372
368
 
373
- // Add evidence in reverse alphabetical order, excluding special keys.
374
- this.addQueryData(flowData, queryData, evidence, this.getSelectedEvidence(evidence, "other"));
369
+ // Add evidence in reverse alphabetical order, excluding special keys.
370
+ this.addQueryData(flowData, queryData, evidence, this.getSelectedEvidence(evidence, 'other'));
375
371
  // Add cookie evidence.
376
- this.addQueryData(flowData, queryData, evidence, this.getSelectedEvidence(evidence, "cookie"));
372
+ this.addQueryData(flowData, queryData, evidence, this.getSelectedEvidence(evidence, 'cookie'));
377
373
  // Add header evidence.
378
- this.addQueryData(flowData, queryData, evidence, this.getSelectedEvidence(evidence, "header"));
374
+ this.addQueryData(flowData, queryData, evidence, this.getSelectedEvidence(evidence, 'header'));
379
375
  // Add query evidence.
380
- this.addQueryData(flowData, queryData, evidence, this.getSelectedEvidence(evidence, "query"));
376
+ this.addQueryData(flowData, queryData, evidence, this.getSelectedEvidence(evidence, 'query'));
381
377
 
382
378
  return queryData;
383
379
  }
384
380
 
385
381
  /**
386
382
  * Add query data to the evidence.
383
+ *
384
+ * @param flowData
387
385
  * @param {object} queryData The destination dictionary to add query data to.
388
386
  * @param {Evidence} allEvidence All evidence in the flow data. This is used to
389
387
  * report which evidence keys are conflicting.
390
388
  * @param {object} evidence Evidence to add to the query Data.
391
389
  */
392
- addQueryData(flowData, queryData, allEvidence, evidence) {
393
-
390
+ addQueryData (flowData, queryData, allEvidence, evidence) {
394
391
  for (const [evidenceKey, evidenceValue] of Object.entries(evidence)) {
395
392
  // Get the key parts
396
- const evidenceKeyParts = evidenceKey.split('.')
397
- const prefix = evidenceKeyParts[0]
393
+ const evidenceKeyParts = evidenceKey.split('.');
394
+ const prefix = evidenceKeyParts[0];
398
395
  const suffix = evidenceKeyParts[1];
399
396
 
400
397
  // Check and add the evidence to the query parameters.
401
- if ((suffix in queryData) == false) {
402
- queryData[suffix] = evidenceValue
403
- }
404
- else {
398
+ if ((suffix in queryData) === false) {
399
+ queryData[suffix] = evidenceValue;
400
+ } else {
405
401
  // If the queryParameter exists already.
406
- // Get the conflicting pieces of evidence and then log a
402
+ // Get the conflicting pieces of evidence and then log a
407
403
  // warning, if the evidence prefix is not query. Otherwise a
408
- // warning is not needed as query evidence is expected
404
+ // warning is not needed as query evidence is expected
409
405
  // to overwrite any existing evidence with the same suffix.
410
- if (prefix !== "query") {
411
- let conflicts = {}
406
+ if (prefix !== 'query') {
407
+ const conflicts = {};
412
408
  for (const [key, value] of Object.entries(allEvidence)) {
413
409
  if (key !== evidenceKey && key.includes(suffix)) {
414
- conflicts[key] = value
410
+ conflicts[key] = value;
415
411
  }
416
412
  }
417
-
413
+
418
414
  let conflictStr = '';
419
415
  for (const [key, value] of Object.entries(conflicts)) {
420
416
  if (conflictStr.length > 0) {
@@ -422,8 +418,8 @@ class CloudRequestEngine extends Engine {
422
418
  }
423
419
  conflictStr += util.format('%s:%s', key, value);
424
420
  }
425
-
426
- let warningMessage = util.format(
421
+
422
+ const warningMessage = util.format(
427
423
  errorMessages.evidenceConflict,
428
424
  evidenceKey,
429
425
  evidenceValue,
@@ -431,53 +427,54 @@ class CloudRequestEngine extends Engine {
431
427
  flowData.pipeline.log('warn', warningMessage);
432
428
  }
433
429
  // Overwrite the existing queryParameter value.
434
- queryData[suffix] = evidenceValue
430
+ queryData[suffix] = evidenceValue;
435
431
  }
436
432
  }
437
433
  }
438
434
 
439
435
  /**
440
436
  * Get evidence with specified prefix.
437
+ *
441
438
  * @param {Evidence} evidence All evidence in the flow data.
442
439
  * @param {stirng} type Required evidence key prefix
443
440
  */
444
- getSelectedEvidence(evidence, type) {
445
- let selectedEvidence = {}
446
-
447
- if (type === "other") {
448
- for (const [key, value] of Object.entries(evidence)) {
449
- if (this.hasKeyPrefix(key, "query") === false &&
450
- this.hasKeyPrefix(key, "header") === false &&
451
- this.hasKeyPrefix(key, "cookie") === false ) {
452
- selectedEvidence[key] = value
453
- }
441
+ getSelectedEvidence (evidence, type) {
442
+ let selectedEvidence = {};
443
+
444
+ if (type === 'other') {
445
+ for (const [key, value] of Object.entries(evidence)) {
446
+ if (this.hasKeyPrefix(key, 'query') === false &&
447
+ this.hasKeyPrefix(key, 'header') === false &&
448
+ this.hasKeyPrefix(key, 'cookie') === false) {
449
+ selectedEvidence[key] = value;
454
450
  }
455
- selectedEvidence = Object.keys(selectedEvidence).sort().reverse().reduce(
456
- (obj, key) => {
457
- obj[key] = selectedEvidence[key];
458
- return obj;
459
- },
460
- {}
461
- );
462
- }
463
- else {
451
+ }
452
+ selectedEvidence = Object.keys(selectedEvidence).sort().reverse().reduce(
453
+ (obj, key) => {
454
+ obj[key] = selectedEvidence[key];
455
+ return obj;
456
+ },
457
+ {}
458
+ );
459
+ } else {
464
460
  for (const [key, value] of Object.entries(evidence)) {
465
461
  if (this.hasKeyPrefix(key, type)) {
466
- selectedEvidence[key] = value
462
+ selectedEvidence[key] = value;
467
463
  }
468
464
  }
469
465
  }
470
- return selectedEvidence
466
+ return selectedEvidence;
471
467
  }
472
-
468
+
473
469
  /**
474
470
  * Check that the key of a KeyValuePair has the given prefix.
471
+ *
475
472
  * @param {string} itemKey Key to check
476
473
  * @param {string} prefix The prefix to check for.
477
474
  * @returns True if the key has the prefix.
478
475
  */
479
- hasKeyPrefix(itemKey, prefix) {
480
- return itemKey.startsWith(prefix + '.')
476
+ hasKeyPrefix (itemKey, prefix) {
477
+ return itemKey.startsWith(prefix + '.');
481
478
  }
482
479
  }
483
480
 
@@ -21,17 +21,17 @@
21
21
  * ********************************************************************* */
22
22
 
23
23
  class CloudRequestError extends Error {
24
- constructor(message, responseHeaders, httpStatusCode){
25
- super(message);
26
- this.name = this.constructor.name
27
- this.errorMessage = message;
28
- this.responseHeaders = responseHeaders;
29
- this.httpStatusCode = httpStatusCode;
30
- // This clips the constructor invocation from the stack trace.
31
- // It's not absolutely essential, but it does make the stack
32
- // trace a little nicer.
33
- Error.captureStackTrace(this, this.constructor);
24
+ constructor (message, responseHeaders, httpStatusCode) {
25
+ super(message);
26
+ this.name = this.constructor.name;
27
+ this.errorMessage = message;
28
+ this.responseHeaders = responseHeaders;
29
+ this.httpStatusCode = httpStatusCode;
30
+ // This clips the constructor invocation from the stack trace.
31
+ // It's not absolutely essential, but it does make the stack
32
+ // trace a little nicer.
33
+ Error.captureStackTrace(this, this.constructor);
34
34
  }
35
35
  }
36
36
 
37
- module.exports = CloudRequestError;
37
+ module.exports = CloudRequestError;
package/errorMessages.js CHANGED
@@ -21,6 +21,6 @@
21
21
  * ********************************************************************* */
22
22
 
23
23
  module.exports = {
24
- // warning message to be shown for conflicted evidences
25
- evidenceConflict: "WARNING: '%s:%s' evidence conflicts with %s"
26
- }
24
+ // warning message to be shown for conflicted evidences
25
+ evidenceConflict: "WARNING: '%s:%s' evidence conflicts with %s"
26
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiftyone.pipeline.cloudrequestengine",
3
- "version": "4.4.7",
3
+ "version": "4.4.9",
4
4
  "description": "Cloud request engine for the 51Degrees Pipeline API",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -13,15 +13,15 @@
13
13
  "devDependencies": {
14
14
  "eslint": "^6.8.0",
15
15
  "eslint-config-standard": "^14.1.1",
16
- "eslint-plugin-import": "^2.20.2",
16
+ "eslint-plugin-import": "^2.27.5",
17
17
  "eslint-plugin-node": "^11.1.0",
18
- "eslint-plugin-promise": "^4.2.1",
19
- "eslint-plugin-standard": "^4.0.1",
20
- "jest": "^27.0.6"
18
+ "eslint-plugin-promise": "^4.3.1",
19
+ "eslint-plugin-standard": "^4.1.0",
20
+ "jest": "^27.5.1"
21
21
  },
22
22
  "dependencies": {
23
- "fiftyone.pipeline.core": "^4.4.7",
24
- "fiftyone.pipeline.engines": "^4.4.7"
23
+ "fiftyone.pipeline.core": "^4.4.8",
24
+ "fiftyone.pipeline.engines": "^4.4.8"
25
25
  },
26
26
  "contributors": [
27
27
  "Filip Hnízdo <filip@octophin.com> (https://octophindigital.com/)",