blob: 08d0a2b10f2a289a521959b0f795218099fbc677 [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
ilanap6c872742018-02-18 11:02:03 +020020function _request(context, method, path, data, isBinary=false, prefix='onboarding') {
21 let server = context.server + '/' + context.prefix[prefix];
22
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
102function download(context, path, filePath, callback){
103 let options = {
104 method: 'GET',
105 url: context.onboarding_server + path,
106 headers: context.headers
107 };
108 var file = fs.createWriteStream(filePath);
109 var r = request(options).pipe(file);
110 r.on('error', function (err) {
111 console.log(err);
112 callback(err);
113 });
114 r.on('finish', function () {
115 file.close();
116 let zipFile = fs.readFileSync(filePath, 'binary');
117 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
118 if (zip.files['MANIFEST.json']) {
119 let manifestData = zip.files['MANIFEST.json']._data;
120 manifestData = manifestData.replace(/\\n/g, '');
121 context.responseData = JSON.parse(manifestData);
122 }
123 callback();
124 });
125
126};
127
128function _random() {
129 let d = new Date();
130 return d.getTime().toString().split('').reverse().join('');
131}
132
133function _getJSONFromFile(file) {
134 return JSON.parse(fs.readFileSync(file, 'utf8'));
135}
136
137
138module.exports = {
139 request: _request,
140 random : _random,
141 getJSONFromFile: _getJSONFromFile,
142 download: download
143};