From 19107b153e9e3a5e4a758b9db6a867c031467320 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 26 May 2023 00:04:16 +0200 Subject: Automated the extraction and delivery loop for a ship --- lib/api.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib/api.js') diff --git a/lib/api.js b/lib/api.js index abcd85d..1519888 100644 --- a/lib/api.js +++ b/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); } -- cgit v1.2.3