Skip to content

Commit

Permalink
Merge pull request #18 from Shahnawaz-Sk/master
Browse files Browse the repository at this point in the history
changes for eu/us config
  • Loading branch information
Shivanshu-lambdatest authored Jan 30, 2024
2 parents 393716c + 0dd48b4 commit 10a7db7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 22 deletions.
42 changes: 31 additions & 11 deletions lib/api_client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { version, appVersion, baseUrl, baseUrlApp } = require("./config");

var request = require("request"),
logger,
packageVersion = require("../package.json").version;
Expand All @@ -10,30 +8,47 @@ var request = require("request"),
*/
var ApiClient = function (settings) {
settings = settings || {};

/**
* Region validation
* @return {Error|null}
*/
const region = (settings.region || "us").toLowerCase();
if (!["us", "eu"].includes(region)) {
throw new Error("Region is invalid.");
}

// Load region-specific config and attach it to ApiClient
ApiClient.config = require(`./config.${region}`);

/**
* Required check for username.
* @return {Error|null}
*/
if (!settings.username) {
throw new Error("Username is required.");
}

/**
* Required check for accessKey.
* @return {Error|null}
*/
if (!settings.accessKey) {
throw new Error("Access Key is required.");
}

if (!logger) {
logger = require("./logger")(settings.logFile);
}
logger.info("Imported ApiClient and User Credenetials", settings);

logger.info("Imported ApiClient and User Credentials", settings);
ApiClient.authHeader =
"Basic " + Buffer.from(settings.username + ":" + settings.accessKey).toString("base64");
ApiClient.settings = settings;

// Set base URL depending on the app flag
if (settings.isApp) {
ApiClient.baseUrl = baseUrlApp + appVersion.latestVersion;
ApiClient.baseUrl = ApiClient.config.baseUrlApp + ApiClient.config.appVersion.latestVersion;
}
};

Expand All @@ -44,9 +59,13 @@ var ApiClient = function (settings) {
* @return {Function|Error} return response to Callable method or throw Error.
*/
ApiClient.request = function (options, fnCallback) {
/** Initialize Callback function is not there */
/** Initialize Callback function is not there */
fnCallback = fnCallback || function () {};
/** Check baseUrl is on ApiClient */

// Access the configuration from ApiClient
const { baseUrl, version } = ApiClient.config;

// Check and set the baseUrl if it's undefined
if (ApiClient.baseUrl === undefined) {
ApiClient.baseUrl = baseUrl + version.latestVersion;
ApiClient.request(options, fnCallback);
Expand All @@ -62,19 +81,20 @@ ApiClient.request = function (options, fnCallback) {
};
options.url = ApiClient.baseUrl + options.url;
}
/** For Debbuging purpose log Request Payload */
logger.info("Api request options ", options);

/** For Debbuging purpose log Request Payload */
logger.info("Api request options", options);
request(options, function (e, response, body) {
if (e) {
logger.error("Error while Api call of ", e);
logger.error("Error while Api call", e);
} else if (response.statusCode === 200) {
// use try-catch Error possible due to json parse of non-parseable
try {
body = JSON.parse(body);
logger.info("Api response json : ", body);
logger.info("Api response json :", body);
return fnCallback(e, body);
} catch (e) {
logger.error("Error while parse to json of output response ", e);
logger.error("Error while parsing to json of output response", e);
}
}
return fnCallback(new Error(e || body), null);
Expand Down
14 changes: 13 additions & 1 deletion lib/api_client_v2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const axios = require('axios');
const { version, appVersion, baseUrl, baseUrlApp } = require("./config");
const packageVersion = require("../package.json").version;
let logger;

Expand All @@ -22,6 +21,19 @@ var ApiClient = function (settings) {
if (!logger) {
logger = require("./logger")(settings.logFile);
}

const region = (settings.region || "us").toLowerCase();

/**
* Region validation
* @return {Error|null}
*/
if (!["us", "eu"].includes(region)) {
throw new Error("Region is invalid.");
}

const { version, appVersion, baseUrl, baseUrlApp } = require(`./config.${region}`);

logger.info("Imported ApiClient and User Credenetials", settings);
ApiClient.basicAuth = "Basic " + Buffer.from(settings.username + ":" + settings.accessKey).toString("base64");

Expand Down
6 changes: 6 additions & 0 deletions lib/config.eu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
version: { latestVersion: "v1", supportedVersions: ["v1"] },
appVersion: { latestVersion: "v1", supportedVersions: ["v1"] },
baseUrl: "https://eu-api.lambdatest.com/automation/api/",
baseUrlApp: "https://eu-mobile-api.lambdatest.com/mobile-automation/api/",
};
File renamed without changes.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lambdatest/node-rest-client",
"version": "2.0.0",
"version": "2.0.1",
"description": "LambdaTest Rest Client for Node.js",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit 10a7db7

Please sign in to comment.