Skip to content

Commit 393716c

Browse files
Merge pull request #14 from Shahnawaz-Sk/master
replacing all request api calls by axios
2 parents 162eb0f + d3dc00e commit 393716c

File tree

5 files changed

+531
-161
lines changed

5 files changed

+531
-161
lines changed

index.js

Lines changed: 3 additions & 1 deletion
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,
4-
};
5+
AutomationClientV2: lambdaAutomationClientV2.AutomationClient,
6+
};

lib/api_client_v2.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
logger.error("Error ", error);
57+
throw error;
58+
}
59+
};
60+
61+
};
62+
module.exports = ApiClient;
63+

lib/automation_client_v2.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const ApiClient = require("./api_client_v2");
2+
3+
function ApiClientInstance(settings) {
4+
// Create an instance of ApiClient
5+
const apiClient = new ApiClient(settings);
6+
7+
this.fetchBuilds = async function(params) {
8+
return await apiClient.makeRequest('GET', '/builds', null, params);
9+
};
10+
11+
this.fetchBuildById = async function(buildId) {
12+
if(!buildId) {
13+
throw new Error('buildId is not found');
14+
}
15+
return await apiClient.makeRequest('GET', `/builds/${buildId}`);
16+
};
17+
18+
this.deleteBuildById = async function(buildId) {
19+
if(!buildId) {
20+
throw new Error('buildId is not found');
21+
}
22+
return await apiClient.makeRequest('DELETE', `/builds/${buildId}`);
23+
};
24+
25+
this.updateBuildById = async function(buildId, requestBody) {
26+
if(!buildId || !requestBody) {
27+
throw new Error('buildId || requestBody is not found');
28+
}
29+
return await apiClient.makeRequest('PATCH', `/builds/${buildId}`, requestBody);
30+
};
31+
32+
this.fetchSessions = async function(params) {
33+
return await apiClient.makeRequest('GET', '/sessions', null, params);
34+
};
35+
36+
this.fetchSessionById = async function(sessionId) {
37+
if(!sessionId) {
38+
throw new Error('session Id not correct');
39+
}
40+
return await apiClient.makeRequest('GET', `/sessions/${sessionId}`);
41+
};
42+
43+
this.deleteSessionById = async function(sessionId) {
44+
if(!sessionId) {
45+
throw new Error('session Id not correct');
46+
}
47+
return await apiClient.makeRequest('DELETE', `/sessions/${sessionId}`);
48+
};
49+
50+
this.updateSessionById = async function(sessionId, requestBody) {
51+
if(!sessionId || !requestBody) {
52+
throw new Error('session Id || requestBody not correct');
53+
}
54+
return await apiClient.makeRequest('PATCH', `/sessions/${sessionId}`, requestBody);
55+
};
56+
57+
this.fetchSessionScreenshot = async function(sessionId) {
58+
if(!sessionId) {
59+
throw new Error('session Id not correct');
60+
}
61+
return await apiClient.makeRequest('GET', `/sessions/${sessionId}/screenshots`);
62+
};
63+
64+
this.fetchSessionVideo = async function(sessionId) {
65+
if(!sessionId) {
66+
throw new Error('session Id not correct');
67+
}
68+
return await apiClient.makeRequest( 'GET', `/sessions/${sessionId}/video`);
69+
};
70+
71+
this.fetchSessionCommandLogs = async function(sessionId) {
72+
if(!sessionId) {
73+
throw new Error('session Id not correct');
74+
}
75+
return await apiClient.makeRequest('GET', `/sessions/${sessionId}/log/command`);
76+
};
77+
78+
this.fetchSessionSeleniumLogs = async function(sessionId) {
79+
if(!sessionId) {
80+
throw new Error('session Id not correct');
81+
}
82+
return await apiClient.makeRequest( 'GET', `/sessions/${sessionId}/log/selenium`);
83+
};
84+
85+
this.fetchSessionNetworkLogs = async function(sessionId) {
86+
if(!sessionId) {
87+
throw new Error('session Id not correct');
88+
}
89+
return await apiClient.makeRequest('GET', `/sessions/${sessionId}/log/network`);
90+
};
91+
92+
this.fetchSessionConsoleLogs = async function(sessionId) {
93+
if(!sessionId) {
94+
throw new Error('session Id not correct');
95+
}
96+
return await apiClient.makeRequest('GET', `/sessions/${sessionId}/log/console`);
97+
};
98+
99+
this.fetchTunnels= async function() {
100+
return await apiClient.makeRequest('GET', '/tunnels');
101+
};
102+
103+
this.deleteTunnelById = async function(tunnelId) {
104+
if(!tunnelId) {
105+
throw new Error('tunnel Id not correct');
106+
}
107+
return await apiClient.makeRequest('DELETE', `/tunnels/${tunnelId}`);
108+
};
109+
110+
this.fetchPlatforms= async function() {
111+
return await apiClient.makeRequest('GET', '/platforms');
112+
};
113+
114+
}
115+
116+
module.exports = {
117+
AutomationClient: function (settings) {
118+
return new ApiClientInstance(settings);
119+
},
120+
};

0 commit comments

Comments
 (0)