-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose the time at which the Rate Limit will reset #219
Conversation
@@ -122,7 +127,7 @@ def _error_message(self): | |||
|
|||
class EmptyResponse(Response): | |||
def __init__(self, status_code): | |||
super(EmptyResponse, self).__init__(status_code, '') | |||
super(EmptyResponse, self).__init__(status_code, '', {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like a good default for a successful but empty response
|
||
def content(self): | ||
if self._is_error(): | ||
if self._status_code == 429: | ||
reset_at = int(self._headers.get('x-ratelimit-reset', '-1')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the default of -1
is being set here
|
||
def content(self): | ||
if self._is_error(): | ||
if self._status_code == 429: | ||
reset_at = int(self._headers.get('x-ratelimit-reset', '-1')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
headers once parsed by the library are put into a CaseInsensitiveDict. Also, headers are by spec case insensitive -> https://requests.readthedocs.io/en/master/user/quickstart/#response-headers
@@ -152,7 +160,7 @@ def _error_message(self): | |||
|
|||
class EmptyResponse(Response): | |||
def __init__(self, status_code): | |||
super(EmptyResponse, self).__init__(status_code, '') | |||
super(EmptyResponse, self).__init__(status_code, '', {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you not expect the API endpoints that return empty responses to get rate limited?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see - so if there's no response.text
we know it's been successful so no need to check the rate limit header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Responses sent from the server could be:
- 204 NO CONTENT (empty body, empty text)
- Plain text response: e.g. "User is blocked"
- JSON response: e.g. { "code": "success" }
Every Response instance of this SDK defines a method to check if they represent errors, by checking against the status code here
https://github.com/auth0/auth0-python/blob/master/auth0/v3/management/rest.py#L111-L112.
Since for now, the headers are only being used for rate limit purposes, and only in the case of an errored response, I think it makes sense to pass an empty dict here. In addition, this headers
property in the Response is not accessible for developers since the parsed content is what is being returned after a successful call, or an exception is raised if this represents an error.
https://github.com/auth0/auth0-python/blob/master/auth0/v3/management/rest.py#L84-L87
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replying to your question, 429 responses have been returning a non-empty body. I don't expect empty responses to represent a rate limit exception ever. 👍
Co-authored-by: Adam Mcgrath <adam.mcgrath@auth0.com>
Changes
When too many requests have been made against the server, the next request will fail with a
429
. In the response headers there will be extra information, such as the time at which this limit is reset.With this PR:
429
errors are raised asRateLimitError
instead ofAuth0Error
.reset_at
with the value of theX-RateLimit-Reset
header. This value represents the Unix timestamp at which the limit will be reset.References
More info https://auth0.com/docs/policies/rate-limits
Testing
Checklist