Skip to content

Commit 5cec18d

Browse files
committed
Added retry
1 parent a042e6e commit 5cec18d

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

src/nypl_py_utils/classes/avro_client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import avro.schema
23
import requests
34

@@ -16,8 +17,8 @@ class AvroClient:
1617
"""
1718

1819
def __init__(self, platform_schema_url):
19-
self.logger = create_log("avro_encoder")
20-
retry_policy = Retry(total=2, backoff_factor=4,
20+
self.logger = create_log("avro_client")
21+
retry_policy = Retry(total=3, backoff_factor=45,
2122
status_forcelist=[500, 502, 503, 504],
2223
allowed_methods=frozenset(['GET']))
2324
self.session = requests.Session()
@@ -38,7 +39,7 @@ def get_json_schema(self, platform_schema_url):
3839
response = self.session.get(url=platform_schema_url,
3940
timeout=300)
4041
response.raise_for_status()
41-
except RequestException as e:
42+
except Exception as e:
4243
self.logger.error(
4344
"Failed to retrieve schema from {url}: {error}".format(
4445
url=platform_schema_url, error=e
@@ -48,7 +49,7 @@ def get_json_schema(self, platform_schema_url):
4849
"Failed to retrieve schema from {url}: {error}".format(
4950
url=platform_schema_url, error=e
5051
)
51-
) from None
52+
)
5253

5354
try:
5455
json_response = response.json()

tests/test_avro_client.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
22
import pytest
3+
from unittest.mock import patch
34

45
from nypl_py_utils.classes.avro_client import (
5-
AvroClientError, AvroDecoder, AvroEncoder)
6+
AvroClient, AvroClientError, AvroDecoder, AvroEncoder)
7+
from requests import session
68
from requests.exceptions import ConnectTimeout
79

810
_TEST_SCHEMA = {'data': {'schema': json.dumps({
@@ -36,15 +38,23 @@ def test_avro_decoder_instance(self, requests_mock):
3638

3739
def test_get_json_schema_success(self, test_avro_encoder_instance,
3840
test_avro_decoder_instance):
39-
assert test_avro_encoder_instance.schema == _TEST_SCHEMA['data'][
40-
'schema']
41-
assert test_avro_decoder_instance.schema == _TEST_SCHEMA['data'][
42-
'schema']
41+
assert test_avro_encoder_instance.schema == _TEST_SCHEMA["data"][
42+
"schema"]
43+
assert test_avro_decoder_instance.schema == _TEST_SCHEMA["data"][
44+
"schema"]
4345

44-
def test_request_error(self, requests_mock):
45-
requests_mock.get('https://test_schema_url', exc=ConnectTimeout)
46+
def test_get_json_schema_error(self, requests_mock):
47+
requests_mock.get("https://test_schema_url", exc=ConnectTimeout)
4648
with pytest.raises(AvroClientError):
47-
AvroEncoder('https://test_schema_url')
49+
AvroEncoder("https://test_schema_url")
50+
51+
def test_get_json_schema_success_on_retry(self, requests_mock):
52+
requests_mock.get("https://test_schema_url",
53+
[{"exc": ConnectionError},
54+
{"text": str(_TEST_SCHEMA), "status_code": 200}])
55+
56+
test_avro_client = AvroClient("https://test_schema_url")
57+
assert test_avro_client.get_json_schema == _TEST_SCHEMA["data"]["schema"]
4858

4959
def test_bad_json_error(self, requests_mock):
5060
requests_mock.get(

0 commit comments

Comments
 (0)