Skip to content

Commit 52fa586

Browse files
authored
Retry HTTP requests on 409 errors (#78)
* Retry http requests on 409 errors * Break up complex conditional
1 parent 237830f commit 52fa586

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pipeline_tools/http_requests.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ def is_retryable(error):
159159
print('{0} {1}'.format(now, repr(error)))
160160

161161
def is_retryable_status_code(error):
162-
return isinstance(error, requests.HTTPError) and not (400 <= error.response.status_code <= 499)
162+
if not isinstance(error, requests.HTTPError):
163+
return False
164+
if error.response.status_code == 409:
165+
return True
166+
else:
167+
return not (400 <= error.response.status_code <= 499)
163168

164169
return is_retryable_status_code(error) or isinstance(error,
165170
(requests.ConnectionError, requests.ReadTimeout))
@@ -293,7 +298,7 @@ def _get_next_file_suffix(files):
293298

294299
@staticmethod
295300
def check_status(response):
296-
"""Check that the response status code is in range 200-299.
301+
"""Check that the response status code is in range 200-299, or 409.
297302
Raises a ValueError and prints response_text if status is not in the expected range. Otherwise,
298303
just returns silently.
299304
Args:
@@ -311,7 +316,7 @@ def check_status(response):
311316
check_status(301, 'bar') raises error
312317
"""
313318
status = response.status_code
314-
matches = 200 <= status <= 299
319+
matches = 200 <= status <= 299 or status == 409
315320
if not matches:
316321
message = 'HTTP status code {0} is not in expected range 2xx. Response: {1}'.format(status, response.text)
317322
raise requests.HTTPError(message, response=response)

pipeline_tools/tests/test_http_requests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ def test_check_status_acceptable_codes(self):
4040
except requests.HTTPError as e:
4141
pytest.fail(str(e))
4242

43+
try:
44+
response = requests.Response()
45+
response.status_code = 409
46+
HttpRequests.check_status(response)
47+
except requests.HTTPError as e:
48+
pytest.fail(str(e))
49+
4350
def test_get_retries_then_raises_exception_on_500(self, requests_mock):
4451
def callback(request, response):
4552
response.status_code = 500

0 commit comments

Comments
 (0)