forked from kokarn/tarkov-image-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner-api.js
123 lines (117 loc) · 3.77 KB
/
scanner-api.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require('fs');
const { setTimeout } = require('timers/promises');
const got = require('got');
const FormData = require('form-data');
const API_URL = 'https://manager.tarkov.dev/api/scanner';
//const API_URL = 'http://localhost:4000/api/scanner';
const sleep = async (ms) => {
return setTimeout(ms, true).catch(err => {
return Promise.resolve(true);
});
};
const apiRequest = async (endpoint, method, options, retries) => {
if (!retries) retries = 0;
try {
const {body} = await got(API_URL+'/'+endpoint, {
json: options,
responseType: 'json',
headers: {
username: process.env.API_USERNAME,
password: process.env.API_PASSWORD,
scanner: process.env.SCANNER_NAME
},
allowGetBody: true,
method: method,
retry: {
limit: 10,
calculateDelay: () => {
return 500;
}
}
});
return Promise.resolve(body);
} catch (error) {
let retry = false;
const retryCodes = [
'ECONNREFUSED',
'ENOTFOUND',
'ETIMEDOUT',
'ECONNRESET'
];
if (error.code && retryCodes.includes(error.code) && !settings.aborted()) {
if (retries <= 10) {
retry = true;
}
} else if (error.message === 'access denied') {
retry = true;
}
if (retry) {
await sleep(500);
return apiRequest(endpoint, method, options, retries+1);
}
if (error.code && error.code === 'ERR_BODY_PARSE_FAILURE') {
return Promise.reject(new Error('invalid api response'));
}
return Promise.reject(error);
}
};
module.exports = {
ping: async () => {
try {
const result = await apiRequest('ping', 'GET');
if (result.errors.length > 0) {
for (let i = 0; i < result.errors.length; i++) {
logger(chalk.red(`Error pinging API: ${result.errors[i]}`));
}
return Promise.reject(new Error(result.errors[0]));
}
} catch (error) {
return Promise.reject(error);
}
},
connected: async () => {
try {
await module.exports.ping();
} catch (error) {
return false;
}
return true;
},
getJson: async (filename) => {
try {
const result = await apiRequest('json', 'GET', {file: filename});
if (result.errors.length > 0) {
for (let i = 0; i < result.errors.length; i++) {
logger(chalk.red(`Error getting JSON: ${result.errors[i]}`));
}
return Promise.reject(new Error(result.errors[0]));
}
return result.data;
} catch (error) {
return Promise.reject(error);
}
},
submitImage: async (itemId, imageType, filePath, overwrite = false) => {
let bufferStream = false;
filePath = await filePath;
if (typeof filePath === 'string') {
bufferStream = fs.createReadStream(filePath);
} else {
bufferStream = filePath;
}
const form = new FormData();
form.append('id', itemId);
form.append('type', imageType);
form.append(imageType, bufferStream);
form.append('overwrite', String(overwrite));
return got.post(API_URL + '/image', {
body: form,
headers: {
username: process.env.API_USERNAME,
password: process.env.API_PASSWORD,
},
responseType: 'json',
resolveBodyOnly: true
});
}
};