@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
package/src/api/ApiClient.js
CHANGED
|
@@ -1,454 +1,454 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*****************************************************************************
|
|
3
|
-
Copyright (c) 2014, 2019 IBM Corporation and other Contributors.
|
|
4
|
-
All rights reserved. This program and the accompanying materials
|
|
5
|
-
are made available under the terms of the Eclipse Public License v1.0
|
|
6
|
-
which accompanies this distribution, and is available at
|
|
7
|
-
http://www.eclipse.org/legal/epl-v10.html
|
|
8
|
-
*****************************************************************************
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
import xhr from 'axios';
|
|
12
|
-
import Promise from 'bluebird';
|
|
13
|
-
import btoa from 'btoa';
|
|
14
|
-
import FormData from 'form-data';
|
|
15
|
-
import log from 'loglevel';
|
|
16
|
-
|
|
17
|
-
import { isBrowser } from '../util';
|
|
18
|
-
|
|
19
|
-
export default class ApiClient {
|
|
20
|
-
constructor(config) {
|
|
21
|
-
this.log = log;
|
|
22
|
-
this.log.setDefaultLevel(config.options.logLevel);
|
|
23
|
-
|
|
24
|
-
this.config = config;
|
|
25
|
-
this.useLtpa = this.config.auth && this.config.auth.useLtpa;
|
|
26
|
-
|
|
27
|
-
this.log.debug("[ApiClient:constructor] ApiClient initialized for " + this.config.getApiBaseUri());
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// e.g. [{name: true}, {description: false}] => -name,description
|
|
31
|
-
parseSortSpec(sortSpec) {
|
|
32
|
-
return sortSpec
|
|
33
|
-
? sortSpec.map(s=>{
|
|
34
|
-
const e = Object.entries(s)[0];
|
|
35
|
-
return `${e[1] ? '-' : ''}${e[0]}`
|
|
36
|
-
}).join(',')
|
|
37
|
-
: undefined;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
callApi(method, expectedHttpCode, expectJsonContent, paths, body, params) {
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
42
|
-
let uri = this.config.getApiBaseUri();
|
|
43
|
-
|
|
44
|
-
if (Array.isArray(paths)) {
|
|
45
|
-
for (var i = 0, l = paths.length; i < l; i++) {
|
|
46
|
-
uri += '/' + paths[i];
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
let xhrConfig = {
|
|
51
|
-
url: uri,
|
|
52
|
-
method: method,
|
|
53
|
-
headers: {
|
|
54
|
-
'Content-Type': 'application/json'
|
|
55
|
-
},
|
|
56
|
-
validateStatus: (status) => {
|
|
57
|
-
if(Array.isArray(expectedHttpCode)) {
|
|
58
|
-
return expectedHttpCode.includes(status);
|
|
59
|
-
} else {
|
|
60
|
-
return status === expectedHttpCode;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
if (this.useLtpa) {
|
|
66
|
-
xhrConfig.withCredentials = true;
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
xhrConfig.headers['Authorization'] = 'Basic ' + btoa(this.config.auth.key + ':' + this.config.auth.token);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (body) {
|
|
73
|
-
xhrConfig.data = body;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (params) {
|
|
77
|
-
xhrConfig.params = params;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function transformResponse(response) {
|
|
81
|
-
if (expectJsonContent && !(typeof response.data === 'object')) {
|
|
82
|
-
try {
|
|
83
|
-
resolve(JSON.parse(response.data));
|
|
84
|
-
} catch (e) {
|
|
85
|
-
reject(e);
|
|
86
|
-
}
|
|
87
|
-
} else {
|
|
88
|
-
resolve(response.data);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
this.log.debug("[ApiClient:transformResponse] " + xhrConfig);
|
|
92
|
-
xhr(xhrConfig).then(transformResponse, reject);
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
getOrganizationDetails() {
|
|
97
|
-
this.log.debug("[ApiClient] getOrganizationDetails()");
|
|
98
|
-
return this.callApi('GET', 200, true, null, null);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
getServiceStatus() {
|
|
103
|
-
this.log.debug("[ApiClient] getServiceStatus()");
|
|
104
|
-
return this.callApi('GET', 200, true, ['service-status'], null);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
//Usage Management
|
|
108
|
-
getActiveDevices(start, end, detail) {
|
|
109
|
-
this.log.debug("[ApiClient] getActiveDevices(" + start + ", " + end + ")");
|
|
110
|
-
detail = detail | false;
|
|
111
|
-
let params = {
|
|
112
|
-
start: start,
|
|
113
|
-
end: end,
|
|
114
|
-
detail: detail
|
|
115
|
-
};
|
|
116
|
-
return this.callApi('GET', 200, true, ['usage', 'active-devices'], null, params);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
getHistoricalDataUsage(start, end, detail) {
|
|
120
|
-
this.log.debug("[ApiClient] getHistoricalDataUsage(" + start + ", " + end + ")");
|
|
121
|
-
detail = detail | false;
|
|
122
|
-
let params = {
|
|
123
|
-
start: start,
|
|
124
|
-
end: end,
|
|
125
|
-
detail: detail
|
|
126
|
-
};
|
|
127
|
-
return this.callApi('GET', 200, true, ['usage', 'historical-data'], null, params);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
getDataUsage(start, end, detail) {
|
|
131
|
-
this.log.debug("[ApiClient] getDataUsage(" + start + ", " + end + ")");
|
|
132
|
-
detail = detail | false;
|
|
133
|
-
let params = {
|
|
134
|
-
start: start,
|
|
135
|
-
end: end,
|
|
136
|
-
detail: detail
|
|
137
|
-
};
|
|
138
|
-
return this.callApi('GET', 200, true, ['usage', 'data-traffic'], null, params);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
//client connectivity status
|
|
143
|
-
getConnectionStates(){
|
|
144
|
-
this.log.debug("[ApiClient] getConnectionStates() - client connectivity");
|
|
145
|
-
return this.callApi('GET', 200, true, ["clientconnectionstates"], null);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
getConnectionState(id){
|
|
149
|
-
this.log.debug("[ApiClient] getConnectionState() - client connectivity");
|
|
150
|
-
return this.callApi('GET', 200, true, ["clientconnectionstates/" + id], null);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
getConnectedClientsConnectionStates(){
|
|
154
|
-
this.log.debug("[ApiClient] getConnectedClientsConnectionStates() - client connectivity");
|
|
155
|
-
return this.callApi('GET', 200, true, ["clientconnectionstates?connectionStatus=connected"], null);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
getRecentConnectionStates(date){
|
|
159
|
-
this.log.debug("[ApiClient] getRecentConnectionStates() - client connectivity");
|
|
160
|
-
return this.callApi('GET', 200, true, ["clientconnectionstates?connectedAfter=" + date], null);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
getCustomConnectionState(query){
|
|
164
|
-
this.log.debug("[ApiClient] getCustomConnectionStates() - client connectivity");
|
|
165
|
-
return this.callApi('GET', 200, true, ["clientconnectionstates" + query], null);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
//bulk apis
|
|
169
|
-
getAllDevices(params) {
|
|
170
|
-
this.log.debug("[ApiClient] getAllDevices() - BULK");
|
|
171
|
-
return this.callApi('GET', 200, true, ["bulk", "devices"], null, params);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Gateway Access Control (Beta)
|
|
176
|
-
* The methods in this section follow the documentation listed under the link:
|
|
177
|
-
* https://console.ng.bluemix.net/docs/services/IoT/gateways/gateway-access-control.html#gateway-access-control-beta-
|
|
178
|
-
* Involves the following sections from the above mentioned link:
|
|
179
|
-
* Assigning a role to a gateway
|
|
180
|
-
* Adding devices to and removing devices from a resource group
|
|
181
|
-
* Finding a resource group
|
|
182
|
-
* Querying a resource group
|
|
183
|
-
* Creating and deleting a resource group
|
|
184
|
-
* Updating group properties
|
|
185
|
-
* Retrieving and updating device properties
|
|
186
|
-
*
|
|
187
|
-
*/
|
|
188
|
-
|
|
189
|
-
//getGatewayGroup(gatewayId)
|
|
190
|
-
//updateDeviceRoles(deviceId, roles[])
|
|
191
|
-
//getAllDevicesInGropu(groupId)
|
|
192
|
-
//addDevicesToGroup(groupId, deviceList[])
|
|
193
|
-
//removeDevicesFromGroup(groupId, deviceList[])
|
|
194
|
-
|
|
195
|
-
getGroupIdsForDevice(deviceId){
|
|
196
|
-
this.log.debug("[ApiClient] getGroupIdsForDevice("+deviceId+")");
|
|
197
|
-
return this.callApi('GET', 200, true, ['authorization', 'devices' , deviceId], null);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
updateDeviceRoles(deviceId, roles){
|
|
201
|
-
this.log.debug("[ApiClient] updateDeviceRoles("+deviceId+","+roles+")");
|
|
202
|
-
return this.callApi('PUT', 200, false, ['authorization', 'devices', deviceId, 'roles'], roles);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
getAllDevicesInGroup(groupId){
|
|
206
|
-
this.log.debug("[ApiClient] getAllDevicesInGropu("+groupId+")");
|
|
207
|
-
return this.callApi('GET', 200, true, ['bulk', 'devices' , groupId], null);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
addDevicesToGroup(groupId, deviceList){
|
|
211
|
-
this.log.debug("[ApiClient] addDevicesToGroup("+groupId+","+deviceList+")");
|
|
212
|
-
return this.callApi('PUT', 200, false, ['bulk', 'devices' , groupId, "add"], deviceList);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
removeDevicesFromGroup(groupId, deviceList){
|
|
216
|
-
this.log.debug("[ApiClient] removeDevicesFromGroup("+groupId+","+deviceList+")");
|
|
217
|
-
return this.callApi('PUT', 200, false, ['bulk', 'devices' , groupId, "remove"], deviceList);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// https://console.ng.bluemix.net/docs/services/IoT/gateways/gateway-access-control.html
|
|
221
|
-
|
|
222
|
-
// Finding a Resource Group
|
|
223
|
-
// getGatewayGroups()
|
|
224
|
-
// Querying a resource group
|
|
225
|
-
// getUniqueDevicesInGroup(groupId)
|
|
226
|
-
// getUniqueGatewayGroup(groupId)
|
|
227
|
-
// Creating and deleting a resource group
|
|
228
|
-
// createGatewayGroup(groupName)
|
|
229
|
-
// deleteGatewayGroup(groupId)
|
|
230
|
-
// Retrieving and updating device properties
|
|
231
|
-
// getGatewayGroupProperties()
|
|
232
|
-
// getDeviceRoles(deviceId)
|
|
233
|
-
// updateGatewayProperties(gatewayId,control_props)
|
|
234
|
-
// updateDeviceControlProperties(deviceId, withroles)
|
|
235
|
-
|
|
236
|
-
// Finding a Resource Group
|
|
237
|
-
getAllGroups(){
|
|
238
|
-
this.log.debug("[ApiClient] getAllGroups()");
|
|
239
|
-
return this.callApi('GET', 200, true, ['groups'], null);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Querying a resource group
|
|
243
|
-
|
|
244
|
-
// Get unique identifiers of the members of the resource group
|
|
245
|
-
getAllDeviceIdsInGroup(groupId){
|
|
246
|
-
this.log.debug("[ApiClient] getAllDeviceIdsInGroup("+groupId+")");
|
|
247
|
-
return this.callApi('GET', 200, true, ['bulk', 'devices' , groupId, "ids"], null);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// properties of the resource group
|
|
251
|
-
getGroup(groupId){
|
|
252
|
-
this.log.debug("[ApiClient] getGroup("+groupId+")");
|
|
253
|
-
return this.callApi('GET', 200, true, ['groups', groupId], null);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Creating and deleting a resource group
|
|
257
|
-
|
|
258
|
-
// Create a Resource Group
|
|
259
|
-
createGroup(groupInfo){
|
|
260
|
-
this.log.debug("[ApiClient] createGroup()");
|
|
261
|
-
return this.callApi('POST', 201, true, ['groups'], groupInfo);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Delete a Resource Group
|
|
265
|
-
deleteGroup(groupId){
|
|
266
|
-
this.log.debug("[ApiClient] deleteGroup("+groupId+")");
|
|
267
|
-
return this.callApi('DELETE', 200, false, ['groups', groupId], null);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// Retrieving and updating device properties
|
|
271
|
-
|
|
272
|
-
// Get the ID of the devices group of a gateway
|
|
273
|
-
getAllDeviceAccessControlProperties(){
|
|
274
|
-
this.log.debug("[ApiClient] getAllDeviceAccessControlProperties()");
|
|
275
|
-
return this.callApi('GET', 200, true, ['authorization', 'devices' ], null);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// Get standard role of a gateway
|
|
279
|
-
getDeviceAccessControlProperties(deviceId){
|
|
280
|
-
this.log.debug("[ApiClient] getDeviceAccessControlProperties("+deviceId+")");
|
|
281
|
-
return this.callApi('GET', 200, true, ['authorization', 'devices', deviceId, 'roles'], null);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// Update device properties without affecting the access control properties
|
|
285
|
-
updateDeviceAccessControlProperties(deviceId,deviceProps){
|
|
286
|
-
this.log.debug("[ApiClient] updateDeviceAccessControlProperties("+deviceId+")");
|
|
287
|
-
return this.callApi('PUT', 200, true, ['authorization', 'devices' , deviceId], deviceProps);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// Assign a standard role to a gateway
|
|
291
|
-
updateDeviceAccessControlPropertiesWithRoles(deviceId, devicePropsWithRoles){
|
|
292
|
-
this.log.debug("[ApiClient] updateDeviceAccessControlPropertiesWithRoles("+deviceId+","+devicePropsWithRoles+")");
|
|
293
|
-
return this.callApi('PUT', 200, true, ['authorization', 'devices', deviceId, 'withroles'], devicePropsWithRoles);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Duplicating updateDeviceRoles(deviceId, roles) for Gateway Roles
|
|
297
|
-
updateGatewayRoles(gatewayId, roles){
|
|
298
|
-
this.log.debug("[ApiClient] updateGatewayRoles("+gatewayId+","+roles+")");
|
|
299
|
-
return this.callApi('PUT', 200, false, ['authorization', 'devices', gatewayId, 'roles'], roles);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// Extending getAllGroups() to fetch individual Groups
|
|
303
|
-
getGroups(groupId){
|
|
304
|
-
this.log.debug("[ApiClient] getGroups("+groupId+")");
|
|
305
|
-
return this.callApi('GET', 200, true, ['groups', groupId], null);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
callFormDataApi(method, expectedHttpCode, expectJsonContent, paths, body, params) {
|
|
310
|
-
return new Promise((resolve, reject) => {
|
|
311
|
-
let uri = this.config.getApiBaseUri();
|
|
312
|
-
|
|
313
|
-
if (Array.isArray(paths)) {
|
|
314
|
-
for (var i = 0, l = paths.length; i < l; i++) {
|
|
315
|
-
uri += '/' + paths[i];
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
let xhrConfig = {
|
|
320
|
-
url: uri,
|
|
321
|
-
method: method,
|
|
322
|
-
headers: {
|
|
323
|
-
'Content-Type': 'multipart/form-data'
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
if (this.useLtpa) {
|
|
328
|
-
xhrConfig.withCredentials = true;
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
xhrConfig.headers['Authorization'] = 'Basic ' + btoa(this.apiKey + ':' + this.apiToken);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
if (body) {
|
|
335
|
-
xhrConfig.data = body;
|
|
336
|
-
if(isBrowser()) {
|
|
337
|
-
xhrConfig.transformRequest = [function (data) {
|
|
338
|
-
var formData = new FormData()
|
|
339
|
-
|
|
340
|
-
if(xhrConfig.method == "POST") {
|
|
341
|
-
if(data.schemaFile) {
|
|
342
|
-
var blob = new Blob([data.schemaFile], { type: "application/json" })
|
|
343
|
-
formData.append('schemaFile', blob)
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
if(data.name) {
|
|
347
|
-
formData.append('name', data.name)
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
if (data.schemaType) {
|
|
351
|
-
formData.append('schemaType', 'json-schema')
|
|
352
|
-
}
|
|
353
|
-
if (data.description) {
|
|
354
|
-
formData.append('description', data.description)
|
|
355
|
-
}
|
|
356
|
-
} else if(xhrConfig.method == "PUT") {
|
|
357
|
-
if(data.schemaFile) {
|
|
358
|
-
var blob = new Blob([data.schemaFile], { type: "application/json", name: data.name })
|
|
359
|
-
formData.append('schemaFile', blob)
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
return formData;
|
|
364
|
-
}]
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (params) {
|
|
369
|
-
xhrConfig.params = params;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
function transformResponse(response) {
|
|
373
|
-
if (response.status === expectedHttpCode) {
|
|
374
|
-
if (expectJsonContent && !(typeof response.data === 'object')) {
|
|
375
|
-
try {
|
|
376
|
-
resolve(JSON.parse(response.data));
|
|
377
|
-
} catch (e) {
|
|
378
|
-
reject(e);
|
|
379
|
-
}
|
|
380
|
-
} else {
|
|
381
|
-
resolve(response.data);
|
|
382
|
-
}
|
|
383
|
-
} else {
|
|
384
|
-
reject(new Error(method + " " + uri + ": Expected HTTP " + expectedHttpCode + " from server but got HTTP " + response.status + ". Error Body: " + JSON.stringify(response.data)));
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
this.log.debug("[ApiClient:transformResponse] " + xhrConfig);
|
|
388
|
-
|
|
389
|
-
if(isBrowser()) {
|
|
390
|
-
xhr(xhrConfig).then(transformResponse, reject);
|
|
391
|
-
} else {
|
|
392
|
-
var formData = null
|
|
393
|
-
var config = {
|
|
394
|
-
url: uri,
|
|
395
|
-
method: method,
|
|
396
|
-
headers: {'Content-Type': 'multipart/form-data'},
|
|
397
|
-
auth : {
|
|
398
|
-
user : this.apiKey,
|
|
399
|
-
pass : this.apiToken
|
|
400
|
-
},
|
|
401
|
-
formData: {},
|
|
402
|
-
rejectUnauthorized: false
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
if(xhrConfig.method == "POST") {
|
|
406
|
-
formData = {
|
|
407
|
-
'schemaFile': {
|
|
408
|
-
'value': body.schemaFile,
|
|
409
|
-
'options': {
|
|
410
|
-
'contentType': 'application/json',
|
|
411
|
-
'filename': body.name
|
|
412
|
-
}
|
|
413
|
-
},
|
|
414
|
-
'schemaType': 'json-schema',
|
|
415
|
-
'name': body.name
|
|
416
|
-
}
|
|
417
|
-
config.formData = formData
|
|
418
|
-
} else if(xhrConfig.method == "PUT") {
|
|
419
|
-
formData = {
|
|
420
|
-
'schemaFile': {
|
|
421
|
-
'value': body.schemaFile,
|
|
422
|
-
'options': {
|
|
423
|
-
'contentType': 'application/json',
|
|
424
|
-
'filename': body.name
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
config.formData = formData
|
|
429
|
-
}
|
|
430
|
-
request(config, function optionalCallback(err, response, body) {
|
|
431
|
-
if (response.statusCode === expectedHttpCode) {
|
|
432
|
-
if (expectJsonContent && !(typeof response.data === 'object')) {
|
|
433
|
-
try {
|
|
434
|
-
resolve(JSON.parse(body));
|
|
435
|
-
} catch (e) {
|
|
436
|
-
reject(e);
|
|
437
|
-
}
|
|
438
|
-
} else {
|
|
439
|
-
resolve(body);
|
|
440
|
-
}
|
|
441
|
-
} else {
|
|
442
|
-
reject(new Error(method + " " + uri + ": Expected HTTP " + expectedHttpCode + " from server but got HTTP " + response.statusCode + ". Error Body: " + err));
|
|
443
|
-
}
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
invalidOperation(message) {
|
|
450
|
-
return new Promise((resolve, reject) => {
|
|
451
|
-
resolve(message)
|
|
452
|
-
})
|
|
453
|
-
}
|
|
454
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
*****************************************************************************
|
|
3
|
+
Copyright (c) 2014, 2019 IBM Corporation and other Contributors.
|
|
4
|
+
All rights reserved. This program and the accompanying materials
|
|
5
|
+
are made available under the terms of the Eclipse Public License v1.0
|
|
6
|
+
which accompanies this distribution, and is available at
|
|
7
|
+
http://www.eclipse.org/legal/epl-v10.html
|
|
8
|
+
*****************************************************************************
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
import xhr from 'axios';
|
|
12
|
+
import Promise from 'bluebird';
|
|
13
|
+
import btoa from 'btoa';
|
|
14
|
+
import FormData from 'form-data';
|
|
15
|
+
import log from 'loglevel';
|
|
16
|
+
|
|
17
|
+
import { isBrowser } from '../util';
|
|
18
|
+
|
|
19
|
+
export default class ApiClient {
|
|
20
|
+
constructor(config) {
|
|
21
|
+
this.log = log;
|
|
22
|
+
this.log.setDefaultLevel(config.options.logLevel);
|
|
23
|
+
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.useLtpa = this.config.auth && this.config.auth.useLtpa;
|
|
26
|
+
|
|
27
|
+
this.log.debug("[ApiClient:constructor] ApiClient initialized for " + this.config.getApiBaseUri());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// e.g. [{name: true}, {description: false}] => -name,description
|
|
31
|
+
parseSortSpec(sortSpec) {
|
|
32
|
+
return sortSpec
|
|
33
|
+
? sortSpec.map(s=>{
|
|
34
|
+
const e = Object.entries(s)[0];
|
|
35
|
+
return `${e[1] ? '-' : ''}${e[0]}`
|
|
36
|
+
}).join(',')
|
|
37
|
+
: undefined;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
callApi(method, expectedHttpCode, expectJsonContent, paths, body, params) {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
let uri = this.config.getApiBaseUri();
|
|
43
|
+
|
|
44
|
+
if (Array.isArray(paths)) {
|
|
45
|
+
for (var i = 0, l = paths.length; i < l; i++) {
|
|
46
|
+
uri += '/' + paths[i];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let xhrConfig = {
|
|
51
|
+
url: uri,
|
|
52
|
+
method: method,
|
|
53
|
+
headers: {
|
|
54
|
+
'Content-Type': 'application/json'
|
|
55
|
+
},
|
|
56
|
+
validateStatus: (status) => {
|
|
57
|
+
if(Array.isArray(expectedHttpCode)) {
|
|
58
|
+
return expectedHttpCode.includes(status);
|
|
59
|
+
} else {
|
|
60
|
+
return status === expectedHttpCode;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
if (this.useLtpa) {
|
|
66
|
+
xhrConfig.withCredentials = true;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
xhrConfig.headers['Authorization'] = 'Basic ' + btoa(this.config.auth.key + ':' + this.config.auth.token);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (body) {
|
|
73
|
+
xhrConfig.data = body;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (params) {
|
|
77
|
+
xhrConfig.params = params;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function transformResponse(response) {
|
|
81
|
+
if (expectJsonContent && !(typeof response.data === 'object')) {
|
|
82
|
+
try {
|
|
83
|
+
resolve(JSON.parse(response.data));
|
|
84
|
+
} catch (e) {
|
|
85
|
+
reject(e);
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
resolve(response.data);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this.log.debug("[ApiClient:transformResponse] " + xhrConfig);
|
|
92
|
+
xhr(xhrConfig).then(transformResponse, reject);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getOrganizationDetails() {
|
|
97
|
+
this.log.debug("[ApiClient] getOrganizationDetails()");
|
|
98
|
+
return this.callApi('GET', 200, true, null, null);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
getServiceStatus() {
|
|
103
|
+
this.log.debug("[ApiClient] getServiceStatus()");
|
|
104
|
+
return this.callApi('GET', 200, true, ['service-status'], null);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//Usage Management
|
|
108
|
+
getActiveDevices(start, end, detail) {
|
|
109
|
+
this.log.debug("[ApiClient] getActiveDevices(" + start + ", " + end + ")");
|
|
110
|
+
detail = detail | false;
|
|
111
|
+
let params = {
|
|
112
|
+
start: start,
|
|
113
|
+
end: end,
|
|
114
|
+
detail: detail
|
|
115
|
+
};
|
|
116
|
+
return this.callApi('GET', 200, true, ['usage', 'active-devices'], null, params);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
getHistoricalDataUsage(start, end, detail) {
|
|
120
|
+
this.log.debug("[ApiClient] getHistoricalDataUsage(" + start + ", " + end + ")");
|
|
121
|
+
detail = detail | false;
|
|
122
|
+
let params = {
|
|
123
|
+
start: start,
|
|
124
|
+
end: end,
|
|
125
|
+
detail: detail
|
|
126
|
+
};
|
|
127
|
+
return this.callApi('GET', 200, true, ['usage', 'historical-data'], null, params);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
getDataUsage(start, end, detail) {
|
|
131
|
+
this.log.debug("[ApiClient] getDataUsage(" + start + ", " + end + ")");
|
|
132
|
+
detail = detail | false;
|
|
133
|
+
let params = {
|
|
134
|
+
start: start,
|
|
135
|
+
end: end,
|
|
136
|
+
detail: detail
|
|
137
|
+
};
|
|
138
|
+
return this.callApi('GET', 200, true, ['usage', 'data-traffic'], null, params);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
//client connectivity status
|
|
143
|
+
getConnectionStates(){
|
|
144
|
+
this.log.debug("[ApiClient] getConnectionStates() - client connectivity");
|
|
145
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates"], null);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
getConnectionState(id){
|
|
149
|
+
this.log.debug("[ApiClient] getConnectionState() - client connectivity");
|
|
150
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates/" + id], null);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
getConnectedClientsConnectionStates(){
|
|
154
|
+
this.log.debug("[ApiClient] getConnectedClientsConnectionStates() - client connectivity");
|
|
155
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates?connectionStatus=connected"], null);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
getRecentConnectionStates(date){
|
|
159
|
+
this.log.debug("[ApiClient] getRecentConnectionStates() - client connectivity");
|
|
160
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates?connectedAfter=" + date], null);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
getCustomConnectionState(query){
|
|
164
|
+
this.log.debug("[ApiClient] getCustomConnectionStates() - client connectivity");
|
|
165
|
+
return this.callApi('GET', 200, true, ["clientconnectionstates" + query], null);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
//bulk apis
|
|
169
|
+
getAllDevices(params) {
|
|
170
|
+
this.log.debug("[ApiClient] getAllDevices() - BULK");
|
|
171
|
+
return this.callApi('GET', 200, true, ["bulk", "devices"], null, params);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Gateway Access Control (Beta)
|
|
176
|
+
* The methods in this section follow the documentation listed under the link:
|
|
177
|
+
* https://console.ng.bluemix.net/docs/services/IoT/gateways/gateway-access-control.html#gateway-access-control-beta-
|
|
178
|
+
* Involves the following sections from the above mentioned link:
|
|
179
|
+
* Assigning a role to a gateway
|
|
180
|
+
* Adding devices to and removing devices from a resource group
|
|
181
|
+
* Finding a resource group
|
|
182
|
+
* Querying a resource group
|
|
183
|
+
* Creating and deleting a resource group
|
|
184
|
+
* Updating group properties
|
|
185
|
+
* Retrieving and updating device properties
|
|
186
|
+
*
|
|
187
|
+
*/
|
|
188
|
+
|
|
189
|
+
//getGatewayGroup(gatewayId)
|
|
190
|
+
//updateDeviceRoles(deviceId, roles[])
|
|
191
|
+
//getAllDevicesInGropu(groupId)
|
|
192
|
+
//addDevicesToGroup(groupId, deviceList[])
|
|
193
|
+
//removeDevicesFromGroup(groupId, deviceList[])
|
|
194
|
+
|
|
195
|
+
getGroupIdsForDevice(deviceId){
|
|
196
|
+
this.log.debug("[ApiClient] getGroupIdsForDevice("+deviceId+")");
|
|
197
|
+
return this.callApi('GET', 200, true, ['authorization', 'devices' , deviceId], null);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
updateDeviceRoles(deviceId, roles){
|
|
201
|
+
this.log.debug("[ApiClient] updateDeviceRoles("+deviceId+","+roles+")");
|
|
202
|
+
return this.callApi('PUT', 200, false, ['authorization', 'devices', deviceId, 'roles'], roles);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
getAllDevicesInGroup(groupId){
|
|
206
|
+
this.log.debug("[ApiClient] getAllDevicesInGropu("+groupId+")");
|
|
207
|
+
return this.callApi('GET', 200, true, ['bulk', 'devices' , groupId], null);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
addDevicesToGroup(groupId, deviceList){
|
|
211
|
+
this.log.debug("[ApiClient] addDevicesToGroup("+groupId+","+deviceList+")");
|
|
212
|
+
return this.callApi('PUT', 200, false, ['bulk', 'devices' , groupId, "add"], deviceList);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
removeDevicesFromGroup(groupId, deviceList){
|
|
216
|
+
this.log.debug("[ApiClient] removeDevicesFromGroup("+groupId+","+deviceList+")");
|
|
217
|
+
return this.callApi('PUT', 200, false, ['bulk', 'devices' , groupId, "remove"], deviceList);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// https://console.ng.bluemix.net/docs/services/IoT/gateways/gateway-access-control.html
|
|
221
|
+
|
|
222
|
+
// Finding a Resource Group
|
|
223
|
+
// getGatewayGroups()
|
|
224
|
+
// Querying a resource group
|
|
225
|
+
// getUniqueDevicesInGroup(groupId)
|
|
226
|
+
// getUniqueGatewayGroup(groupId)
|
|
227
|
+
// Creating and deleting a resource group
|
|
228
|
+
// createGatewayGroup(groupName)
|
|
229
|
+
// deleteGatewayGroup(groupId)
|
|
230
|
+
// Retrieving and updating device properties
|
|
231
|
+
// getGatewayGroupProperties()
|
|
232
|
+
// getDeviceRoles(deviceId)
|
|
233
|
+
// updateGatewayProperties(gatewayId,control_props)
|
|
234
|
+
// updateDeviceControlProperties(deviceId, withroles)
|
|
235
|
+
|
|
236
|
+
// Finding a Resource Group
|
|
237
|
+
getAllGroups(){
|
|
238
|
+
this.log.debug("[ApiClient] getAllGroups()");
|
|
239
|
+
return this.callApi('GET', 200, true, ['groups'], null);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Querying a resource group
|
|
243
|
+
|
|
244
|
+
// Get unique identifiers of the members of the resource group
|
|
245
|
+
getAllDeviceIdsInGroup(groupId){
|
|
246
|
+
this.log.debug("[ApiClient] getAllDeviceIdsInGroup("+groupId+")");
|
|
247
|
+
return this.callApi('GET', 200, true, ['bulk', 'devices' , groupId, "ids"], null);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// properties of the resource group
|
|
251
|
+
getGroup(groupId){
|
|
252
|
+
this.log.debug("[ApiClient] getGroup("+groupId+")");
|
|
253
|
+
return this.callApi('GET', 200, true, ['groups', groupId], null);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Creating and deleting a resource group
|
|
257
|
+
|
|
258
|
+
// Create a Resource Group
|
|
259
|
+
createGroup(groupInfo){
|
|
260
|
+
this.log.debug("[ApiClient] createGroup()");
|
|
261
|
+
return this.callApi('POST', 201, true, ['groups'], groupInfo);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Delete a Resource Group
|
|
265
|
+
deleteGroup(groupId){
|
|
266
|
+
this.log.debug("[ApiClient] deleteGroup("+groupId+")");
|
|
267
|
+
return this.callApi('DELETE', 200, false, ['groups', groupId], null);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Retrieving and updating device properties
|
|
271
|
+
|
|
272
|
+
// Get the ID of the devices group of a gateway
|
|
273
|
+
getAllDeviceAccessControlProperties(){
|
|
274
|
+
this.log.debug("[ApiClient] getAllDeviceAccessControlProperties()");
|
|
275
|
+
return this.callApi('GET', 200, true, ['authorization', 'devices' ], null);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Get standard role of a gateway
|
|
279
|
+
getDeviceAccessControlProperties(deviceId){
|
|
280
|
+
this.log.debug("[ApiClient] getDeviceAccessControlProperties("+deviceId+")");
|
|
281
|
+
return this.callApi('GET', 200, true, ['authorization', 'devices', deviceId, 'roles'], null);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Update device properties without affecting the access control properties
|
|
285
|
+
updateDeviceAccessControlProperties(deviceId,deviceProps){
|
|
286
|
+
this.log.debug("[ApiClient] updateDeviceAccessControlProperties("+deviceId+")");
|
|
287
|
+
return this.callApi('PUT', 200, true, ['authorization', 'devices' , deviceId], deviceProps);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Assign a standard role to a gateway
|
|
291
|
+
updateDeviceAccessControlPropertiesWithRoles(deviceId, devicePropsWithRoles){
|
|
292
|
+
this.log.debug("[ApiClient] updateDeviceAccessControlPropertiesWithRoles("+deviceId+","+devicePropsWithRoles+")");
|
|
293
|
+
return this.callApi('PUT', 200, true, ['authorization', 'devices', deviceId, 'withroles'], devicePropsWithRoles);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Duplicating updateDeviceRoles(deviceId, roles) for Gateway Roles
|
|
297
|
+
updateGatewayRoles(gatewayId, roles){
|
|
298
|
+
this.log.debug("[ApiClient] updateGatewayRoles("+gatewayId+","+roles+")");
|
|
299
|
+
return this.callApi('PUT', 200, false, ['authorization', 'devices', gatewayId, 'roles'], roles);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Extending getAllGroups() to fetch individual Groups
|
|
303
|
+
getGroups(groupId){
|
|
304
|
+
this.log.debug("[ApiClient] getGroups("+groupId+")");
|
|
305
|
+
return this.callApi('GET', 200, true, ['groups', groupId], null);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
callFormDataApi(method, expectedHttpCode, expectJsonContent, paths, body, params) {
|
|
310
|
+
return new Promise((resolve, reject) => {
|
|
311
|
+
let uri = this.config.getApiBaseUri();
|
|
312
|
+
|
|
313
|
+
if (Array.isArray(paths)) {
|
|
314
|
+
for (var i = 0, l = paths.length; i < l; i++) {
|
|
315
|
+
uri += '/' + paths[i];
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
let xhrConfig = {
|
|
320
|
+
url: uri,
|
|
321
|
+
method: method,
|
|
322
|
+
headers: {
|
|
323
|
+
'Content-Type': 'multipart/form-data'
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
if (this.useLtpa) {
|
|
328
|
+
xhrConfig.withCredentials = true;
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
xhrConfig.headers['Authorization'] = 'Basic ' + btoa(this.apiKey + ':' + this.apiToken);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (body) {
|
|
335
|
+
xhrConfig.data = body;
|
|
336
|
+
if(isBrowser()) {
|
|
337
|
+
xhrConfig.transformRequest = [function (data) {
|
|
338
|
+
var formData = new FormData()
|
|
339
|
+
|
|
340
|
+
if(xhrConfig.method == "POST") {
|
|
341
|
+
if(data.schemaFile) {
|
|
342
|
+
var blob = new Blob([data.schemaFile], { type: "application/json" })
|
|
343
|
+
formData.append('schemaFile', blob)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if(data.name) {
|
|
347
|
+
formData.append('name', data.name)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (data.schemaType) {
|
|
351
|
+
formData.append('schemaType', 'json-schema')
|
|
352
|
+
}
|
|
353
|
+
if (data.description) {
|
|
354
|
+
formData.append('description', data.description)
|
|
355
|
+
}
|
|
356
|
+
} else if(xhrConfig.method == "PUT") {
|
|
357
|
+
if(data.schemaFile) {
|
|
358
|
+
var blob = new Blob([data.schemaFile], { type: "application/json", name: data.name })
|
|
359
|
+
formData.append('schemaFile', blob)
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return formData;
|
|
364
|
+
}]
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (params) {
|
|
369
|
+
xhrConfig.params = params;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function transformResponse(response) {
|
|
373
|
+
if (response.status === expectedHttpCode) {
|
|
374
|
+
if (expectJsonContent && !(typeof response.data === 'object')) {
|
|
375
|
+
try {
|
|
376
|
+
resolve(JSON.parse(response.data));
|
|
377
|
+
} catch (e) {
|
|
378
|
+
reject(e);
|
|
379
|
+
}
|
|
380
|
+
} else {
|
|
381
|
+
resolve(response.data);
|
|
382
|
+
}
|
|
383
|
+
} else {
|
|
384
|
+
reject(new Error(method + " " + uri + ": Expected HTTP " + expectedHttpCode + " from server but got HTTP " + response.status + ". Error Body: " + JSON.stringify(response.data)));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
this.log.debug("[ApiClient:transformResponse] " + xhrConfig);
|
|
388
|
+
|
|
389
|
+
if(isBrowser()) {
|
|
390
|
+
xhr(xhrConfig).then(transformResponse, reject);
|
|
391
|
+
} else {
|
|
392
|
+
var formData = null
|
|
393
|
+
var config = {
|
|
394
|
+
url: uri,
|
|
395
|
+
method: method,
|
|
396
|
+
headers: {'Content-Type': 'multipart/form-data'},
|
|
397
|
+
auth : {
|
|
398
|
+
user : this.apiKey,
|
|
399
|
+
pass : this.apiToken
|
|
400
|
+
},
|
|
401
|
+
formData: {},
|
|
402
|
+
rejectUnauthorized: false
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if(xhrConfig.method == "POST") {
|
|
406
|
+
formData = {
|
|
407
|
+
'schemaFile': {
|
|
408
|
+
'value': body.schemaFile,
|
|
409
|
+
'options': {
|
|
410
|
+
'contentType': 'application/json',
|
|
411
|
+
'filename': body.name
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
'schemaType': 'json-schema',
|
|
415
|
+
'name': body.name
|
|
416
|
+
}
|
|
417
|
+
config.formData = formData
|
|
418
|
+
} else if(xhrConfig.method == "PUT") {
|
|
419
|
+
formData = {
|
|
420
|
+
'schemaFile': {
|
|
421
|
+
'value': body.schemaFile,
|
|
422
|
+
'options': {
|
|
423
|
+
'contentType': 'application/json',
|
|
424
|
+
'filename': body.name
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
config.formData = formData
|
|
429
|
+
}
|
|
430
|
+
request(config, function optionalCallback(err, response, body) {
|
|
431
|
+
if (response.statusCode === expectedHttpCode) {
|
|
432
|
+
if (expectJsonContent && !(typeof response.data === 'object')) {
|
|
433
|
+
try {
|
|
434
|
+
resolve(JSON.parse(body));
|
|
435
|
+
} catch (e) {
|
|
436
|
+
reject(e);
|
|
437
|
+
}
|
|
438
|
+
} else {
|
|
439
|
+
resolve(body);
|
|
440
|
+
}
|
|
441
|
+
} else {
|
|
442
|
+
reject(new Error(method + " " + uri + ": Expected HTTP " + expectedHttpCode + " from server but got HTTP " + response.statusCode + ". Error Body: " + err));
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
invalidOperation(message) {
|
|
450
|
+
return new Promise((resolve, reject) => {
|
|
451
|
+
resolve(message)
|
|
452
|
+
})
|
|
453
|
+
}
|
|
454
|
+
}
|