Skip to content

Commit e8d8bc8

Browse files
authored
Merge pull request #20 from meraj-lt/IN-11617
replace 'request' with 'axios' for API calls in api_client.js
2 parents 10a7db7 + 10b1325 commit e8d8bc8

File tree

3 files changed

+41
-397
lines changed

3 files changed

+41
-397
lines changed

lib/api_client.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var request = require("request"),
1+
var axios = require("axios"),
22
logger,
33
packageVersion = require("../package.json").version;
44

@@ -84,21 +84,30 @@ ApiClient.request = function (options, fnCallback) {
8484

8585
/** For Debbuging purpose log Request Payload */
8686
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);
98109
}
99-
}
100-
return fnCallback(new Error(e || body), null);
101-
});
110+
});
102111
}
103112
};
104113

0 commit comments

Comments
 (0)