Skip to content

Commit 77a3585

Browse files
committed
exposure of two client instances
1 parent ba30bf3 commit 77a3585

File tree

7 files changed

+1293
-126
lines changed

7 files changed

+1293
-126
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var lambdaAutomationClient = require("./lib/automation_client");
2+
var lambdaAutomationClientV2 = require("./lib/automation_client_v2");
23
module.exports = {
34
AutomationClient: lambdaAutomationClient.AutomationClient,
5+
AutomationClientV2: lambdaAutomationClientV2.AutomationClient,
46
};

lib/api_client.js

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

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+
*/
611
var ApiClient = function (settings) {
712
settings = settings || {};
813
/**
@@ -23,40 +28,58 @@ var ApiClient = function (settings) {
2328
logger = require("./logger")(settings.logFile);
2429
}
2530
logger.info("Imported ApiClient and User Credenetials", settings);
26-
ApiClient.basicAuth = "Basic " + Buffer.from(settings.username + ":" + settings.accessKey).toString("base64");
31+
ApiClient.authHeader =
32+
"Basic " + Buffer.from(settings.username + ":" + settings.accessKey).toString("base64");
33+
ApiClient.settings = settings;
2734

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

30-
this.makeRequest = async function(method, path, body = null, params = {}) {
31-
// Validate path
32-
if (!path) {
33-
throw new Error('Path is required');
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;
3464
}
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-
65+
/** For Debbuging purpose log Request Payload */
5066
logger.info("Api request options ", options);
51-
// Axios request
52-
try {
53-
const response = await axios(options);
54-
return response.data;
55-
} catch (error) {
56-
throw error;
57-
}
58-
};
59-
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+
}
6083
};
61-
module.exports = ApiClient;
6284

85+
module.exports = ApiClient;

lib/api_client_v2.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const axios = require('axios');
2+
const { version, appVersion, baseUrl, baseUrlApp } = require("./config");
3+
const packageVersion = require("../package.json").version;
4+
let logger;
5+
6+
var ApiClient = function (settings) {
7+
settings = settings || {};
8+
/**
9+
* Required check for username.
10+
* @return {Error|null}
11+
*/
12+
if (!settings.username) {
13+
throw new Error("Username is required.");
14+
}
15+
/**
16+
* Required check for accessKey.
17+
* @return {Error|null}
18+
*/
19+
if (!settings.accessKey) {
20+
throw new Error("Access Key is required.");
21+
}
22+
if (!logger) {
23+
logger = require("./logger")(settings.logFile);
24+
}
25+
logger.info("Imported ApiClient and User Credenetials", settings);
26+
ApiClient.basicAuth = "Basic " + Buffer.from(settings.username + ":" + settings.accessKey).toString("base64");
27+
28+
ApiClient.apiBaseUrl = settings.isApp ? baseUrlApp + appVersion.latestVersion : baseUrl + version.latestVersion;
29+
30+
this.makeRequest = async function(method, path, body = null, params = {}) {
31+
// Validate path
32+
if (!path) {
33+
throw new Error('Path is required');
34+
}
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+
50+
logger.info("Api request options ", options);
51+
// Axios request
52+
try {
53+
const response = await axios(options);
54+
return response.data;
55+
} catch (error) {
56+
throw error;
57+
}
58+
};
59+
60+
};
61+
module.exports = ApiClient;
62+

0 commit comments

Comments
 (0)