blob: 46534072f298628f288f2b304ae257c50130d05b [file] [log] [blame]
ilanap637206b2018-02-04 17:06:22 +02001/*
2 * Copyright © 2016-2017 European Support Limited
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16const request = require('request');
17const fs = require('fs');
18require('node-zip');
19
ilanapbd76c092018-02-19 10:36:26 +020020function _request(context, method, path, data, isBinary=false, type='onboarding') {
21 let server = context.getUrlForType(type);
ilanap6c872742018-02-18 11:02:03 +020022
ilanap637206b2018-02-04 17:06:22 +020023 let options = {
24 method: method,
25 url: server + path,
26 headers: context.headers
27 };
ilanap041deed2018-02-20 11:57:12 +020028 console.log('--> Calling REST ' + options.method +' url: ' + options.url);
29
ilanap637206b2018-02-04 17:06:22 +020030 return new Promise(function (resolve, reject) {
31 if (method === 'POST' || method === 'PUT') {
32 if (isBinary) {
33 var formData = {
34 upload: fs.createReadStream(data),
35 };
36 options.formData = formData;
37 } else {
38 options.json = data;
39 }
40 }
41 request(options, function (err, result, data) {
42 context.inputData = null;
43 if (err) {
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);
47 console.log(err);
48 reject(err);
49 } else {
50 let isExpected = (context.shouldFail) ? (result.statusCode != 200 && result.statusCode != 201) : (result.statusCode == 200 || result.statusCode == 201);
51 if (!isExpected) {
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);
57 }
58 if (context.shouldFail && context.errorCode) {
ayalabenaad27812018-02-18 10:03:22 +020059 if (typeof data === 'string' && data) {
60 data = JSON.parse(data);
61 }
ilanap637206b2018-02-04 17:06:22 +020062 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);
67 }
68 }
ayalabenaad27812018-02-18 10:03:22 +020069 if (context.shouldFail && context.errorMessage) {
70 if (typeof data === 'string' && data) {
71 data = JSON.parse(data);
72 }
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);
78 }
79 }
ilanap637206b2018-02-04 17:06:22 +020080 if (context.shouldFail) {
81 context.shouldFail = false;
82 resolve({statusCode: result.statusCode, data: {}});
83 return;
84 }
85 if (method === 'GET' && isBinary) {
86 // downloading (NetworkPackage) files
87 return ({
88 blob: blobUtil.createBlob([data], {type: 'text/plain'}),
89 headers: result.headers
90 });
91 } else {
92 if (typeof data === 'string' && data) {
93 data = JSON.parse(data);
94 }
95 context.responseData = data;
96 context.inputData = data;
97 resolve({statusCode: result.statusCode, data: data});
98 }
99 }
100 });
101 });
102}
103
ilanapbd76c092018-02-19 10:36:26 +0200104function download(context, path, filePath, callback, type='onboarding') {
105 let server = context.getUrlForType(type);
106 let options = {
ilanap637206b2018-02-04 17:06:22 +0200107 method: 'GET',
ilanapbd76c092018-02-19 10:36:26 +0200108 url: server + path,
ilanap637206b2018-02-04 17:06:22 +0200109 headers: context.headers
110 };
ilanap041deed2018-02-20 11:57:12 +0200111 console.log('--> Calling REST download url: ' + options.url);
112
ilanapbd76c092018-02-19 10:36:26 +0200113 var file = fs.createWriteStream(filePath);
ilanap637206b2018-02-04 17:06:22 +0200114 var r = request(options).pipe(file);
115 r.on('error', function (err) {
116 console.log(err);
117 callback(err);
118 });
119 r.on('finish', function () {
120 file.close();
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);
127 }
128 callback();
129 });
130
131};
132
133function _random() {
134 let d = new Date();
135 return d.getTime().toString().split('').reverse().join('');
136}
137
138function _getJSONFromFile(file) {
139 return JSON.parse(fs.readFileSync(file, 'utf8'));
140}
141
142
143module.exports = {
144 request: _request,
145 random : _random,
146 getJSONFromFile: _getJSONFromFile,
147 download: download
148};