-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add rate limiting information to client responses #5
base: master
Are you sure you want to change the base?
Conversation
shapeways/oauth2_client.py
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiny recommendation to slightly reduce code complexity would be to remove unnecessary else
s:
(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
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.
Not required change => ✅
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.
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
This PR surfaces our rate limiting headers from both of our RL sources - cloudflare and shapeways.com