Skip to content
TORNLIFE More

Coding Advice: Preventing Maximum API Calls

Started by cryosis7 [926640] on in API Development.

5 replies · 182 views · thread synced · 6 days ago · View on torn.com
About this thread

Posts archived: 6 / 6 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
6
Discussion span
→
People posting
4
Likes on archived posts
4
Authority score
52 / 100
Historical score
22 / 100
Story score
29 / 100
Engagement score
51 / 100
cryosis7 [926640]

So I'm by no means an expert to programming, in fact Torn's API is the first time I've made a program that interacts with something other than itself (and maybe a text document).

Anyway, the programs I've been experimenting with tend to be batch processing sort of work, i.e. read a bunch of information from a file, get the appropriate data from Torn and present it when done.

This obviously leads to a large amount of API calls and I just passed the rite of passage of making too many requests.

How could I incorporate a cap into my program?

I'm expecting I'd use a global variable to hold the calls count and a function to sleep if calls > max allowed; but how would you balance this with the time, and if it's safe to start running again?



TL;DR: How would I prevent my code from exceeding the API call limit since it will be making a lot of calls as fast as possible.
GamingAnonymous [1761543]

It would be something like this:

Create a variable to store the maximum amount of requests you would like each minute. (Should be a few below the actual maximum to be safe).
Create a variable to store the current number API calls this minute.
Create a variable to store the time. (DateTime for example in C#) This will start as null. (Or equivalent in any language).


Whenever an API call is made it should follow this logic:
If the time stored is null, set the time to the current time and send the API request.
If the time stored is not null and the number of API calls is less than the maximum allowed then send the request.
If the time stored is not null and the number of API calls is the same as the max allowed, disallow the API call.
If the current time is greater than that of the stored time, reset the API call counter and set the stored time to that of the current, send the API call.

This is probably overthinking it a bit, but these are generally the checks you would use. Just break it down into small chunks.


Mafia [610357]

This is some piece of my script how I control the api request to prevent it over limit..



There are 2 function :



[image: i.imgur.com]





And this is how you can use it..

you must check it first by call checkAPIusage(20) that check if I can make another 20 request and to make sure its not conflicted with previous api request cooldown..

By calling this method, it will remove previous api usage/timer that exceeds 60seconds (1minute) and sum 20 new incoming request in a pool with remaining previous api usage to check if it over 100 (limit) or in under control

This is to control your API usage (not over 100 request in minute).. or you can break/exit/stop your code when false are returned.

[image: i.imgur.com]



addAPIusage() is used to push/insert your new timestamp after you made successful api request.
cryosis7 [926640]

Wow thanks for the answers :) Will implement similar functions.



And FYI: I maxed out because of an accidental infinite loop :P
Meme_Raven [1891116]

Depends on your language.

I highly despise logging but call logging is appreciable. Log the amount of calls your application uses, and use it as a range in the current clock cycle (in most time cycles if not all the seconds remain the same) when above X limit, drop.