Rewrote the api rate limiter with promises instead of callbacks
This commit is contained in:
parent
efdf50a55a
commit
9963ab79b7
6 changed files with 105 additions and 40 deletions
6
database/001_systems.sql
Normal file
6
database/001_systems.sql
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
CREATE TABLE systems (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
symbol TEXT NOT NULL UNIQUE,
|
||||||
|
data TEXT NOT NULL,
|
||||||
|
updated DATE DEFAULT (datetime('now'))
|
||||||
|
);
|
|
@ -3,6 +3,7 @@ import Database from 'better-sqlite3';
|
||||||
|
|
||||||
const allMigrations = [
|
const allMigrations = [
|
||||||
'database/000_init.sql',
|
'database/000_init.sql',
|
||||||
|
'database/001_systems.sql',
|
||||||
];
|
];
|
||||||
|
|
||||||
const db = new Database(
|
const db = new Database(
|
||||||
|
|
26
database/systems.js
Normal file
26
database/systems.js
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import db from './db.js';
|
||||||
|
|
||||||
|
const getSystemStatement = db.prepare(`SELECT data from systems where symbol = ?;`);
|
||||||
|
const setSystemStatement = db.prepare(`INSERT INTO systems(symbol, data) VALUES (?, ?);`);
|
||||||
|
|
||||||
|
export function getSystem(symbol) {
|
||||||
|
try {
|
||||||
|
const data = getSystemStatement.get(symbol);
|
||||||
|
if (data === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return JSON.parse(data.data);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setSystem(symbol, data) {
|
||||||
|
try {
|
||||||
|
return setSystemStatement.run(symbol, JSON.stringify(data)).lastInsertRowid;
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
59
lib/api.js
59
lib/api.js
|
@ -5,32 +5,28 @@ let busy = false; // lets us know if we are already sending api requests or not.
|
||||||
let headers = undefined; // a file scope variable so that we only evaluate these once.
|
let headers = undefined; // a file scope variable so that we only evaluate these once.
|
||||||
let queue = new PriorityQueue(); // a priority queue to hold api calls we want to send, allows for throttling.
|
let queue = new PriorityQueue(); // a priority queue to hold api calls we want to send, allows for throttling.
|
||||||
|
|
||||||
// chain takes an array of actions as argument. For each one it sets the `next` property.
|
// send takes a request object as argument and an optional context ctx
|
||||||
// example action: {
|
// example request: {
|
||||||
// action: function to call,
|
|
||||||
// next: optional nested action object, would get overriden by this function, except for the last action,
|
|
||||||
// ... other attributes as required by the action function (for example ship or waypoing symbol...)
|
|
||||||
// }
|
|
||||||
export function chain(actions) {
|
|
||||||
for(let i=actions.length-1;i>0;--i) {
|
|
||||||
actions[i-1].next = actions[i];
|
|
||||||
}
|
|
||||||
actions[0].action(actions[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// send takes a data object as argument
|
|
||||||
// example data: {
|
|
||||||
// endpoint: the url endpoint to call,
|
// endpoint: the url endpoint to call,
|
||||||
// method: HTTP method for `fetch` call, defaults to 'GET',
|
// method: HTTP method for `fetch` call, defaults to 'GET',
|
||||||
// next: optional nested action object, as specified above with the chain function,
|
|
||||||
// payload: optional json object that will be send along with the request,
|
// payload: optional json object that will be send along with the request,
|
||||||
|
// priority: optional priority value (defaults to 10, lower than 10 means the message will be sent faster)
|
||||||
// }
|
// }
|
||||||
export function send(data) {
|
export function send(request, ctx) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let data = {
|
||||||
|
ctx: ctx,
|
||||||
|
reject: reject,
|
||||||
|
request: request,
|
||||||
|
resolve: resolve,
|
||||||
|
};
|
||||||
if (!busy) {
|
if (!busy) {
|
||||||
|
busy = true;
|
||||||
send_this(data);
|
send_this(data);
|
||||||
} else {
|
} else {
|
||||||
queue.enqueue(data, data.priority ? data.priority : 10);
|
queue.enqueue(data, request.priority ? request.priority : 10);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function send_next() {
|
function send_next() {
|
||||||
|
@ -41,6 +37,7 @@ function send_next() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// send_this take a data object as argument built in the send function above
|
||||||
function send_this(data) {
|
function send_this(data) {
|
||||||
if (headers === undefined) {
|
if (headers === undefined) {
|
||||||
const token = getToken();
|
const token = getToken();
|
||||||
|
@ -55,22 +52,20 @@ function send_this(data) {
|
||||||
let options = {
|
let options = {
|
||||||
headers: headers,
|
headers: headers,
|
||||||
};
|
};
|
||||||
if (data.method !== undefined) {
|
if (data.request.method !== undefined) {
|
||||||
options['method'] = data.method;
|
options['method'] = data.request.method;
|
||||||
}
|
}
|
||||||
if (data.payload !== undefined) {
|
if (data.request.payload !== undefined) {
|
||||||
options['body'] = JSON.stringify(data.payload);
|
options['body'] = JSON.stringify(data.request.payload);
|
||||||
}
|
}
|
||||||
busy = true;
|
fetch(`https://api.spacetraders.io/v2${data.request.endpoint}`, options)
|
||||||
fetch(`https://api.spacetraders.io/v2${data.endpoint}`, options)
|
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(response => {
|
.then(response => data.resolve(response))
|
||||||
if (data.next !== undefined) { // if we have a next action, call it now
|
.catch(err => data.reject(err));
|
||||||
data.next.action(data.next, response);
|
|
||||||
} else { // otherwise use this debug action
|
|
||||||
console.log(JSON.stringify(response, null, 2));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(err => console.error(err));
|
|
||||||
setTimeout(send_next, 500);
|
setTimeout(send_next, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function debugLog(ctx) {
|
||||||
|
console.log(`--- ${Date()} -----------------------------------------------------------------------------`);
|
||||||
|
console.log(JSON.stringify(ctx, null, 2));
|
||||||
|
}
|
||||||
|
|
36
lib/systems.js
Normal file
36
lib/systems.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import * as api from './api.js';
|
||||||
|
import * as db from '../database/systems.js';
|
||||||
|
|
||||||
|
// Retrieves a list of waypoints that have a specific ctx.trait like a SHIPYARD or a MARKETPLACE in the system ctx.symbol
|
||||||
|
export async function trait(ctx) {
|
||||||
|
const s = await getSystem(ctx);
|
||||||
|
return s.filter(s => s.traits.some(t => t.symbol === ctx.trait));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieves a list of waypoints that have a specific ctx.type like ASTEROID_FIELD in the system ctx.symbol
|
||||||
|
export async function type(ctx, response) {
|
||||||
|
const s = await getSystem(ctx);
|
||||||
|
return s.filter(s => s.type === ctx.type);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieves the system's information for ctx.symbol and cache it in the database
|
||||||
|
async function getSystem(ctx) {
|
||||||
|
let s = db.getSystem(ctx.symbol);
|
||||||
|
if (s === null) {
|
||||||
|
const response = await api.send({endpoint: `/systems/${ctx.symbol}/waypoints?limit=20&page=1`});
|
||||||
|
if (response.error !== undefined) {
|
||||||
|
switch(response.error.code) {
|
||||||
|
case 404:
|
||||||
|
throw `Error retrieving waypoints for system ${ctx.symbol}: ${response.error.message}`;
|
||||||
|
default: // yet unhandled error
|
||||||
|
throw response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (response.meta !== undefined && response.meta.total > response.meta.limit) {
|
||||||
|
throw `Error retrieving waypoints for system ${ctx.symbol}: Pagination is not implemented yet!`;
|
||||||
|
}
|
||||||
|
s = response.data;
|
||||||
|
db.setSystem(ctx.symbol, s);
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
9
main.js
9
main.js
|
@ -1,6 +1,7 @@
|
||||||
import * as agent from './lib/agent.js';
|
import * as agent from './lib/agent.js';
|
||||||
import * as api from './lib/api.js';
|
import * as api from './lib/api.js';
|
||||||
import * as ships from './lib/ships.js';
|
import * as ships from './lib/ships.js';
|
||||||
|
import * as systems from './lib/systems.js';
|
||||||
|
|
||||||
function usage() {
|
function usage() {
|
||||||
console.log(`contracts\t\t\tList all of your contracts.
|
console.log(`contracts\t\t\tList all of your contracts.
|
||||||
|
@ -72,11 +73,11 @@ default:
|
||||||
case 'sell':
|
case 'sell':
|
||||||
ships.sell({ship: process.argv[3], good: process.argv[4], units: process.argv[5]});
|
ships.sell({ship: process.argv[3], good: process.argv[4], units: process.argv[5]});
|
||||||
break;
|
break;
|
||||||
case 'shipyard':
|
case 'asteroids':
|
||||||
api.send({endpoint: `/systems/${process.argv[3]}/waypoints/${process.argv[4]}/shipyard`});
|
api.debugLog(await systems.type({symbol: process.argv[3], type: 'ASTEROID_FIELD'}));
|
||||||
break;
|
break;
|
||||||
case 'waypoints':
|
case 'shipyards':
|
||||||
api.send({endpoint: `/systems/${process.argv[3]}/waypoints?limit=20&page=1`});
|
api.debugLog(await systems.trait({symbol: process.argv[3], trait: 'SHIPYARD'}));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
|
|
Loading…
Add table
Reference in a new issue