[javascript] implemented paginated requests
This commit is contained in:
parent
d78d9b1663
commit
a50ea1be7c
1 changed files with 29 additions and 6 deletions
|
@ -48,10 +48,29 @@ queue_processor();
|
||||||
// example request: {
|
// example request: {
|
||||||
// endpoint: the path part of the url to call,
|
// endpoint: the path part of the url to call,
|
||||||
// method: HTTP method for `fetch` call, defaults to 'GET',
|
// 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,
|
// 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)
|
// 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) => {
|
return new Promise((resolve, reject) => {
|
||||||
let data = {
|
let data = {
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
@ -87,17 +106,21 @@ async function send_this(data) {
|
||||||
if (data.request.payload !== undefined) {
|
if (data.request.payload !== undefined) {
|
||||||
options['body'] = JSON.stringify(data.request.payload);
|
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+'});
|
fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'});
|
||||||
try {
|
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();
|
response = await response.json();
|
||||||
switch(response.error?.code) {
|
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
|
// TODO reject all promises in queue
|
||||||
// close database file
|
// reset database
|
||||||
// rm database file
|
|
||||||
// logrotate
|
// logrotate
|
||||||
// spawnSync
|
// spawnSync?
|
||||||
// break;
|
// break;
|
||||||
case 429: // 429 means rate limited, let's hold back as instructed
|
case 429: // 429 means rate limited, let's hold back as instructed
|
||||||
backoffSeconds = response.error.data.retryAfter;
|
backoffSeconds = response.error.data.retryAfter;
|
||||||
|
|
Loading…
Add table
Reference in a new issue