Automated the extraction and delivery loop for a ship
This commit is contained in:
parent
b1157af9cd
commit
19107b153e
4 changed files with 94 additions and 112 deletions
11
lib/api.js
11
lib/api.js
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue