ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [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 needle = require('needle'); |
| 17 | const fs = require('fs'); |
| 18 | require('node-zip'); |
| 19 | var btoa = require('btoa'); |
| 20 | const md5 = require('md5'); |
| 21 | const _ = require('lodash'); |
| 22 | |
| 23 | function getOptionsForRequest(context, method, path, type) { |
| 24 | if (type == undefined || type == null) { |
| 25 | type = context.defaultServerType |
| 26 | } |
| 27 | let server = context.getUrlForType(type); |
| 28 | let options = { |
| 29 | method: method, |
| 30 | url: server + path, |
| 31 | headers: _.clone(context.headers[type]) |
| 32 | }; |
| 33 | // options.headers["Content-Type"] = "application/json"; |
| 34 | // options.headers["accept"] = "application/json"; |
| 35 | return options; |
| 36 | } |
| 37 | |
| 38 | function _requestBinaryFormData(context, method, path, fileName, formInputName, type) { |
| 39 | let options = getOptionsForRequest(context, method, path, type); |
| 40 | let formData = {}; |
| 41 | if (method === 'POST' || method === 'PUT') { |
| 42 | //formData[formInputName] = fs.createReadStream(fileName); |
| 43 | //options.formData = formData; |
| 44 | let fileData = { |
| 45 | file: fileName |
| 46 | }; |
| 47 | fileData['content_type'] = 'multipart/form-data'; |
| 48 | options.formData = {}; |
| 49 | options.formData[formInputName] = fileData; |
| 50 | } |
| 51 | return _request(context, method, path, options); |
| 52 | } |
| 53 | function _requestBinaryBody(context, method, path, fileName, type) { |
| 54 | let options = getOptionsForRequest(context, method, path, type); |
| 55 | if (method === 'POST' || method === 'PUT') { |
| 56 | options.body = fs.createReadStream(fileName); |
| 57 | options.headers['Content-Type'] = 'application/octet-stream'; |
| 58 | |
| 59 | } |
| 60 | return _request(context, method, path, options); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | function _requestPayload(context, method, path, filePath, type) { |
| 65 | let options = getOptionsForRequest(context, method, path, type); |
| 66 | options.json = _createPayload(filePath); |
| 67 | options.headers['Content-MD5'] = addCheckSum(options.json); |
| 68 | return _request(context, method, path, options); |
| 69 | } |
| 70 | |
| 71 | function _requestRest(context, method, path, data, type) { |
| 72 | let options = getOptionsForRequest(context, method, path, type); |
| 73 | if (method === 'POST' || method === 'PUT') { |
| 74 | options.json = data; |
| 75 | } |
| 76 | return _request(context, method, path, options); |
| 77 | } |
| 78 | |
ilanap | 5f06322 | 2019-08-05 14:10:34 +0300 | [diff] [blame] | 79 | |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 80 | function _request(context, method, path, options) { |
| 81 | console.log('--> Calling REST ' + options.method +' url: ' + options.url); |
| 82 | let inputData = options.json; |
| 83 | let needleOptions = {headers: options.headers, rejectUnauthorized: false}; |
| 84 | if (inputData == undefined) { |
| 85 | if (options.formData != undefined) { |
| 86 | inputData = options.formData; |
| 87 | needleOptions.multipart = true; |
| 88 | } |
| 89 | if (inputData && inputData.body != undefined) { |
| 90 | inputData = options.body; |
| 91 | } |
| 92 | } else { |
| 93 | needleOptions.json = true; |
| 94 | } |
| 95 | return needle(method, options.url, inputData, needleOptions) |
| 96 | .then(function(result) { |
| 97 | context.inputData = null; |
ilanap | 5f06322 | 2019-08-05 14:10:34 +0300 | [diff] [blame] | 98 | let successResult = result.statusCode >= 200 && result.statusCode < 300; |
| 99 | let isExpected = context.shouldFail ? !successResult : successResult; |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 100 | data = result.body; |
| 101 | if (!isExpected) { |
| 102 | console.log('Did not get expected response code'); |
| 103 | throw 'Status Code was ' + result.statusCode ; |
| 104 | } |
ilanap | 5f06322 | 2019-08-05 14:10:34 +0300 | [diff] [blame] | 105 | if (context.statusCode) { |
| 106 | let expectedStatusCode = context.statusCode; |
| 107 | context.statusCode = null; |
| 108 | if (result.statusCode !== expectedStatusCode) { |
| 109 | throw 'Response Status Code was ' + result.statusCode + ' instead of ' + expectedStatusCode; |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | if (context.shouldFail) { |
| 113 | context.shouldFail = false; |
ilanap | 5f06322 | 2019-08-05 14:10:34 +0300 | [diff] [blame] | 114 | if (context.errorCode) { |
| 115 | if (typeof data === 'string' && data) { |
| 116 | data = JSON.parse(data); |
| 117 | } |
| 118 | let contextErrorCode = context.errorCode; |
| 119 | let errorCode = data.errorCode; |
| 120 | context.errorCode = null; |
| 121 | if (errorCode !== contextErrorCode) { |
| 122 | throw 'Error Code was ' + errorCode + ' instead of ' + contextErrorCode; |
| 123 | } |
| 124 | } |
| 125 | if (context.errorMessage) { |
| 126 | if (typeof data === 'string' && data) { |
| 127 | data = JSON.parse(data); |
| 128 | } |
| 129 | let errorMessage = data.message; |
| 130 | let contextErrorMessage = context.errorMessage; |
| 131 | context.errorMessage = null; |
| 132 | if (errorMessage !== contextErrorMessage) { |
| 133 | throw 'Error Message was ' + errorMessage + ' instead of ' + contextErrorMessage; |
| 134 | } |
| 135 | } |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 136 | return({statusCode: result.statusCode, data: {}}); |
| 137 | } |
| 138 | |
| 139 | if (typeof data === 'string' && data) { |
| 140 | if (data.startsWith('[') || data.startsWith('{')) { |
| 141 | data = JSON.parse(data); |
| 142 | } |
| 143 | } |
| 144 | context.responseData = data; |
| 145 | context.inputData = data; |
| 146 | return({statusCode: result.statusCode, data: data}); |
| 147 | |
| 148 | }) |
| 149 | .catch(function(err) { |
| 150 | console.error('Request URL: ' + options.url); |
| 151 | console.error('Request Method: ' + options.method); |
| 152 | console.log(err); |
| 153 | throw err; |
| 154 | }) |
| 155 | } |
| 156 | |
| 157 | function download(context, path, filePath, type) { |
| 158 | if (type == undefined || type == null) { |
| 159 | type = context.defaultServerType |
| 160 | } |
| 161 | let server = context.getUrlForType(type); |
| 162 | let options = { |
| 163 | method: 'GET', |
| 164 | url: server + path, |
| 165 | headers: context.headers[type] |
| 166 | }; |
| 167 | |
| 168 | console.log('--> Calling REST download url: ' + options.url); |
| 169 | return needle('GET', options.url, {}, { |
| 170 | headers: options.headers, |
| 171 | rejectUnauthorized: false, |
| 172 | output: filePath |
| 173 | }) |
| 174 | .then(function (result) { |
| 175 | let zipFile = fs.readFileSync(filePath, 'binary'); |
| 176 | let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); |
| 177 | if (zip.files['MANIFEST.json']) { |
| 178 | let manifestData = zip.files['MANIFEST.json']._data; |
| 179 | manifestData = manifestData.replace(/\\n/g, ''); |
| 180 | context.responseData = JSON.parse(manifestData); |
| 181 | } |
| 182 | return zip; |
| 183 | }) |
| 184 | .catch(function (err) { |
| 185 | console.error('Request URL: ' + options.url); |
| 186 | console.error('Request Method: ' + options.method); |
| 187 | throw err; |
| 188 | }) |
| 189 | } |
| 190 | |
| 191 | function _random() { |
| 192 | let d = new Date(); |
| 193 | return d.getTime().toString().split('').reverse().join(''); |
| 194 | } |
| 195 | |
| 196 | function _getJSONFromFile(file) { |
| 197 | return JSON.parse(fs.readFileSync(file, 'utf8')); |
| 198 | } |
| 199 | |
| 200 | function _createPayload(fileName) { |
| 201 | var body = fs.readFileSync(fileName); |
| 202 | let payload = { |
| 203 | payloadData: body.toString('base64'), |
| 204 | payloadName: fileName.substring(fileName.lastIndexOf("/") + 1 ) |
| 205 | }; |
| 206 | return payload; |
| 207 | } |
| 208 | |
| 209 | function addCheckSum(payloadData) { |
| 210 | let _md5 = md5(JSON.stringify(payloadData)); |
| 211 | return btoa(_md5.toLowerCase()); |
| 212 | } |
| 213 | |
| 214 | function _getFile(file, format) { |
| 215 | if(format === '' ){ |
| 216 | return fs.readFileSync(file) |
| 217 | } |
| 218 | return fs.readFileSync(file, format); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | module.exports = { |
| 223 | getFile: _getFile, |
| 224 | request: _requestRest, |
| 225 | requestPayload: _requestPayload, |
| 226 | requestBinaryFormData: _requestBinaryFormData, |
| 227 | requestBinaryBody: _requestBinaryBody, |
| 228 | random : _random, |
| 229 | getJSONFromFile: _getJSONFromFile, |
| 230 | download: download, |
| 231 | payload: _createPayload |
| 232 | }; |