tomasbasham / ratelimit

API Rate Limit Decorator
MIT License
768 stars 159 forks source link

Add a “Usage note”? #55

Open samwyse opened 3 years ago

samwyse commented 3 years ago

Sometimes you need to limit API calls from another package that you do not control. In this case, monkey patching the API may be the best option. But where? In the requests package, calls to .get, .head, and other methods of a Request() object are funneled to self.request(), so that is one possibility.  However, that method creates a temporary Session object and sends all invocations through it. As a result, the best way to limit all API calls is:

throttled = limits(calls=15, period=FIFTEEN_MINUTES)
Session.request = throttled(Session.request)