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