1
0
Fork 0

Automated the extraction and delivery loop for a ship

This commit is contained in:
Julien Dessaux 2023-05-26 00:04:16 +02:00
parent b1157af9cd
commit 19107b153e
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 94 additions and 112 deletions

View file

@ -1,3 +1,5 @@
import * as fs from 'fs';
import { getToken } from '../database/config.js';
import { PriorityQueue } from './priority_queue.js';
@ -58,10 +60,15 @@ function send_this(data) {
if (data.request.payload !== undefined) {
options['body'] = JSON.stringify(data.request.payload);
}
fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'});
fetch(`https://api.spacetraders.io/v2${data.request.endpoint}`, options)
.then(response => response.json())
.then(response => data.resolve(response))
.catch(err => data.reject(err));
.then(response => {
fs.writeFileSync('log', JSON.stringify({event: 'response', date: new Date(), data: response}) + '\n', {flag: 'a+'});
return data.resolve(response);})
.catch(err => {
fs.writeFileSync('log', JSON.stringify({event: 'error', date: new Date(), data: err}) + '\n', {flag: 'a+'});
data.reject(err)});
setTimeout(send_next, 500);
}