ramonsaraiva / pubg-python

A python wrapper for the PUBG developer API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RateLimitError Auto-Handling

Klamath233 opened this issue · comments

Firstly, thank you for your great work! In practice, I find myself scratching my head when I'm constantly getting RateLimitError when I'm collecting data for my friends, due to the 5 request/min limitation. Therefore, I'm suggesting a feature which wraps around the requests using some structure like the following:

cont = False
while not cont:
    try:
        # make the request
        
       cont = True
   except pubg_python.exceptions.RateLimitError:
        time.sleep(60)

This has the disadvantage of making the return time of the call very unpredictable. However, we could warn the users about it and let them choose which to use.

There are two things i want to implement about ratelimits.

One is to have rate limit information when you're handling the API calls, so you know how much time you need to wait for the next "possible call".

The other one is to be able to activate something like you suggested, that would automatically wait for the rate limit time when it receives an exception like that.

Thanks for the suggestion, i will potentialy add it in the next couple days.

Great! Have a nice day and happy coding. If you'd like me to help I'd love to contribute.

Contributions are always welcome. Please do it if you want to :-) Cheers!

I would like to know more about the "rate limit information" you mentioned earlier. Is there a way to know it from PUBG API?

Yes there are a few HTTP headers they use to expose ratelimits.

Thanks!

=op

Does this implemented?

Not yet. 😢 Will try implementing in the next few weeks.

1 Year Later. :)

I still wonder if this actually should be implemented cause making an application sleep because of a ratelimit is really dangerous. I think this should be handled by the application code instead, cause the way it will "wait" really depends on its external architecture.

One is to have rate limit information when you're handling the API calls, so you know how much time you need to wait for the next "possible call".

I think at least this should be implemented.

@glmn I'll add to the RateLimitException (https://github.com/ramonsaraiva/pubg-python/blob/master/pubg_python/exceptions.py#L45) context the timestamp it will unlock requests.

@Klamath233 | @glmn

Take a look at #77
Docs are available in https://github.com/ramonsaraiva/pubg-python#ratelimits - after version 0.11.0 (https://github.com/ramonsaraiva/pubg-python/releases/tag/0.11.0 & https://pypi.org/project/pubg-python/0.11.0/)

As per documentation, a snippet like:

api = PUBG('my-super-secret-key', Shard.STEAM)

while True:
    try:
        print('Processing samples...')
        api.samples().get()
    except RateLimitError as error:
        sleep_seconds = (error.rl_reset - datetime.now()).total_seconds()
        if sleep_seconds > 0:
            print('Reached my limit! sleeping for {}'.format(sleep_seconds))
            time.sleep(sleep_seconds)

Would generate this output:

(pubg-python) ~/src/pubg-python (master ✘)✹✭ ᐅ python test_ratelimit.py
Processing samples...
Reached my limit! sleeping for 39.307168 seconds
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Processing samples...
Reached my limit! sleeping for 43.842393 seconds

Great! Will switch from (stupidly) simple ratelimiter in my discord bot to yours.
Thank you for this improvement.

Awesome. Glad this will make your life easier. :-) Cheers!