Skip to content

Commit b61c899

Browse files
committed
migrated request library to axios
1 parent fd5fd0e commit b61c899

File tree

8 files changed

+389
-221
lines changed

8 files changed

+389
-221
lines changed

commands/build_info.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45

@@ -40,6 +41,7 @@ function get_build_info(args) {
4041
}
4142
}
4243
let options = {
44+
method: 'get',
4345
url: constants[env].BUILD_BASE_URL + args.buildId,
4446
auth: {
4547
username: username,
@@ -55,25 +57,38 @@ function get_build_info(args) {
5557
return;
5658
} else {
5759
if (args["reject_unauthorized"] == "false") {
58-
options["rejectUnauthorized"] = false;
60+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
5961
console.log("Setting rejectUnauthorized to false for web requests");
6062
}
6163
}
6264
}
6365

64-
request.get(options, (err, res, body) => {
65-
if (err) {
66-
reject(err);
66+
axios(options)
67+
.then(response => {
68+
if (response.data.status == "success") {
69+
resolve(response.data.data);
6770
} else {
68-
if (res.statusCode == "401") {
71+
resolve(response.data.message);
72+
}
73+
})
74+
.catch(error => {
75+
if (error.response) {
76+
// The request was made and the server responded with a status code
77+
// that falls out of the range of 2xx
78+
if (error.response.status == 401) {
6979
resolve("Unauthorized");
70-
} else if (JSON.parse(body).status == "success") {
71-
resolve(JSON.parse(body).data);
7280
} else {
73-
resolve(JSON.parse(body).message);
81+
console.log(error.response.data);
7482
}
83+
} else if (error.request) {
84+
// The request was made but no response was received
85+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
86+
// http.ClientRequest in node.js
87+
console.log(error.cause);
88+
} else {
89+
reject(error);
7590
}
76-
});
91+
})
7792
});
7893
}
7994

commands/build_stop.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45
const fs = require("fs");
@@ -66,6 +67,7 @@ function stop_session(args) {
6667
}
6768

6869
let options = {
70+
method: 'put',
6971
url: constants[env].BUILD_STOP_URL + args.session_id,
7072
headers: {
7173
Authorization: "Token " + access_key,
@@ -82,39 +84,38 @@ function stop_session(args) {
8284
return;
8385
} else {
8486
if (args["reject_unauthorized"] == "false") {
85-
options["rejectUnauthorized"] = false;
87+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
8688
console.log("Setting rejectUnauthorized to false for web requests");
8789
}
8890
}
8991
}
90-
request.put(options, function (err, resp, body) {
91-
if (err) {
92-
reject(err);
92+
93+
axios(options)
94+
.then(response => {
95+
if(response.data.length == 0){
96+
resolve("No tests to stop in session " + args.session_id);
9397
} else {
94-
try {
95-
responseData = JSON.parse(body);
96-
} catch (e) {
97-
console.log("Error in JSON response", body);
98-
responseData = null;
99-
}
100-
if (resp.statusCode != 200) {
101-
if (responseData && responseData["error"]) {
102-
reject(responseData["error"]);
103-
} else {
104-
console.log(responseData);
105-
reject("error", responseData);
106-
}
107-
} else {
108-
if (responseData.length == 0) {
109-
resolve("No tests to stop in session " + args.session_id);
110-
}
111-
resolve(
112-
"Session Stopped successfully, No. of tests stopped are: " +
113-
responseData.length
114-
);
98+
resolve(
99+
"Session Stopped successfully, No. of tests stopped are: " +
100+
response.data.length
101+
);
102+
}
103+
})
104+
.catch(error => {
105+
if (error.response != null) {
106+
if (error.response.status != 200) {
107+
reject(error.response.data)
115108
}
109+
} else if (error.request) {
110+
// The request was made but no response was received
111+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
112+
reject(error.cause);
113+
} else {
114+
reject(error);
116115
}
117-
});
116+
})
117+
118+
118119
});
119120
}
120121

commands/generate_reports.js

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45
const build_stats = require("./utils/poller/build_stats.js");
@@ -27,33 +28,34 @@ function download_artefact(
2728
const stream = fs.createWriteStream(file_path);
2829
stream.end();
2930
let options = {
31+
method: 'get',
3032
url: constants[env].REPORT_URL + test_id,
3133
auth: {
3234
username: username,
3335
password: access_key,
3436
},
3537
gzip: true,
3638
timeout: 120000,
39+
responseType: 'stream'
3740
};
3841
if (rejectUnauthorized == false) {
39-
options["rejectUnauthorized"] = false;
42+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
4043
console.log("Setting rejectUnauthorized to false for web requests");
4144
}
4245

43-
request(options, (err, res, body) => {
44-
if (err) {
45-
reject(err);
46-
}
47-
response_code = res.statusCode;
48-
resp = res
49-
}).pipe(
50-
fs
51-
.createWriteStream(file_path, {
46+
axios(options)
47+
.then((response) => {
48+
response_code = response.status;
49+
resp = response;
50+
response.data.pipe(
51+
fs.createWriteStream(file_path, {
5252
overwrite: true,
5353
})
54-
.on("finish", function () {
55-
if (response_code == 200) {
56-
const zip = new StreamZip({ file: file_path });
54+
);
55+
56+
response.data.on('end', function () {
57+
if (response_code == 200) {
58+
const zip = new StreamZip({ file: file_path });
5759
zip.on("ready", () => {
5860
zip.extract(null, old_path, (err, count) => {
5961
zip.close();
@@ -64,18 +66,37 @@ function download_artefact(
6466
: `Extracted ${count} entries for ` + test_id
6567
);
6668
});
67-
});
68-
} else {
69-
fs.unlinkSync(file_path);
70-
if (resp.body != null) {
71-
const responseObject = JSON.parse(resp.body);
72-
const dataValue = responseObject.data;
73-
reject("Could not download artefacts for test id " + test_id + " with reason " + dataValue);
74-
}
75-
reject("Could not download artefacts for test id " + test_id);
69+
})
70+
}
71+
});
72+
73+
})
74+
.catch((error) => {
75+
76+
if (error.response) {
77+
resp = error.response
78+
// The request was made and the server responded with a status code
79+
// that falls out of the range of 2xx
80+
if (error.response.status == 401) {
81+
resolve("Unauthorized");
82+
} else {
83+
fs.unlinkSync(file_path);
84+
if (resp.data != null) {
85+
const responseObject = resp.data;
86+
const dataValue = responseObject.data;
87+
reject("Could not download artefacts for test id " + test_id + " with reason " + dataValue);
7688
}
77-
})
78-
);
89+
reject("Could not download artefacts for test id " + test_id);
90+
}
91+
} else if (error.request) {
92+
console.log(error.cause);
93+
} else {
94+
reject(error);
95+
}
96+
97+
});
98+
99+
79100
});
80101
}
81102

@@ -233,6 +254,15 @@ function generate_report(args) {
233254
});
234255
}
235256

257+
function generate_report_command(args) {
258+
generate_report(args)
259+
.then(function (resp) {})
260+
.catch(function (err) {
261+
console.log("ERR:", err);
262+
});
263+
};
264+
236265
module.exports = {
237-
generate_report:generate_report
266+
generate_report:generate_report,
267+
generate_report_command:generate_report_command
238268
};

commands/utils/uploader.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
const https = require('https');
2+
const axios = require('axios');
13
const fs = require("fs");
2-
const request = require("request");
34
const constants = require("./constants.js");
5+
const { reject } = require('async');
46

57
function get_signed_url(lt_config, prefix, env = "prod") {
68
console.log("Getting project upload url");
79
return new Promise(function (resolve, reject) {
810
let options = {
11+
method: 'post',
912
url: constants[env].INTEGRATION_BASE_URL + constants.PROJECT_UPLOAD_URL,
1013
body: JSON.stringify({
1114
Username: lt_config["lambdatest_auth"]["username"],
@@ -15,31 +18,39 @@ function get_signed_url(lt_config, prefix, env = "prod") {
1518
};
1619

1720
if (lt_config.run_settings.reject_unauthorized == false) {
18-
options["rejectUnauthorized"] = false;
21+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
1922
}
20-
let responseData = null;
21-
request.post(options, function (err, resp, body) {
22-
if (err) {
23-
console.log(err);
24-
reject(err);
25-
} else {
26-
try {
27-
responseData = JSON.parse(body);
28-
} catch (e) {
29-
console.log("Error in JSON response", body);
30-
responseData = null;
31-
}
32-
if (resp.statusCode != 200 && resp.statusCode != 202) {
33-
if (responseData && responseData["error"]) {
34-
reject(responseData["error"]);
35-
} else {
36-
reject(responseData);
37-
}
23+
24+
axios(options)
25+
.then(response => {
26+
console.log("Got project upload url ");
27+
console.log(response);
28+
resolve(response.data);
29+
})
30+
.catch(error => {
31+
if (error.response) {
32+
// The request was made and the server responded with a status code
33+
// that falls out of the range of 2xx
34+
if (error.response.status != 200 && error.response.status != 202) {
35+
if (error.response && error.response.data) {
36+
reject(error.response.data);
3837
} else {
39-
resolve(responseData);
38+
reject(error.response);
4039
}
40+
} else {
41+
reject(error.response);
4142
}
42-
});
43+
} else if (error.request) {
44+
// The request was made but no response was received
45+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
46+
// http.ClientRequest in node.js
47+
console.log(error.cause);
48+
reject(error.cause);
49+
} else {
50+
console.log(error);
51+
reject(error);
52+
}
53+
})
4354
});
4455
}
4556

@@ -55,7 +66,7 @@ function upload_zip(lt_config, file_name, prefix = "project", env = "prod") {
5566
}
5667
get_signed_url(lt_config, prefix, env)
5768
.then(function (responseDataURL) {
58-
console.log("Uploading the project");
69+
console.log("Uploading the project " + responseDataURL);
5970
let options = {
6071
url: responseDataURL["value"]["message"],
6172
formData: {

0 commit comments

Comments
 (0)