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 support for rate limiting #21
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
87fd05e
Add granular control over the conditions under which we retry a request
spyrbri ee8e926
Add retry functionality and allow config
spyrbri 0c32dda
Update Readme
spyrbri 346c540
Bump version to 1.2.0
spyrbri d6b5795
fixup! Add retry functionality and allow config
spyrbri 88d1b8e
Review fixes
spyrbri File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import requests | ||
from requests.adapters import HTTPAdapter | ||
from requests.packages.urllib3.util.retry import Retry | ||
|
||
METHOD_WHITELIST = ['HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'] | ||
STATUS_FORCELIST = (429, 500, 502, 503, 504, 520, 524) | ||
|
||
def requests_retry_session(retries=20, backoff_factor=2, session=None,): | ||
session = session or requests.Session() | ||
adapter = _retry_adapter(retries, backoff_factor) | ||
session.mount('https://', adapter) | ||
return session | ||
|
||
def _retry_adapter(retries, backoff_factor): | ||
retry = Retry( | ||
total=retries, | ||
read=retries, | ||
connect=retries, | ||
status=retries, | ||
method_whitelist=METHOD_WHITELIST, | ||
status_forcelist=STATUS_FORCELIST, | ||
backoff_factor=backoff_factor, | ||
) | ||
return HTTPAdapter(max_retries=retry) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import unittest | ||
|
||
import httpretty | ||
import chartmogul | ||
from chartmogul import Config, DataSource | ||
from datetime import date, datetime | ||
from requests.exceptions import RetryError | ||
from chartmogul.retry_request import requests_retry_session | ||
|
||
class RetryRequestTestCase(unittest.TestCase): | ||
|
||
@httpretty.activate | ||
def test_retry_request(self): | ||
httpretty.register_uri( | ||
httpretty.GET, | ||
"https://example:444/testing", | ||
responses=[ | ||
httpretty.Response(body='{}', status=500), | ||
httpretty.Response(body='{}', status=200), | ||
] | ||
) | ||
|
||
with self.assertRaises(RetryError): | ||
requests_retry_session(0).get('https://example:444/testing') | ||
|
||
response = requests_retry_session(2, 0).get('https://example:444/testing') | ||
self.assertEqual(response.text, '{}') | ||
|
||
@httpretty.activate | ||
def test_requests_retry_session_on_resource(self): | ||
httpretty.register_uri( | ||
httpretty.POST, | ||
"https://api.chartmogul.com/v1/data_sources", | ||
responses=[ | ||
httpretty.Response(body='{}', status=500), | ||
httpretty.Response(body='{}', status=500), | ||
httpretty.Response(body='{}', status=500), | ||
httpretty.Response(body='{}', status=500), | ||
httpretty.Response(body='{}', status=200), | ||
] | ||
) | ||
|
||
# max_retries set as 4 | ||
# backoff_factor set as 0 to avoid waiting while testing | ||
config = Config("token", "secret", None, 4, 0) | ||
try: | ||
DataSource.create(config, data={ "test_date": date(2015, 1, 1) }).get() | ||
except RetryError: | ||
self.fail("request raised retryError unexpectedly!") |
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.
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.
This is a mock too right? Sorry I couldn't see where we mock it :)
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.
It is mocked by httpretty here
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.
Thank you!