2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 const request = require('request');
17 const fs = require('fs');
20 function _request(context, method, path, data, isBinary=false, type='onboarding') {
21 let server = context.getUrlForType(type);
26 headers: context.headers[type]
28 console.log('--> Calling REST ' + options.method +' url: ' + options.url);
30 return new Promise(function (resolve, reject) {
31 if (method === 'POST' || method === 'PUT') {
34 upload: fs.createReadStream(data),
36 options.formData = formData;
41 request(options, function (err, result, data) {
42 context.inputData = null;
44 console.error('Request URL: ' + result.request.uri.href);
45 console.error('Request Method: ' + result.request.method);
46 console.error('Response Status Code: ' +result.statusCode);
50 let isExpected = (context.shouldFail) ? (result.statusCode != 200 && result.statusCode != 201) : (result.statusCode == 200 || result.statusCode == 201);
52 console.error('Request URL: ' + result.request.uri.href);
53 console.error('Request Method: ' + result.request.method);
54 console.error('Response Status Code: ' +result.statusCode);
55 console.error(result.body);
56 reject('Status Code was ' + result.statusCode);
58 if (context.shouldFail && context.errorCode) {
59 if (typeof data === 'string' && data) {
60 data = JSON.parse(data);
62 let errorCode = data.errorCode;
63 let contextErrorCode = context.errorCode;
64 context.errorCode = null;
65 if (errorCode !== contextErrorCode) {
66 reject('Error Code was ' + errorCode + ' instead of ' + contextErrorCode);
69 if (context.shouldFail && context.errorMessage) {
70 if (typeof data === 'string' && data) {
71 data = JSON.parse(data);
73 let errorMessage = data.message;
74 let contextErrorMessage = context.errorMessage;
75 context.errorMessage = null;
76 if (errorMessage !== contextErrorMessage) {
77 reject('Error Message was ' + errorMessage + ' instead of ' + contextErrorMessage);
80 if (context.shouldFail) {
81 context.shouldFail = false;
82 resolve({statusCode: result.statusCode, data: {}});
85 if (method === 'GET' && isBinary) {
86 // downloading (NetworkPackage) files
88 blob: blobUtil.createBlob([data], {type: 'text/plain'}),
89 headers: result.headers
92 if (typeof data === 'string' && data) {
93 data = JSON.parse(data);
95 context.responseData = data;
96 context.inputData = data;
97 resolve({statusCode: result.statusCode, data: data});
104 function download(context, path, filePath, callback, type='onboarding') {
105 let server = context.getUrlForType(type);
109 headers: context.headers[type]
111 console.log('--> Calling REST download url: ' + options.url);
113 var file = fs.createWriteStream(filePath);
114 var r = request(options).pipe(file);
115 r.on('error', function (err) {
119 r.on('finish', function () {
121 let zipFile = fs.readFileSync(filePath, 'binary');
122 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
123 if (zip.files['MANIFEST.json']) {
124 let manifestData = zip.files['MANIFEST.json']._data;
125 manifestData = manifestData.replace(/\\n/g, '');
126 context.responseData = JSON.parse(manifestData);
135 return d.getTime().toString().split('').reverse().join('');
138 function _getJSONFromFile(file) {
139 return JSON.parse(fs.readFileSync(file, 'utf8'));
146 getJSONFromFile: _getJSONFromFile,