forked from kokarn/tarkov-image-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscanner-api.js
More file actions
105 lines (99 loc) · 3.26 KB
/
scanner-api.js
File metadata and controls
105 lines (99 loc) · 3.26 KB
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
const fs = require('fs');
const { setTimeout } = require('timers/promises');
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 response = await fetch(API_URL+'/'+endpoint, {
body: options ? JSON.stringify(options) : undefined,
headers: {
username: process.env.API_USERNAME,
password: process.env.API_PASSWORD,
scanner: process.env.SCANNER_NAME
},
method: method,
retry: {
limit: 10,
calculateDelay: () => {
return 500;
}
}
});
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}
return response.json();
} catch (error) {
if (retries > 10) {
return Promise.reject(error);
}
await sleep(500);
return apiRequest(endpoint, method, options, retries+1);
}
};
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 fetch(API_URL + '/image', {
method: 'POST',
body: form,
headers: {
username: process.env.API_USERNAME,
password: process.env.API_PASSWORD,
},
}).then(response => response.json());
}
};