Hi all
Have a project where I am looping over an array of exportedTickets and remove a tag from them. Am using this npm package that handles the Api Rates => https://www.npmjs.com/package/limiter
The problem is that it takes too long for the customer.
This package sets that the allowed number of calls is to 70(to stay safe from the api rates) each minute.
And that it removes one token after each iteration with this code snippet:
const remainingRequests = await limiter.removeTokens(1);
Is it correct that I am consuming 3 api rates in this function?
2 for getting the ticket with tags(include filtering) - getTicketTags
1 for when I am updating the ticket. - setTag
This is the function:
import { RateLimiter } from 'limiter';
const limiter = new RateLimiter({ tokensPerInterval: 70, interval: 'minute' });
export async function removeTagOnTicket(exportedTicketIDs, props, tag) {
const promises = exportedTicketIDs.map(async (ticketId) => {
const remainingRequests = await limiter.removeTokens(1);
console.log('Remaining time of API limits -->', remainingRequests);
const ticketTags = await props.client.request
.invokeTemplate('getTicketTags', {
context: { id: ticketId },
})
.then((data) => JSON.parse(data.response))
.then((ticket) => {
return ticket.ticket.tags;
})
.catch((error) => console.error(error));
// REMOVE TAG FROM ARRAY
let newTags = ticketTags.filter(function (element) {
return element !== tag;
});
return props.client.request.invokeTemplate('setTag', {
context: { id: ticketId },
body: JSON.stringify({ tags: newTags }),
});
});
const results = await Promise.all(promises);
return results;
}
Am not to keen to increse the tokensPerInterval any higher due to the API limit.
Is there an obvious error in how I am using this npm package to handle api rates?
Have anyone else used another npm package to handle rate limits?
How would you handle this?