blob: 4a54ebe3a7ee4620c9e0c77def8cc07b83be3c17 [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 };
28 return new Promise(function (resolve, reject) {
29 if (method === 'POST' || method === 'PUT') {
30 if (isBinary) {
31 var formData = {
32 upload: fs.createReadStream(data),
33 };
34 options.formData = formData;
35 } else {
36 options.json = data;
37 }
38 }
39 request(options, function (err, result, data) {
40 context.inputData = null;
41 if (err) {
42 console.error('Request URL: ' + result.request.uri.href);
43 console.error('Request Method: ' + result.request.method);
44 console.error('Response Status Code: ' +result.statusCode);
45 console.log(err);
46 reject(err);
47 } else {
48 let isExpected = (context.shouldFail) ? (result.statusCode != 200 && result.statusCode != 201) : (result.statusCode == 200 || result.statusCode == 201);
49 if (!isExpected) {
50 console.error('Request URL: ' + result.request.uri.href);
51 console.error('Request Method: ' + result.request.method);
52 console.error('Response Status Code: ' +result.statusCode);
53 console.error(result.body);
54 reject('Status Code was ' + result.statusCode);
55 }
56 if (context.shouldFail && context.errorCode) {
ayalabenaad27812018-02-18 10:03:22 +020057 if (typeof data === 'string' && data) {
58 data = JSON.parse(data);
59 }
ilanap637206b2018-02-04 17:06:22 +020060 let errorCode = data.errorCode;
61 let contextErrorCode = context.errorCode;
62 context.errorCode = null;
63 if (errorCode !== contextErrorCode) {
64 reject('Error Code was ' + errorCode + ' instead of ' + contextErrorCode);
65 }
66 }
ayalabenaad27812018-02-18 10:03:22 +020067 if (context.shouldFail && context.errorMessage) {
68 if (typeof data === 'string' && data) {
69 data = JSON.parse(data);
70 }
71 let errorMessage = data.message;
72 let contextErrorMessage = context.errorMessage;
73 context.errorMessage = null;
74 if (errorMessage !== contextErrorMessage) {
75 reject('Error Message was ' + errorMessage + ' instead of ' + contextErrorMessage);
76 }
77 }
ilanap637206b2018-02-04 17:06:22 +020078 if (context.shouldFail) {
79 context.shouldFail = false;
80 resolve({statusCode: result.statusCode, data: {}});
81 return;
82 }
83 if (method === 'GET' && isBinary) {
84 // downloading (NetworkPackage) files
85 return ({
86 blob: blobUtil.createBlob([data], {type: 'text/plain'}),
87 headers: result.headers
88 });
89 } else {
90 if (typeof data === 'string' && data) {
91 data = JSON.parse(data);
92 }
93 context.responseData = data;
94 context.inputData = data;
95 resolve({statusCode: result.statusCode, data: data});
96 }
97 }
98 });
99 });
100}
101
ilanapbd76c092018-02-19 10:36:26 +0200102function download(context, path, filePath, callback, type='onboarding') {
103 let server = context.getUrlForType(type);
104 let options = {
ilanap637206b2018-02-04 17:06:22 +0200105 method: 'GET',
ilanapbd76c092018-02-19 10:36:26 +0200106 url: server + path,
ilanap637206b2018-02-04 17:06:22 +0200107 headers: context.headers
108 };
ilanapbd76c092018-02-19 10:36:26 +0200109 var file = fs.createWriteStream(filePath);
ilanap637206b2018-02-04 17:06:22 +0200110 var r = request(options).pipe(file);
111 r.on('error', function (err) {
112 console.log(err);
113 callback(err);
114 });
115 r.on('finish', function () {
116 file.close();
117 let zipFile = fs.readFileSync(filePath, 'binary');
118 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
119 if (zip.files['MANIFEST.json']) {
120 let manifestData = zip.files['MANIFEST.json']._data;
121 manifestData = manifestData.replace(/\\n/g, '');
122 context.responseData = JSON.parse(manifestData);
123 }
124 callback();
125 });
126
127};
128
129function _random() {
130 let d = new Date();
131 return d.getTime().toString().split('').reverse().join('');
132}
133
134function _getJSONFromFile(file) {
135 return JSON.parse(fs.readFileSync(file, 'utf8'));
136}
137
138
139module.exports = {
140 request: _request,
141 random : _random,
142 getJSONFromFile: _getJSONFromFile,
143 download: download
144};