ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | const request = require('request'); |
| 17 | const fs = require('fs'); |
| 18 | require('node-zip'); |
| 19 | |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame^] | 20 | function _request(context, method, path, data, isBinary=false, type='onboarding') { |
| 21 | let server = context.getUrlForType(type); |
ilanap | 6c87274 | 2018-02-18 11:02:03 +0200 | [diff] [blame] | 22 | |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 23 | 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) { |
ayalaben | aad2781 | 2018-02-18 10:03:22 +0200 | [diff] [blame] | 57 | if (typeof data === 'string' && data) { |
| 58 | data = JSON.parse(data); |
| 59 | } |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 60 | 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 | } |
ayalaben | aad2781 | 2018-02-18 10:03:22 +0200 | [diff] [blame] | 67 | 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 | } |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 78 | 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 | |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame^] | 102 | function download(context, path, filePath, callback, type='onboarding') { |
| 103 | let server = context.getUrlForType(type); |
| 104 | let options = { |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 105 | method: 'GET', |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame^] | 106 | url: server + path, |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 107 | headers: context.headers |
| 108 | }; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame^] | 109 | var file = fs.createWriteStream(filePath); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 110 | 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 | |
| 129 | function _random() { |
| 130 | let d = new Date(); |
| 131 | return d.getTime().toString().split('').reverse().join(''); |
| 132 | } |
| 133 | |
| 134 | function _getJSONFromFile(file) { |
| 135 | return JSON.parse(fs.readFileSync(file, 'utf8')); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | module.exports = { |
| 140 | request: _request, |
| 141 | random : _random, |
| 142 | getJSONFromFile: _getJSONFromFile, |
| 143 | download: download |
| 144 | }; |