8000 Extending HTTP retries to all methods (#255) · CyberSys/firebase-admin-python@7888599 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7888599

Browse files
authored
Extending HTTP retries to all methods (firebase#255)
1 parent 4d01830 commit 7888599

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Unreleased
22

3-
-
3+
- [fixed] Extending HTTP retries to more HTTP methods like POST and PATCH.
44

55
# v2.15.1
66

firebase_admin/_http_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
from requests.packages.urllib3.util import retry # pylint: disable=import-error
2323

2424

25+
_ANY_METHOD = None
26+
2527
# Default retry configuration: Retries once on low-level connection and socket read errors.
2628
# Retries up to 4 times on HTTP 500 and 503 errors, with exponential backoff. Returns the
2729
# last response upon exhausting all retries.
2830
DEFAULT_RETRY_CONFIG = retry.Retry(
29-
connect=1, read=1, status=4, status_forcelist=[500, 503],
31+
connect=1, read=1, status=4, status_forcelist=[500, 503], method_whitelist=_ANY_METHOD,
3032
raise_on_status=False, backoff_factor=0.5)
3133

3234

tests/test_http_client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,37 @@ def _instrument(client, payload, status=200):
8787
class TestHttpRetry(object):
8888
"""Unit tests for the default HTTP retry configuration."""
8989

90+
ENTITY_ENCLOSING_METHODS = ['post', 'put', 'patch']
91+
ALL_METHODS = ENTITY_ENCLOSING_METHODS + ['get', 'delete', 'head', 'options']
92+
9093
@classmethod
9194
def setup_class(cls):
9295
# Turn off exponential backoff for faster execution
9396
_http_client.DEFAULT_RETRY_CONFIG.backoff_factor = 0
9497

95-
def test_retry_on_503(self, httpserver):
98+
@pytest.mark.parametrize('method', ALL_METHODS)
99+
def test_retry_on_503(self, httpserver, method):
96100
httpserver.serve_content({}, 503)
97101
client = _http_client.JsonHttpClient(
98102
credential=testutils.MockGoogleCredential(), base_url=httpserver.url)
103+
body = None
104+
if method in self.ENTITY_ENCLOSING_METHODS:
105+
body = {'key': 'value'}
99106
with pytest.raises(req AC82 uests.exceptions.HTTPError) as excinfo:
100-
client.request('get', '/')
107+
client.request(method, '/', json=body)
101108
assert excinfo.value.response.status_code == 503
102109
assert len(httpserver.requests) == 5
103110

104-
def test_retry_on_500(self, httpserver):
111+
@pytest.mark.parametrize('method', ALL_METHODS)
112+
def test_retry_on_500(self, httpserver, method):
105113
httpserver.serve_content({}, 500)
106114
client = _http_client.JsonHttpClient(
107115
credential=testutils.MockGoogleCredential(), base_url=httpserver.url)
116+
body = None
117+
if method in self.ENTITY_ENCLOSING_METHODS:
118+
body = {'key': 'value'}
108119
with pytest.raises(requests.exceptions.HTTPError) as excinfo:
109-
client.request('get', '/')
120+
client.request(method, '/', json=body)
110121
assert excinfo.value.response.status_code == 500
111122
assert len(httpserver.requests) == 5
112123

0 commit comments

Comments
 (0)
0