summaryrefslogtreecommitdiff
path: root/nodejs/lib
diff options
context:
space:
mode:
authorJulien Dessaux2023-09-01 23:51:51 +0200
committerJulien Dessaux2023-09-01 23:51:51 +0200
commita50ea1be7c6332974769b15608e8fdc811f4f658 (patch)
tree887fe3869db47bf029003384edd21931112e296f /nodejs/lib
parent[javascript] updated dependencies (diff)
downloadspacetraders-a50ea1be7c6332974769b15608e8fdc811f4f658.tar.gz
spacetraders-a50ea1be7c6332974769b15608e8fdc811f4f658.tar.bz2
spacetraders-a50ea1be7c6332974769b15608e8fdc811f4f658.zip
[javascript] implemented paginated requests
Diffstat (limited to '')
-rw-r--r--nodejs/lib/api.js35
1 files changed, 29 insertions, 6 deletions
diff --git a/nodejs/lib/api.js b/nodejs/lib/api.js
index 667ef37..3e2bbd0 100644
--- a/nodejs/lib/api.js
+++ b/nodejs/lib/api.js
@@ -48,10 +48,29 @@ queue_processor();
// example request: {
// endpoint: the path part of the url to call,
// method: HTTP method for `fetch` call, defaults to 'GET',
+// page: run a paginated request starting from this page until all the following pages are fetched
// 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(request, ctx) {
+export async function send(request, ctx) {
+ if (request.page === undefined) {
+ return await send_one(request, ctx);
+ }
+ let ret = [];
+ while (true) {
+ const response = await send_one(request, ctx);
+ if (response.meta === undefined) {
+ throw {"message": "paginated request did not return a meta block", "request": request, "response": response};
+ }
+ ret = ret.concat(response.data);
+ if (response.meta.limit * response.meta.page >= response.meta.total) {
+ return ret;
+ }
+ request.page++;
+ }
+}
+
+function send_one(request, ctx) {
return new Promise((resolve, reject) => {
let data = {
ctx: ctx,
@@ -87,17 +106,21 @@ async function send_this(data) {
if (data.request.payload !== undefined) {
options['body'] = JSON.stringify(data.request.payload);
}
+ let pagination = "";
+ if (data.request.page !== undefined) {
+ pagination=`?limit=20&page=${data.request.page}`;
+ }
fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'});
try {
- let response = await fetch(`https://api.spacetraders.io/v2${data.request.endpoint}`, options);
+ let response = await fetch(`https://api.spacetraders.io/v2${data.request.endpoint}${pagination}`, options);
response = await response.json();
switch(response.error?.code) {
- //case 401: // TODO 401 means a server reset happened
+ case 401: // TODO 401 means a server reset happened
+ throw response;
// TODO reject all promises in queue
- // close database file
- // rm database file
+ // reset database
// logrotate
- // spawnSync
+ // spawnSync?
// break;
case 429: // 429 means rate limited, let's hold back as instructed
backoffSeconds = response.error.data.retryAfter;