-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseConverterApiClient.js
59 lines (47 loc) · 1.71 KB
/
BaseConverterApiClient.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
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');
class BaseConverterApiClient {
static API_URL = "https://convert.tigerra.com";
constructor(authToken) {
this.authToken = authToken;
}
async sendRequest(method, endpoint, params = {}, filePath = null) {
const url = `${BaseConverterApiClient.API_URL}${endpoint}`;
const headers = {
'Authorization': `Bearer ${this.authToken}`
};
let data = params;
if (method === 'POST' && filePath) {
if (!fs.existsSync(filePath)) {
throw new Error(`File not found: ${filePath}`);
}
const form = new FormData();
form.append('file', fs.createReadStream(filePath));
for (const key in params) {
form.append(key, params[key]);
}
data = form;
headers['Content-Type'] = `multipart/form-data; boundary=${form._boundary}`;
}
try {
const response = await axios({
method: method,
url: url,
headers: headers,
data: data,
timeout: 300000 // Set timeout to 300 seconds
});
return response.data;
} catch (error) {
if (error.response) {
throw new Error(`HTTP Error: ${error.response.status} - Response: ${error.response.data}`);
} else if (error.request) {
throw new Error(`No response received: ${error.message}`);
} else {
throw new Error(`Request error: ${error.message}`);
}
}
}
}
module.exports = BaseConverterApiClient;