-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSideeXWebServiceClientAPI.js
97 lines (87 loc) · 3.01 KB
/
SideeXWebServiceClientAPI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require('fs');
const fetch = require('node-fetch');
const http = require('http');
const https = require('https');
const FormData = require('form-data');
const ProtocolType = {
HTTP: 0,
HTTPS_DISABLE: 1,
HTTPS_ENABLE: 2
}
class SideeXWebserviceClientAPI {
constructor(baseURL, protocolType = ProtocolType.HTTP, caFilePath = null) {
this.baseURL = baseURL;
this.protocolType = protocolType;
this.caFilePath = caFilePath;
this.keepAliveAgent = new http.Agent();
if (this.baseURL.charAt(this.baseURL.length - 1) != '/') {
this.baseURL = this.baseURL + "/";
}
if (this.protocolType == ProtocolType.HTTPS_DISABLE) {
this.keepAliveAgent = new https.Agent();
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
} else if (this.protocolType == ProtocolType.HTTPS_ENABLE) {
this.keepAliveAgent = new https.Agent({
ca: fs.readFileSync(this.caFilePath),
});
}
}
async echo() {
let response = await fetch(`${this.baseURL}sideex-webservice/echo`, {
method: 'GET',
agent: this.keepAliveAgent
})
// console.log(response.text())
return await response.text();
}
async runTestSuite(file) {
try {
fs.accessSync(file.path, fs.constants.F_OK | fs.constants.R_OK);
let formData = new FormData();
formData.append('file', file, {
contentType: 'application/zip',
filename: file.path,
knownLength: file.length
});
let response = await fetch(this.baseURL + "sideex-webservice/runTestSuites", {
method: 'POST',
body: formData,
agent: this.keepAliveAgent
})
return await response.text();
} catch (e) {
throw e;
}
}
async getState(token) {
let response = await fetch(`${this.baseURL}sideex-webservice/getState?token=${token}`, {
method: 'GET',
agent: this.keepAliveAgent
})
return await response.text();
}
async download(formData, filePath, option) {
let tempBaseURL = this.baseURL;
if (option == 0) {
tempBaseURL = tempBaseURL + "sideex-webservice/downloadReports";
} else {
tempBaseURL = tempBaseURL + "sideex-webservice/downloadLogs";
}
let response = await fetch(`${tempBaseURL}?token=${formData.token}&file=${formData.file}`, {
method: 'GET',
agent: this.keepAliveAgent
})
await response.body.pipe(fs.createWriteStream(filePath));
}
async deleteJob(token) {
let response = await fetch(`${this.baseURL}sideex-webservice/deleteJob?token=${token}`, {
method: 'POST',
agent: this.keepAliveAgent
})
return await response.text();
}
}
module.exports = {
SideeXWebserviceClientAPI,
ProtocolType: ProtocolType
}