@wiotp/sdk 0.4.2 → 0.6.2
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/LICENSE +203 -203
- package/README.md +68 -68
- package/dist/BaseClient.js +259 -0
- package/dist/BaseConfig.js +194 -0
- package/dist/api/ApiClient.js +508 -0
- package/dist/api/ApiErrors.js +118 -0
- package/dist/api/DscClient.js +332 -0
- package/dist/api/LecClient.js +48 -0
- package/dist/api/MgmtClient.js +77 -0
- package/dist/api/RegistryClient.js +234 -0
- package/dist/api/RulesClient.js +105 -0
- package/dist/api/StateClient.js +738 -0
- package/dist/application/ApplicationClient.js +436 -0
- package/dist/application/ApplicationConfig.js +233 -0
- package/dist/application/index.js +23 -0
- package/dist/bundled/wiotp-bundle.js +35592 -0
- package/dist/bundled/wiotp-bundle.min.js +47 -0
- package/dist/device/DeviceClient.js +125 -0
- package/dist/device/DeviceConfig.js +216 -0
- package/dist/device/index.js +23 -0
- package/dist/gateway/GatewayClient.js +159 -0
- package/dist/gateway/GatewayConfig.js +52 -0
- package/dist/gateway/index.js +23 -0
- package/dist/index.js +55 -0
- package/dist/util.js +50 -0
- package/package.json +92 -84
- package/src/BaseClient.js +215 -215
- package/src/BaseConfig.js +157 -157
- package/src/api/ApiClient.js +454 -454
- package/src/api/ApiErrors.js +33 -33
- package/src/api/DscClient.js +164 -145
- package/src/api/LecClient.js +32 -32
- package/src/api/MgmtClient.js +57 -57
- package/src/api/RegistryClient.js +194 -194
- package/src/api/RulesClient.js +84 -84
- package/src/api/StateClient.js +650 -650
- package/src/application/ApplicationClient.js +348 -348
- package/src/application/ApplicationConfig.js +191 -191
- package/src/application/index.js +12 -12
- package/src/device/DeviceClient.js +78 -78
- package/src/device/DeviceConfig.js +175 -175
- package/src/device/index.js +14 -14
- package/src/gateway/GatewayClient.js +114 -114
- package/src/gateway/GatewayConfig.js +21 -21
- package/src/gateway/index.js +13 -13
- package/{index.js → src/index.js} +19 -19
- package/src/util.js +38 -38
- package/src/util/IoTFoundation.pem +0 -82
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
9
|
+
|
|
10
|
+
var _bluebird = _interopRequireDefault(require("bluebird"));
|
|
11
|
+
|
|
12
|
+
var _btoa = _interopRequireDefault(require("btoa"));
|
|
13
|
+
|
|
14
|
+
var _formData = _interopRequireDefault(require("form-data"));
|
|
15
|
+
|
|
16
|
+
var _loglevel = _interopRequireDefault(require("loglevel"));
|
|
17
|
+
|
|
18
|
+
var _util = require("../util");
|
|
19
|
+
|
|
20
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
21
|
+
|
|
22
|
+
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
23
|
+
|
|
24
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
25
|
+
|
|
26
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
27
|
+
|
|
28
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
29
|
+
|
|
30
|
+
var ApiClient =
|
|
31
|
+
/*#__PURE__*/
|
|
32
|
+
function () {
|
|
33
|
+
function ApiClient(config) {
|
|
34
|
+
_classCallCheck(this, ApiClient);
|
|
35
|
+
|
|
36
|
+
this.log = _loglevel["default"];
|
|
37
|
+
this.log.setDefaultLevel(config.options.logLevel);
|
|
38
|
+
this.config = config;
|
|
39
|
+
this.useLtpa = this.config.auth && this.config.auth.useLtpa;
|
|
40
|
+
this.log.debug("[ApiClient:constructor] ApiClient initialized for " + this.config.getApiBaseUri());
|
|
41
|
+
} // e.g. [{name: true}, {description: false}] => -name,description
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
_createClass(ApiClient, [{
|
|
45
|
+
key: "parseSortSpec",
|
|
46
|
+
value: function parseSortSpec(sortSpec) {
|
|
47
|
+
return sortSpec ? sortSpec.map(function (s) {
|
|
48
|
+
var e = Object.entries(s)[0];
|
|
49
|
+
return "".concat(e[1] ? '-' : '').concat(e[0]);
|
|
50
|
+
}).join(',') : undefined;
|
|
51
|
+
}
|
|
52
|
+
}, {
|
|
53
|
+
key: "callApi",
|
|
54
|
+
value: function callApi(method, expectedHttpCode, expectJsonContent, paths, body, params) {
|
|
55
|
+
var _this = this;
|
|
56
|
+
|
|
57
|
+
return new _bluebird["default"](function (resolve, reject) {
|
|
58
|
+
var uri = _this.config.getApiBaseUri();
|
|
59
|
+
|
|
60
|
+
if (Array.isArray(paths)) {
|
|
61
|
+
for (var i = 0, l = paths.length; i < l; i++) {
|
|
62
|
+
uri += '/' + paths[i];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
var xhrConfig = {
|
|
67
|
+
url: uri,
|
|
68
|
+
method: method,
|
|
69
|
+
headers: {
|
|
70
|
+
'Content-Type': 'application/json'
|
|
71
|
+
},
|
|
72
|
+
validateStatus: function validateStatus(status) {
|
|
73
|
+
if (Array.isArray(expectedHttpCode)) {
|
|
74
|
+
return expectedHttpCode.includes(status);
|
|
75
|
+
} else {
|
|
76
|
+
return status === expectedHttpCode;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
if (_this.useLtpa) {
|
|
82
|
+
xhrConfig.withCredentials = true;
|
|
83
|
+
} else {
|
|
84
|
+
xhrConfig.headers['Authorization'] = 'Basic ' + (0, _btoa["default"])(_this.config.auth.key + ':' + _this.config.auth.token);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (body) {
|
|
88
|
+
xhrConfig.data = body;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (params) {
|
|
92
|
+
xhrConfig.params = params;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function transformResponse(response) {
|
|
96
|
+
if (expectJsonContent && !(_typeof(response.data) === 'object')) {
|
|
97
|
+
try {
|
|
98
|
+
resolve(JSON.parse(response.data));
|
|
99
|
+
} catch (e) {
|
|
100
|
+
reject(e);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
resolve(response.data);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
_this.log.debug("[ApiClient:transformResponse] " + xhrConfig);
|
|
108
|
+
|
|
109
|
+
(0, _axios["default"])(xhrConfig).then(transformResponse, reject);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}, {
|
|
113
|
+
key: "getOrganizationDetails",
|
|
114
|
+
value: function getOrganizationDetails() {
|
|
115
|
+
this.log.debug("[ApiClient] getOrganizationDetails()");
|
|
116
|
+
return this.callApi('GET', 200, true, null, null);
|
|
117
|
+
}
|
|
118
|
+
}, {
|
|
119
|
+
key: "getServiceStatus",
|
|
120
|
+
value: function getServiceStatus() {
|
|
121
|
+
this.log.debug("[ApiClient] getServiceStatus()");
|
|
122
|
+
return this.callApi('GET', 200, true, ['service-status'], null);
|
|
123
|
+
} //Usage Management
|
|
124
|
+
|
|
125
|
+
}, {
|
|
126
|
+
key: "getActiveDevices",
|
|
127
|
+
value: function getActiveDevices(start, end, detail) {
|
|
128
|
+
this.log.debug("[ApiClient] getActiveDevices(" + start + ", " + end + ")");
|
|
129
|
+
detail = detail | false;
|
|
130
|
+
var params = {
|
|
131
|
+
start: start,
|
|
132
|
+
end: end,
|
|
133
|
+
detail: detail
|
|
134
|
+
};
|
|
135
|
+
return this.callApi('GET', 200, true, ['usage', 'active-devices'], null, params);
|
|
136
|
+
}
|
|
137
|
+
}, {
|
|
138
|
+
key: "getHistoricalDataUsage",
|
|
139
|
+
value: function getHistoricalDataUsage(start, end, detail) {
|
|
140
|
+
this.log.debug("[ApiClient] getHistoricalDataUsage(" + start + ", " + end + ")");
|
|
141
|
+
detail = detail | false;
|
|
142
|
+
var params = {
|
|
143
|
+
start: start,
|
|
144
|
+
end: end,
|
|
145
|
+
detail: detail
|
|
146
|
+
};
|
|
147
|
+
return this.callApi('GET', 200, true, ['usage', 'historical-data'], null, params);
|
|
148
|
+
}
|
|
149
|
+
}, {
|
|
150
|
+
key: "getDataUsage",
|
|
151
|
+
value: function getDataUsage(start, end, detail) {
|
|
152
|
+
this.log.debug("[ApiClient] getDataUsage(" + start + ", " + end + ")");
|
|
153
|
+
detail = detail | false;
|
|
154
|
+
var params = {
|
|
155
|
+
start: start,
|
|
156
|
+
end: end,
|
|
157
|
+
detail: detail
|
|
158
|
+
};
|
|
159
|
+
return this.callApi('GET', 200, true, ['usage', 'data-traffic'], null, params);
|
|
160
|
+
} //client connectivity status
|
|
161
|
+
|
|
162
|
+
}, {
|
|
163
|
+
key: "getConnectionStates",
|
|
164
|
+
value: function getConnectionStates() {
|
|
165
|
+
this.log.debug("[ApiClient] getConnectionStates() - client connectivity");
|
|
166
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates"], null);
|
|
167
|
+
}
|
|
168
|
+
}, {
|
|
169
|
+
key: "getConnectionState",
|
|
170
|
+
value: function getConnectionState(id) {
|
|
171
|
+
this.log.debug("[ApiClient] getConnectionState() - client connectivity");
|
|
172
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates/" + id], null);
|
|
173
|
+
}
|
|
174
|
+
}, {
|
|
175
|
+
key: "getConnectedClientsConnectionStates",
|
|
176
|
+
value: function getConnectedClientsConnectionStates() {
|
|
177
|
+
this.log.debug("[ApiClient] getConnectedClientsConnectionStates() - client connectivity");
|
|
178
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates?connectionStatus=connected"], null);
|
|
179
|
+
}
|
|
180
|
+
}, {
|
|
181
|
+
key: "getRecentConnectionStates",
|
|
182
|
+
value: function getRecentConnectionStates(date) {
|
|
183
|
+
this.log.debug("[ApiClient] getRecentConnectionStates() - client connectivity");
|
|
184
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates?connectedAfter=" + date], null);
|
|
185
|
+
}
|
|
186
|
+
}, {
|
|
187
|
+
key: "getCustomConnectionState",
|
|
188
|
+
value: function getCustomConnectionState(query) {
|
|
189
|
+
this.log.debug("[ApiClient] getCustomConnectionStates() - client connectivity");
|
|
190
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates" + query], null);
|
|
191
|
+
} //bulk apis
|
|
192
|
+
|
|
193
|
+
}, {
|
|
194
|
+
key: "getAllDevices",
|
|
195
|
+
value: function getAllDevices(params) {
|
|
196
|
+
this.log.debug("[ApiClient] getAllDevices() - BULK");
|
|
197
|
+
return this.callApi('GET', 200, true, ["bulk", "devices"], null, params);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Gateway Access Control (Beta)
|
|
201
|
+
* The methods in this section follow the documentation listed under the link:
|
|
202
|
+
* https://console.ng.bluemix.net/docs/services/IoT/gateways/gateway-access-control.html#gateway-access-control-beta-
|
|
203
|
+
* Involves the following sections from the above mentioned link:
|
|
204
|
+
* Assigning a role to a gateway
|
|
205
|
+
* Adding devices to and removing devices from a resource group
|
|
206
|
+
* Finding a resource group
|
|
207
|
+
* Querying a resource group
|
|
208
|
+
* Creating and deleting a resource group
|
|
209
|
+
* Updating group properties
|
|
210
|
+
* Retrieving and updating device properties
|
|
211
|
+
*
|
|
212
|
+
*/
|
|
213
|
+
//getGatewayGroup(gatewayId)
|
|
214
|
+
//updateDeviceRoles(deviceId, roles[])
|
|
215
|
+
//getAllDevicesInGropu(groupId)
|
|
216
|
+
//addDevicesToGroup(groupId, deviceList[])
|
|
217
|
+
//removeDevicesFromGroup(groupId, deviceList[])
|
|
218
|
+
|
|
219
|
+
}, {
|
|
220
|
+
key: "getGroupIdsForDevice",
|
|
221
|
+
value: function getGroupIdsForDevice(deviceId) {
|
|
222
|
+
this.log.debug("[ApiClient] getGroupIdsForDevice(" + deviceId + ")");
|
|
223
|
+
return this.callApi('GET', 200, true, ['authorization', 'devices', deviceId], null);
|
|
224
|
+
}
|
|
225
|
+
}, {
|
|
226
|
+
key: "updateDeviceRoles",
|
|
227
|
+
value: function updateDeviceRoles(deviceId, roles) {
|
|
228
|
+
this.log.debug("[ApiClient] updateDeviceRoles(" + deviceId + "," + roles + ")");
|
|
229
|
+
return this.callApi('PUT', 200, false, ['authorization', 'devices', deviceId, 'roles'], roles);
|
|
230
|
+
}
|
|
231
|
+
}, {
|
|
232
|
+
key: "getAllDevicesInGroup",
|
|
233
|
+
value: function getAllDevicesInGroup(groupId) {
|
|
234
|
+
this.log.debug("[ApiClient] getAllDevicesInGropu(" + groupId + ")");
|
|
235
|
+
return this.callApi('GET', 200, true, ['bulk', 'devices', groupId], null);
|
|
236
|
+
}
|
|
237
|
+
}, {
|
|
238
|
+
key: "addDevicesToGroup",
|
|
239
|
+
value: function addDevicesToGroup(groupId, deviceList) {
|
|
240
|
+
this.log.debug("[ApiClient] addDevicesToGroup(" + groupId + "," + deviceList + ")");
|
|
241
|
+
return this.callApi('PUT', 200, false, ['bulk', 'devices', groupId, "add"], deviceList);
|
|
242
|
+
}
|
|
243
|
+
}, {
|
|
244
|
+
key: "removeDevicesFromGroup",
|
|
245
|
+
value: function removeDevicesFromGroup(groupId, deviceList) {
|
|
246
|
+
this.log.debug("[ApiClient] removeDevicesFromGroup(" + groupId + "," + deviceList + ")");
|
|
247
|
+
return this.callApi('PUT', 200, false, ['bulk', 'devices', groupId, "remove"], deviceList);
|
|
248
|
+
} // https://console.ng.bluemix.net/docs/services/IoT/gateways/gateway-access-control.html
|
|
249
|
+
// Finding a Resource Group
|
|
250
|
+
// getGatewayGroups()
|
|
251
|
+
// Querying a resource group
|
|
252
|
+
// getUniqueDevicesInGroup(groupId)
|
|
253
|
+
// getUniqueGatewayGroup(groupId)
|
|
254
|
+
// Creating and deleting a resource group
|
|
255
|
+
// createGatewayGroup(groupName)
|
|
256
|
+
// deleteGatewayGroup(groupId)
|
|
257
|
+
// Retrieving and updating device properties
|
|
258
|
+
// getGatewayGroupProperties()
|
|
259
|
+
// getDeviceRoles(deviceId)
|
|
260
|
+
// updateGatewayProperties(gatewayId,control_props)
|
|
261
|
+
// updateDeviceControlProperties(deviceId, withroles)
|
|
262
|
+
// Finding a Resource Group
|
|
263
|
+
|
|
264
|
+
}, {
|
|
265
|
+
key: "getAllGroups",
|
|
266
|
+
value: function getAllGroups() {
|
|
267
|
+
this.log.debug("[ApiClient] getAllGroups()");
|
|
268
|
+
return this.callApi('GET', 200, true, ['groups'], null);
|
|
269
|
+
} // Querying a resource group
|
|
270
|
+
// Get unique identifiers of the members of the resource group
|
|
271
|
+
|
|
272
|
+
}, {
|
|
273
|
+
key: "getAllDeviceIdsInGroup",
|
|
274
|
+
value: function getAllDeviceIdsInGroup(groupId) {
|
|
275
|
+
this.log.debug("[ApiClient] getAllDeviceIdsInGroup(" + groupId + ")");
|
|
276
|
+
return this.callApi('GET', 200, true, ['bulk', 'devices', groupId, "ids"], null);
|
|
277
|
+
} // properties of the resource group
|
|
278
|
+
|
|
279
|
+
}, {
|
|
280
|
+
key: "getGroup",
|
|
281
|
+
value: function getGroup(groupId) {
|
|
282
|
+
this.log.debug("[ApiClient] getGroup(" + groupId + ")");
|
|
283
|
+
return this.callApi('GET', 200, true, ['groups', groupId], null);
|
|
284
|
+
} // Creating and deleting a resource group
|
|
285
|
+
// Create a Resource Group
|
|
286
|
+
|
|
287
|
+
}, {
|
|
288
|
+
key: "createGroup",
|
|
289
|
+
value: function createGroup(groupInfo) {
|
|
290
|
+
this.log.debug("[ApiClient] createGroup()");
|
|
291
|
+
return this.callApi('POST', 201, true, ['groups'], groupInfo);
|
|
292
|
+
} // Delete a Resource Group
|
|
293
|
+
|
|
294
|
+
}, {
|
|
295
|
+
key: "deleteGroup",
|
|
296
|
+
value: function deleteGroup(groupId) {
|
|
297
|
+
this.log.debug("[ApiClient] deleteGroup(" + groupId + ")");
|
|
298
|
+
return this.callApi('DELETE', 200, false, ['groups', groupId], null);
|
|
299
|
+
} // Retrieving and updating device properties
|
|
300
|
+
// Get the ID of the devices group of a gateway
|
|
301
|
+
|
|
302
|
+
}, {
|
|
303
|
+
key: "getAllDeviceAccessControlProperties",
|
|
304
|
+
value: function getAllDeviceAccessControlProperties() {
|
|
305
|
+
this.log.debug("[ApiClient] getAllDeviceAccessControlProperties()");
|
|
306
|
+
return this.callApi('GET', 200, true, ['authorization', 'devices'], null);
|
|
307
|
+
} // Get standard role of a gateway
|
|
308
|
+
|
|
309
|
+
}, {
|
|
310
|
+
key: "getDeviceAccessControlProperties",
|
|
311
|
+
value: function getDeviceAccessControlProperties(deviceId) {
|
|
312
|
+
this.log.debug("[ApiClient] getDeviceAccessControlProperties(" + deviceId + ")");
|
|
313
|
+
return this.callApi('GET', 200, true, ['authorization', 'devices', deviceId, 'roles'], null);
|
|
314
|
+
} // Update device properties without affecting the access control properties
|
|
315
|
+
|
|
316
|
+
}, {
|
|
317
|
+
key: "updateDeviceAccessControlProperties",
|
|
318
|
+
value: function updateDeviceAccessControlProperties(deviceId, deviceProps) {
|
|
319
|
+
this.log.debug("[ApiClient] updateDeviceAccessControlProperties(" + deviceId + ")");
|
|
320
|
+
return this.callApi('PUT', 200, true, ['authorization', 'devices', deviceId], deviceProps);
|
|
321
|
+
} // Assign a standard role to a gateway
|
|
322
|
+
|
|
323
|
+
}, {
|
|
324
|
+
key: "updateDeviceAccessControlPropertiesWithRoles",
|
|
325
|
+
value: function updateDeviceAccessControlPropertiesWithRoles(deviceId, devicePropsWithRoles) {
|
|
326
|
+
this.log.debug("[ApiClient] updateDeviceAccessControlPropertiesWithRoles(" + deviceId + "," + devicePropsWithRoles + ")");
|
|
327
|
+
return this.callApi('PUT', 200, true, ['authorization', 'devices', deviceId, 'withroles'], devicePropsWithRoles);
|
|
328
|
+
} // Duplicating updateDeviceRoles(deviceId, roles) for Gateway Roles
|
|
329
|
+
|
|
330
|
+
}, {
|
|
331
|
+
key: "updateGatewayRoles",
|
|
332
|
+
value: function updateGatewayRoles(gatewayId, roles) {
|
|
333
|
+
this.log.debug("[ApiClient] updateGatewayRoles(" + gatewayId + "," + roles + ")");
|
|
334
|
+
return this.callApi('PUT', 200, false, ['authorization', 'devices', gatewayId, 'roles'], roles);
|
|
335
|
+
} // Extending getAllGroups() to fetch individual Groups
|
|
336
|
+
|
|
337
|
+
}, {
|
|
338
|
+
key: "getGroups",
|
|
339
|
+
value: function getGroups(groupId) {
|
|
340
|
+
this.log.debug("[ApiClient] getGroups(" + groupId + ")");
|
|
341
|
+
return this.callApi('GET', 200, true, ['groups', groupId], null);
|
|
342
|
+
}
|
|
343
|
+
}, {
|
|
344
|
+
key: "callFormDataApi",
|
|
345
|
+
value: function callFormDataApi(method, expectedHttpCode, expectJsonContent, paths, body, params) {
|
|
346
|
+
var _this2 = this;
|
|
347
|
+
|
|
348
|
+
return new _bluebird["default"](function (resolve, reject) {
|
|
349
|
+
var uri = _this2.config.getApiBaseUri();
|
|
350
|
+
|
|
351
|
+
if (Array.isArray(paths)) {
|
|
352
|
+
for (var i = 0, l = paths.length; i < l; i++) {
|
|
353
|
+
uri += '/' + paths[i];
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
var xhrConfig = {
|
|
358
|
+
url: uri,
|
|
359
|
+
method: method,
|
|
360
|
+
headers: {
|
|
361
|
+
'Content-Type': 'multipart/form-data'
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
if (_this2.useLtpa) {
|
|
366
|
+
xhrConfig.withCredentials = true;
|
|
367
|
+
} else {
|
|
368
|
+
xhrConfig.headers['Authorization'] = 'Basic ' + (0, _btoa["default"])(_this2.apiKey + ':' + _this2.apiToken);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (body) {
|
|
372
|
+
xhrConfig.data = body;
|
|
373
|
+
|
|
374
|
+
if ((0, _util.isBrowser)()) {
|
|
375
|
+
xhrConfig.transformRequest = [function (data) {
|
|
376
|
+
var formData = new _formData["default"]();
|
|
377
|
+
|
|
378
|
+
if (xhrConfig.method == "POST") {
|
|
379
|
+
if (data.schemaFile) {
|
|
380
|
+
var blob = new Blob([data.schemaFile], {
|
|
381
|
+
type: "application/json"
|
|
382
|
+
});
|
|
383
|
+
formData.append('schemaFile', blob);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (data.name) {
|
|
387
|
+
formData.append('name', data.name);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (data.schemaType) {
|
|
391
|
+
formData.append('schemaType', 'json-schema');
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (data.description) {
|
|
395
|
+
formData.append('description', data.description);
|
|
396
|
+
}
|
|
397
|
+
} else if (xhrConfig.method == "PUT") {
|
|
398
|
+
if (data.schemaFile) {
|
|
399
|
+
var blob = new Blob([data.schemaFile], {
|
|
400
|
+
type: "application/json",
|
|
401
|
+
name: data.name
|
|
402
|
+
});
|
|
403
|
+
formData.append('schemaFile', blob);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return formData;
|
|
408
|
+
}];
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (params) {
|
|
413
|
+
xhrConfig.params = params;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function transformResponse(response) {
|
|
417
|
+
if (response.status === expectedHttpCode) {
|
|
418
|
+
if (expectJsonContent && !(_typeof(response.data) === 'object')) {
|
|
419
|
+
try {
|
|
420
|
+
resolve(JSON.parse(response.data));
|
|
421
|
+
} catch (e) {
|
|
422
|
+
reject(e);
|
|
423
|
+
}
|
|
424
|
+
} else {
|
|
425
|
+
resolve(response.data);
|
|
426
|
+
}
|
|
427
|
+
} else {
|
|
428
|
+
reject(new Error(method + " " + uri + ": Expected HTTP " + expectedHttpCode + " from server but got HTTP " + response.status + ". Error Body: " + JSON.stringify(response.data)));
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
_this2.log.debug("[ApiClient:transformResponse] " + xhrConfig);
|
|
433
|
+
|
|
434
|
+
if ((0, _util.isBrowser)()) {
|
|
435
|
+
(0, _axios["default"])(xhrConfig).then(transformResponse, reject);
|
|
436
|
+
} else {
|
|
437
|
+
var formData = null;
|
|
438
|
+
var config = {
|
|
439
|
+
url: uri,
|
|
440
|
+
method: method,
|
|
441
|
+
headers: {
|
|
442
|
+
'Content-Type': 'multipart/form-data'
|
|
443
|
+
},
|
|
444
|
+
auth: {
|
|
445
|
+
user: _this2.apiKey,
|
|
446
|
+
pass: _this2.apiToken
|
|
447
|
+
},
|
|
448
|
+
formData: {},
|
|
449
|
+
rejectUnauthorized: false
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
if (xhrConfig.method == "POST") {
|
|
453
|
+
formData = {
|
|
454
|
+
'schemaFile': {
|
|
455
|
+
'value': body.schemaFile,
|
|
456
|
+
'options': {
|
|
457
|
+
'contentType': 'application/json',
|
|
458
|
+
'filename': body.name
|
|
459
|
+
}
|
|
460
|
+
},
|
|
461
|
+
'schemaType': 'json-schema',
|
|
462
|
+
'name': body.name
|
|
463
|
+
};
|
|
464
|
+
config.formData = formData;
|
|
465
|
+
} else if (xhrConfig.method == "PUT") {
|
|
466
|
+
formData = {
|
|
467
|
+
'schemaFile': {
|
|
468
|
+
'value': body.schemaFile,
|
|
469
|
+
'options': {
|
|
470
|
+
'contentType': 'application/json',
|
|
471
|
+
'filename': body.name
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
config.formData = formData;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
request(config, function optionalCallback(err, response, body) {
|
|
479
|
+
if (response.statusCode === expectedHttpCode) {
|
|
480
|
+
if (expectJsonContent && !(_typeof(response.data) === 'object')) {
|
|
481
|
+
try {
|
|
482
|
+
resolve(JSON.parse(body));
|
|
483
|
+
} catch (e) {
|
|
484
|
+
reject(e);
|
|
485
|
+
}
|
|
486
|
+
} else {
|
|
487
|
+
resolve(body);
|
|
488
|
+
}
|
|
489
|
+
} else {
|
|
490
|
+
reject(new Error(method + " " + uri + ": Expected HTTP " + expectedHttpCode + " from server but got HTTP " + response.statusCode + ". Error Body: " + err));
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
}, {
|
|
497
|
+
key: "invalidOperation",
|
|
498
|
+
value: function invalidOperation(message) {
|
|
499
|
+
return new _bluebird["default"](function (resolve, reject) {
|
|
500
|
+
resolve(message);
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}]);
|
|
504
|
+
|
|
505
|
+
return ApiClient;
|
|
506
|
+
}();
|
|
507
|
+
|
|
508
|
+
exports["default"] = ApiClient;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = exports.handleError = exports.ServiceNotFound = exports.DestinationAlreadyExists = exports.InvalidServiceCredentials = exports.WiotpError = void 0;
|
|
7
|
+
|
|
8
|
+
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
9
|
+
|
|
10
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
11
|
+
|
|
12
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
|
13
|
+
|
|
14
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
15
|
+
|
|
16
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
17
|
+
|
|
18
|
+
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
|
19
|
+
|
|
20
|
+
function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
|
21
|
+
|
|
22
|
+
function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
23
|
+
|
|
24
|
+
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
|
25
|
+
|
|
26
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
27
|
+
|
|
28
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
29
|
+
|
|
30
|
+
var WiotpError =
|
|
31
|
+
/*#__PURE__*/
|
|
32
|
+
function (_Error) {
|
|
33
|
+
_inherits(WiotpError, _Error);
|
|
34
|
+
|
|
35
|
+
function WiotpError(message, cause) {
|
|
36
|
+
var _this;
|
|
37
|
+
|
|
38
|
+
_classCallCheck(this, WiotpError);
|
|
39
|
+
|
|
40
|
+
_this = _possibleConstructorReturn(this, _getPrototypeOf(WiotpError).call(this, message));
|
|
41
|
+
_this.cause = cause;
|
|
42
|
+
_this.name = _this.constructor.name;
|
|
43
|
+
return _this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return WiotpError;
|
|
47
|
+
}(_wrapNativeSuper(Error));
|
|
48
|
+
|
|
49
|
+
exports.WiotpError = WiotpError;
|
|
50
|
+
|
|
51
|
+
var InvalidServiceCredentials =
|
|
52
|
+
/*#__PURE__*/
|
|
53
|
+
function (_WiotpError) {
|
|
54
|
+
_inherits(InvalidServiceCredentials, _WiotpError);
|
|
55
|
+
|
|
56
|
+
function InvalidServiceCredentials() {
|
|
57
|
+
_classCallCheck(this, InvalidServiceCredentials);
|
|
58
|
+
|
|
59
|
+
return _possibleConstructorReturn(this, _getPrototypeOf(InvalidServiceCredentials).apply(this, arguments));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return InvalidServiceCredentials;
|
|
63
|
+
}(WiotpError);
|
|
64
|
+
|
|
65
|
+
exports.InvalidServiceCredentials = InvalidServiceCredentials;
|
|
66
|
+
|
|
67
|
+
var DestinationAlreadyExists =
|
|
68
|
+
/*#__PURE__*/
|
|
69
|
+
function (_WiotpError2) {
|
|
70
|
+
_inherits(DestinationAlreadyExists, _WiotpError2);
|
|
71
|
+
|
|
72
|
+
function DestinationAlreadyExists() {
|
|
73
|
+
_classCallCheck(this, DestinationAlreadyExists);
|
|
74
|
+
|
|
75
|
+
return _possibleConstructorReturn(this, _getPrototypeOf(DestinationAlreadyExists).apply(this, arguments));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return DestinationAlreadyExists;
|
|
79
|
+
}(WiotpError);
|
|
80
|
+
|
|
81
|
+
exports.DestinationAlreadyExists = DestinationAlreadyExists;
|
|
82
|
+
|
|
83
|
+
var ServiceNotFound =
|
|
84
|
+
/*#__PURE__*/
|
|
85
|
+
function (_WiotpError3) {
|
|
86
|
+
_inherits(ServiceNotFound, _WiotpError3);
|
|
87
|
+
|
|
88
|
+
function ServiceNotFound() {
|
|
89
|
+
_classCallCheck(this, ServiceNotFound);
|
|
90
|
+
|
|
91
|
+
return _possibleConstructorReturn(this, _getPrototypeOf(ServiceNotFound).apply(this, arguments));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return ServiceNotFound;
|
|
95
|
+
}(WiotpError);
|
|
96
|
+
|
|
97
|
+
exports.ServiceNotFound = ServiceNotFound;
|
|
98
|
+
|
|
99
|
+
var handleError = function handleError(err, errorMappings) {
|
|
100
|
+
if (err && err.response && err.response.data && err.response.data.exception && err.response.data.exception.id) {
|
|
101
|
+
if (errorMappings && errorMappings[err.response.data.exception.id]) {
|
|
102
|
+
throw new errorMappings[err.response.data.exception.id](err.response.data.message, err);
|
|
103
|
+
} else {
|
|
104
|
+
throw new WiotpError(err.response.data.message, err);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
exports.handleError = handleError;
|
|
112
|
+
var _default = {
|
|
113
|
+
WiotpError: WiotpError,
|
|
114
|
+
InvalidServiceCredentials: InvalidServiceCredentials,
|
|
115
|
+
DestinationAlreadyExists: DestinationAlreadyExists,
|
|
116
|
+
ServiceNotFound: ServiceNotFound
|
|
117
|
+
};
|
|
118
|
+
exports["default"] = _default;
|