|
1 | | -var request = require("request"), |
| 1 | +var axios = require("axios"), |
2 | 2 | logger, |
3 | 3 | packageVersion = require("../package.json").version; |
4 | 4 |
|
@@ -84,21 +84,30 @@ ApiClient.request = function (options, fnCallback) { |
84 | 84 |
|
85 | 85 | /** For Debbuging purpose log Request Payload */ |
86 | 86 | logger.info("Api request options", options); |
87 | | - request(options, function (e, response, body) { |
88 | | - if (e) { |
89 | | - logger.error("Error while Api call", e); |
90 | | - } else if (response.statusCode === 200) { |
91 | | - // use try-catch Error possible due to json parse of non-parseable |
92 | | - try { |
93 | | - body = JSON.parse(body); |
94 | | - logger.info("Api response json :", body); |
95 | | - return fnCallback(e, body); |
96 | | - } catch (e) { |
97 | | - logger.error("Error while parsing to json of output response", e); |
| 87 | + |
| 88 | + // Convert request options to axios format |
| 89 | + const axiosOptions = { |
| 90 | + method: options.method || 'GET', |
| 91 | + url: options.url, |
| 92 | + headers: options.headers, |
| 93 | + params: options.qs, |
| 94 | + data: options.body |
| 95 | + }; |
| 96 | + |
| 97 | + axios(axiosOptions) |
| 98 | + .then(function (response) { |
| 99 | + logger.info("Api response json :", response.data); |
| 100 | + return fnCallback(null, response.data); |
| 101 | + }) |
| 102 | + .catch(function (error) { |
| 103 | + if (error.response) { |
| 104 | + logger.error("Error while Api call", error.response.data); |
| 105 | + return fnCallback(new Error(JSON.stringify(error.response.data)), null); |
| 106 | + } else { |
| 107 | + logger.error("Error while Api call", error.message); |
| 108 | + return fnCallback(error, null); |
98 | 109 | } |
99 | | - } |
100 | | - return fnCallback(new Error(e || body), null); |
101 | | - }); |
| 110 | + }); |
102 | 111 | } |
103 | 112 | }; |
104 | 113 |
|
|
0 commit comments