Add rate limiting information to client responses#5
Open
Conversation
grimzy
approved these changes
Nov 22, 2019
Comment on lines
+93
to
+115
| if response.status_code != HTTP_OK: | ||
| if response.status_code == HTTP_RATE_LIMITED: | ||
| # We're rate limited | ||
| rate_limit.is_rate_limited=True | ||
|
|
||
| if CF_RATE_LIMIT_RETRY in headers.keys(): | ||
| # Limited by CF - backoff for a number of seconds | ||
| rate_limit.rate_limit_type=CF_RATE_LIMIT | ||
| rate_limit.rate_limit_remaining=0 | ||
| rate_limit.rate_limit_retry_inseconds=headers[CF_RATE_LIMIT_RETRY] | ||
| else: | ||
| # Shapeways Rate limiting - stupidly, we move the retryInSeconds entry from the response headers | ||
| # to the body. Dealing with this here. | ||
| rate_limit.rate_limit_remaining = 0 | ||
| rate_limit.rate_limit_retry_inseconds = response.json()['rateLimit']['retryInSeconds'] | ||
|
|
||
| return {CONTENT_SUCCESS: False, CONTENT_RATE_LIMIT: rate_limit.__dict__} | ||
| else: | ||
| # Generic error | ||
| content = response.json() | ||
| content[CONTENT_SUCCESS] = False | ||
| content[CONTENT_RATE_LIMIT] = rate_limit.__dict__ | ||
| return content |
There was a problem hiding this comment.
Tiny recommendation to slightly reduce code complexity would be to remove unnecessary elses:
(untested)
# Handle HTTP errors
if response.status_code != HTTP_OK:
# Generic error
content = response.json()
content[CONTENT_SUCCESS] = False
content[CONTENT_RATE_LIMIT] = rate_limit.__dict__
if response.status_code == HTTP_RATE_LIMITED:
# We're rate limited
rate_limit.is_rate_limited=True
# Shapeways Rate limiting - stupidly, we move the retryInSeconds entry from the response headers
# to the body. Dealing with this here.
rate_limit.rate_limit_remaining = 0
rate_limit.rate_limit_retry_inseconds = response.json()['rateLimit']['retryInSeconds']
if CF_RATE_LIMIT_RETRY in headers.keys():
# Limited by CF - backoff for a number of seconds
rate_limit.rate_limit_type=CF_RATE_LIMIT
rate_limit.rate_limit_retry_inseconds=headers[CF_RATE_LIMIT_RETRY]
content = {CONTENT_SUCCESS: False, CONTENT_RATE_LIMIT: rate_limit.__dict__}
return content
Contributor
Author
There was a problem hiding this comment.
So i had that originally - trouble is that the json payload only has the rateLimitRetry key if and only if the rate limiter has been achieved. Therefore, this statement cannot be treated as always valid
rate_limit.rate_limit_retry_inseconds = response.json()['rateLimit']['retryInSeconds']
Hence, extra elses
d33psky
reviewed
Nov 25, 2019
d33psky
reviewed
Nov 25, 2019
d33psky
reviewed
Nov 25, 2019
d33psky
reviewed
Nov 25, 2019
d33psky
suggested changes
Nov 25, 2019
d33psky
approved these changes
Nov 25, 2019
jawerty
approved these changes
Dec 9, 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR surfaces our rate limiting headers from both of our RL sources - cloudflare and shapeways.com