Number of remaining API calls? #286
-
Hello, I'm building something that requires to download posts from an actor's feed. Is there any way to see the number of API calls I can still make before a TooManyRequests is thrown? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hello, I think it duplicates #167. I wanna provide such API but I'm not sure of the best implementation. Saving the last response headers to the client could be problematic in async version. Maybe adding some kind of callback which will be called on plain response with headers is good enough. Wdyt? Do you have examples of any other Python libs that provide such functionality? |
Beta Was this translation helpful? Give feedback.
-
Hello. I prepared simple example of override method that gives you access to response headers (which contains rate limits). class RateLimitedClient(Client):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._limit = self._remaining = self._reset = None
def get_rate_limit(self):
return self._limit, self._remaining, self._reset
def _invoke(self, *args, **kwargs):
response = super()._invoke(*args, **kwargs)
self._limit = response.headers.get('RateLimit-Limit')
self._remaining = response.headers.get('RateLimit-Remaining')
self._reset = response.headers.get('RateLimit-Reset')
return response Usage: client = RateLimitedClient()
client.login(os.environ['USERNAME'], os.environ['PASSWORD'])
post = client.get_timeline()
print(client.get_rate_limit()) |
Beta Was this translation helpful? Give feedback.
Hello. I prepared simple example of override method that gives you access to response headers (which contains rate limits).