Skip to content

Commit ba30bf3

Browse files
committed
replacing all request api call by axios
1 parent 162eb0f commit ba30bf3

File tree

5 files changed

+310
-1108
lines changed

5 files changed

+310
-1108
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var lambdaAutomationClient = require("./lib/automation_client");
22
module.exports = {
33
AutomationClient: lambdaAutomationClient.AutomationClient,
4-
};
4+
};

lib/api_client.js

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1+
const axios = require('axios');
12
const { version, appVersion, baseUrl, baseUrlApp } = require("./config");
3+
const packageVersion = require("../package.json").version;
4+
let logger;
25

3-
var request = require("request"),
4-
logger,
5-
packageVersion = require("../package.json").version;
6-
7-
/**
8-
* ApiClient is a function based Class.
9-
* @param {!AutomationApiClient} settings An object that for lambda user credentials
10-
*/
116
var ApiClient = function (settings) {
127
settings = settings || {};
138
/**
@@ -28,58 +23,40 @@ var ApiClient = function (settings) {
2823
logger = require("./logger")(settings.logFile);
2924
}
3025
logger.info("Imported ApiClient and User Credenetials", settings);
31-
ApiClient.authHeader =
32-
"Basic " + Buffer.from(settings.username + ":" + settings.accessKey).toString("base64");
33-
ApiClient.settings = settings;
26+
ApiClient.basicAuth = "Basic " + Buffer.from(settings.username + ":" + settings.accessKey).toString("base64");
3427

35-
if (settings.isApp) {
36-
ApiClient.baseUrl = baseUrlApp + appVersion.latestVersion;
37-
}
38-
};
28+
ApiClient.apiBaseUrl = settings.isApp ? baseUrlApp + appVersion.latestVersion : baseUrl + version.latestVersion;
3929

40-
/**
41-
* Extends ApiClient Class with static method for all Api Request.
42-
* @param {!ApiClient} options An object that determine the Request payload
43-
* @param {Function} fnCallback is callback function.
44-
* @return {Function|Error} return response to Callable method or throw Error.
45-
*/
46-
ApiClient.request = function (options, fnCallback) {
47-
/** Initialize Callback function is not there */
48-
fnCallback = fnCallback || function () {};
49-
/** Check baseUrl is on ApiClient */
50-
if (ApiClient.baseUrl === undefined) {
51-
ApiClient.baseUrl = baseUrl + version.latestVersion;
52-
ApiClient.request(options, fnCallback);
53-
} else {
54-
/** Update Options with User Credential If It is object */
55-
if (typeof options === "object") {
56-
options.headers = {
57-
Authorization: ApiClient.authHeader,
58-
"Content-Type": "application/json",
59-
Accept: "application/json",
60-
client: "npm-rest-client",
61-
version: packageVersion,
62-
};
63-
options.url = ApiClient.baseUrl + options.url;
30+
this.makeRequest = async function(method, path, body = null, params = {}) {
31+
// Validate path
32+
if (!path) {
33+
throw new Error('Path is required');
6434
}
65-
/** For Debbuging purpose log Request Payload */
35+
36+
// Use instance-specific baseUrl
37+
const url = `${ApiClient.apiBaseUrl}${path}`;
38+
39+
// Prepare request options
40+
const options = {
41+
method: method,
42+
headers: { Authorization: ApiClient.basicAuth },
43+
url: url,
44+
};
45+
46+
47+
if (body) options.data = JSON.stringify(body);
48+
if (Object.keys(params).length) options.params = params;
49+
6650
logger.info("Api request options ", options);
67-
request(options, function (e, response, body) {
68-
if (e) {
69-
logger.error("Error while Api call of ", e);
70-
} else if (response.statusCode === 200) {
71-
// use try-catch Error possible due to json parse of non-parseable
72-
try {
73-
body = JSON.parse(body);
74-
logger.info("Api response json : ", body);
75-
return fnCallback(e, body);
76-
} catch (e) {
77-
logger.error("Error while parse to json of output response ", e);
78-
}
79-
}
80-
return fnCallback(new Error(e || body), null);
81-
});
82-
}
83-
};
51+
// Axios request
52+
try {
53+
const response = await axios(options);
54+
return response.data;
55+
} catch (error) {
56+
throw error;
57+
}
58+
};
8459

60+
};
8561
module.exports = ApiClient;
62+

0 commit comments

Comments
 (0)