-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add granular control over the conditions under which we retry a request * Add retry functionality and allow config * Update Readme * Bump version to 1.2.0 * fixup! Add retry functionality and allow config * Review fixes
- Loading branch information
Showing
7 changed files
with
99 additions
and
12 deletions.
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!") |