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) { |
| 56 | let errorCode = data.errorCode; |
| 57 | let contextErrorCode = context.errorCode; |
| 58 | context.errorCode = null; |
| 59 | if (errorCode !== contextErrorCode) { |
| 60 | reject('Error Code was ' + errorCode + ' instead of ' + contextErrorCode); |
| 61 | } |
| 62 | } |
| 63 | if (context.shouldFail) { |
| 64 | context.shouldFail = false; |
| 65 | resolve({statusCode: result.statusCode, data: {}}); |
| 66 | return; |
| 67 | } |
| 68 | if (method === 'GET' && isBinary) { |
| 69 | // downloading (NetworkPackage) files |
| 70 | return ({ |
| 71 | blob: blobUtil.createBlob([data], {type: 'text/plain'}), |
| 72 | headers: result.headers |
| 73 | }); |
| 74 | } else { |
| 75 | if (typeof data === 'string' && data) { |
| 76 | data = JSON.parse(data); |
| 77 | } |
| 78 | context.responseData = data; |
| 79 | context.inputData = data; |
| 80 | resolve({statusCode: result.statusCode, data: data}); |
| 81 | } |
| 82 | } |
| 83 | }); |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | function download(context, path, filePath, callback){ |
| 88 | let options = { |
| 89 | method: 'GET', |
| 90 | url: context.onboarding_server + path, |
| 91 | headers: context.headers |
| 92 | }; |
| 93 | var file = fs.createWriteStream(filePath); |
| 94 | var r = request(options).pipe(file); |
| 95 | r.on('error', function (err) { |
| 96 | console.log(err); |
| 97 | callback(err); |
| 98 | }); |
| 99 | r.on('finish', function () { |
| 100 | file.close(); |
| 101 | let zipFile = fs.readFileSync(filePath, 'binary'); |
| 102 | let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); |
| 103 | if (zip.files['MANIFEST.json']) { |
| 104 | let manifestData = zip.files['MANIFEST.json']._data; |
| 105 | manifestData = manifestData.replace(/\\n/g, ''); |
| 106 | context.responseData = JSON.parse(manifestData); |
| 107 | } |
| 108 | callback(); |
| 109 | }); |
| 110 | |
| 111 | }; |
| 112 | |
| 113 | function _random() { |
| 114 | let d = new Date(); |
| 115 | return d.getTime().toString().split('').reverse().join(''); |
| 116 | } |
| 117 | |
| 118 | function _getJSONFromFile(file) { |
| 119 | return JSON.parse(fs.readFileSync(file, 'utf8')); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | module.exports = { |
| 124 | request: _request, |
| 125 | random : _random, |
| 126 | getJSONFromFile: _getJSONFromFile, |
| 127 | download: download |
| 128 | }; |