fiftyone.pipeline.cloudrequestengine 4.3.3 → 4.3.7
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 +17 -16
- package/cloudRequestError.js +1 -0
- package/package.json +4 -4
- package/requestClient.js +50 -25
- package/tests/cloudEngine.test.js +4 -7
- package/tests/cloudRequestEngine.test.js +10 -13
- package/tests/cloudResponse.test.js +1 -1
package/cloudEngine.js
CHANGED
|
@@ -99,29 +99,30 @@ class CloudEngine extends Engine {
|
|
|
99
99
|
const engine = this;
|
|
100
100
|
|
|
101
101
|
return engine.ready().then(function () {
|
|
102
|
+
|
|
102
103
|
let cloudData = flowData.get('cloud').get('cloud');
|
|
103
104
|
|
|
104
105
|
cloudData = JSON.parse(cloudData);
|
|
106
|
+
const result = {};
|
|
105
107
|
|
|
106
|
-
if (cloudData
|
|
107
|
-
flowData.pipeline.log('
|
|
108
|
-
cloudData
|
|
108
|
+
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'] : "" +
|
|
109
111
|
"\n" + "This may be because the provided API key is not authorised for " + engine.dataKey + " queries.");
|
|
110
|
-
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
111
114
|
|
|
112
115
|
// Loop over cloudData.device properties to check if they have a value
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
});
|
|
116
|
+
Object.entries(cloudData[engine.dataKey]).forEach(function ([key, value]) {
|
|
117
|
+
result[key] = new AspectPropertyValue();
|
|
118
|
+
|
|
119
|
+
if (cloudData[engine.dataKey][key + 'nullreason']) {
|
|
120
|
+
result[key].noValueMessage = cloudData[engine.dataKey][key + 'nullreason'];
|
|
121
|
+
} else {
|
|
122
|
+
result[key].value = value;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
125
126
|
|
|
126
127
|
const data = new AspectDataDictionary(
|
|
127
128
|
{
|
package/cloudRequestError.js
CHANGED
|
@@ -24,6 +24,7 @@ class CloudRequestError extends Error {
|
|
|
24
24
|
constructor(message, responseHeaders, httpStatusCode){
|
|
25
25
|
super(message);
|
|
26
26
|
this.name = this.constructor.name
|
|
27
|
+
this.errorMessage = message;
|
|
27
28
|
this.responseHeaders = responseHeaders;
|
|
28
29
|
this.httpStatusCode = httpStatusCode;
|
|
29
30
|
// This clips the constructor invocation from the stack trace.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fiftyone.pipeline.cloudrequestengine",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.7",
|
|
4
4
|
"description": "Cloud request engine for the 51Degrees Pipeline API",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
"eslint-plugin-node": "^11.1.0",
|
|
18
18
|
"eslint-plugin-promise": "^4.2.1",
|
|
19
19
|
"eslint-plugin-standard": "^4.0.1",
|
|
20
|
-
"jest": "^
|
|
20
|
+
"jest": "^27.0.6"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"fiftyone.pipeline.core": "^4.3.
|
|
24
|
-
"fiftyone.pipeline.engines": "^4.3.
|
|
23
|
+
"fiftyone.pipeline.core": "^4.3.7",
|
|
24
|
+
"fiftyone.pipeline.engines": "^4.3.7"
|
|
25
25
|
},
|
|
26
26
|
"contributors": [
|
|
27
27
|
"Filip Hnízdo <filip@octophin.com> (https://octophindigital.com/)",
|
package/requestClient.js
CHANGED
|
@@ -36,40 +36,65 @@ class RequestClient {
|
|
|
36
36
|
* content = The content of the response or an error message
|
|
37
37
|
*/
|
|
38
38
|
post(url, data, origin) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
let httpModule;
|
|
40
|
+
|
|
41
|
+
if (url.indexOf('https') !== -1) {
|
|
42
|
+
httpModule = require('https');
|
|
43
|
+
} else {
|
|
44
|
+
httpModule = require('http');
|
|
45
|
+
}
|
|
42
46
|
|
|
47
|
+
if (data) {
|
|
48
|
+
var requestData = "";
|
|
49
|
+
var keys = Object.keys(data);
|
|
50
|
+
for (var i = 0; i < keys.length; i++) {
|
|
51
|
+
requestData += encodeURIComponent(keys[i]) + "=" + encodeURIComponent(data[keys[i]]);
|
|
52
|
+
if (i < (keys.length-1)) requestData += "&";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return new Promise(function (resolve, reject) {
|
|
43
57
|
var httpOptions = {
|
|
44
|
-
|
|
58
|
+
method: 'POST',
|
|
45
59
|
headers: {
|
|
46
|
-
|
|
47
|
-
|
|
60
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
61
|
+
}
|
|
48
62
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
63
|
+
if (origin) {
|
|
64
|
+
httpOptions.headers['Origin'] = origin
|
|
65
|
+
}
|
|
66
|
+
if (requestData) {
|
|
67
|
+
httpOptions.headers['Content-Length'] = requestData.length
|
|
52
68
|
}
|
|
53
69
|
|
|
54
|
-
request
|
|
70
|
+
const req = httpModule.request(url, httpOptions, function (resp) {
|
|
71
|
+
let data = '';
|
|
55
72
|
let result = {
|
|
56
|
-
headers:
|
|
57
|
-
statusCode:
|
|
58
|
-
content: body
|
|
73
|
+
headers: resp ? resp.headers : undefined,
|
|
74
|
+
statusCode: resp ? resp.statusCode : undefined
|
|
59
75
|
}
|
|
76
|
+
|
|
77
|
+
resp.on('data', (chunk) => {
|
|
78
|
+
data += chunk;
|
|
79
|
+
});
|
|
60
80
|
|
|
61
|
-
|
|
62
|
-
result.content =
|
|
63
|
-
reject
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
resolve(body);
|
|
71
|
-
}
|
|
81
|
+
resp.on('end', () => {
|
|
82
|
+
result.content = data;
|
|
83
|
+
// If response from cloud is not 2** / 3**, reject with error
|
|
84
|
+
if (resp.statusCode > 399) {
|
|
85
|
+
reject(result);
|
|
86
|
+
} else {
|
|
87
|
+
resolve(data);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
72
90
|
});
|
|
91
|
+
|
|
92
|
+
req.on('error', (err) => {
|
|
93
|
+
reject({ content: err.message });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
req.write(requestData)
|
|
97
|
+
req.end();
|
|
73
98
|
});
|
|
74
99
|
}
|
|
75
100
|
|
|
@@ -117,7 +142,7 @@ class RequestClient {
|
|
|
117
142
|
|
|
118
143
|
resp.on('end', () => {
|
|
119
144
|
result.content = data;
|
|
120
|
-
// If response from cloud is not 2**, reject with error
|
|
145
|
+
// If response from cloud is not 2** / 3**, reject with error
|
|
121
146
|
if (resp.statusCode > 399) {
|
|
122
147
|
reject(result);
|
|
123
148
|
} else {
|
|
@@ -59,15 +59,12 @@ const flowData = pipeline.createFlowData();
|
|
|
59
59
|
// Check that if an invalid resource key is used, a correct error
|
|
60
60
|
// message will be returned and stored in flowData.errors
|
|
61
61
|
test('missing property service - invalid resource key', done => {
|
|
62
|
+
pipeline.suppressProcessExceptions = true;
|
|
62
63
|
flowData.process().then(function () {
|
|
63
64
|
var errorFound = false;
|
|
64
|
-
flowData.errors.CloudEngineBase
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
(error.message.includes(testResourceKey) &&
|
|
68
|
-
error.message.includes('not a valid resource key'));
|
|
69
|
-
});
|
|
70
|
-
});
|
|
65
|
+
error = flowData.errors.CloudEngineBase[0];
|
|
66
|
+
errorFound = error.message.includes(testResourceKey) &&
|
|
67
|
+
error.message.includes('is not a valid Resource Key');
|
|
71
68
|
expect(errorFound).toBe(true);
|
|
72
69
|
|
|
73
70
|
done();
|
|
@@ -81,7 +81,6 @@ test('custom end point - default value', done => {
|
|
|
81
81
|
// Check that values from the http response are populated in the
|
|
82
82
|
// error object.
|
|
83
83
|
test('HTTP data set in error', done => {
|
|
84
|
-
let errorThrown = false;
|
|
85
84
|
|
|
86
85
|
let engine = new CloudRequestEngine({
|
|
87
86
|
resourceKey: testResourceKey
|
|
@@ -90,24 +89,22 @@ test('HTTP data set in error', done => {
|
|
|
90
89
|
const builder = new PipelineBuilder();
|
|
91
90
|
const pipeline = builder.add(engine).build();
|
|
92
91
|
const flowData = pipeline.createFlowData();
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
let expectedError = '\'' + testResourceKey + '\' is not a valid Resource Key.';
|
|
93
|
+
|
|
94
|
+
// When an error occurs, check that the message is logged.
|
|
95
95
|
pipeline.on('error', (e) => {
|
|
96
|
-
expect(e).toBeDefined();
|
|
97
|
-
|
|
98
|
-
let expectedError = '\'' + testResourceKey + '\' is not a valid resource key.';
|
|
99
|
-
expect(e.message[0].message).toEqual(expectedError);
|
|
100
|
-
expect(e.message[0].httpStatusCode).toEqual(400);
|
|
101
|
-
expect(e.message[0].responseHeaders.etag).toBeDefined();
|
|
102
|
-
errorThrown = true;
|
|
96
|
+
expect(e).toBeDefined();
|
|
97
|
+
done();
|
|
103
98
|
});
|
|
104
99
|
|
|
105
|
-
// Make sure that the expected error was thrown.
|
|
106
100
|
flowData.process().then(function () {
|
|
107
|
-
expect(errorThrown).toBe(true);
|
|
108
101
|
done();
|
|
102
|
+
}).catch((e) => {
|
|
103
|
+
// When an error occurs, check that the expected values are populated.
|
|
104
|
+
expect(e[0].errorMessage.indexOf(expectedError) !== -1).toBe(true);
|
|
105
|
+
expect(e[0].httpStatusCode).toEqual(400);
|
|
106
|
+
expect(e[0].responseHeaders.etag).toBeDefined();
|
|
109
107
|
});
|
|
110
|
-
|
|
111
108
|
});
|
|
112
109
|
|
|
113
110
|
/**
|
|
@@ -56,7 +56,7 @@ test("process", done => {
|
|
|
56
56
|
|
|
57
57
|
var data = pipeline.createFlowData();
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
data.process().then((processedData) => {
|
|
60
60
|
var result = processedData.getFromElement(engine)["cloud"];
|
|
61
61
|
|
|
62
62
|
expect(result).toBe(JSON.stringify(jsonResponse));
|