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 | }; |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 28 | console.log('--> Calling REST ' + options.method +' url: ' + options.url); |
| 29 | |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 30 | 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) { |
ayalaben | aad2781 | 2018-02-18 10:03:22 +0200 | [diff] [blame] | 59 | if (typeof data === 'string' && data) { |
| 60 | data = JSON.parse(data); |
| 61 | } |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 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); |
| 67 | } |
| 68 | } |
ayalaben | aad2781 | 2018-02-18 10:03:22 +0200 | [diff] [blame] | 69 | 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 | } |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 80 | 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 | |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 104 | function download(context, path, filePath, callback, type='onboarding') { |
| 105 | let server = context.getUrlForType(type); |
| 106 | let options = { |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 107 | method: 'GET', |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 108 | url: server + path, |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 109 | headers: context.headers |
| 110 | }; |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 111 | console.log('--> Calling REST download url: ' + options.url); |
| 112 | |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 113 | var file = fs.createWriteStream(filePath); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 114 | 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 | |
| 133 | function _random() { |
| 134 | let d = new Date(); |
| 135 | return d.getTime().toString().split('').reverse().join(''); |
| 136 | } |
| 137 | |
| 138 | function _getJSONFromFile(file) { |
| 139 | return JSON.parse(fs.readFileSync(file, 'utf8')); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | module.exports = { |
| 144 | request: _request, |
| 145 | random : _random, |
| 146 | getJSONFromFile: _getJSONFromFile, |
| 147 | download: download |
| 148 | }; |