Skip to content

Commit 5d16a03

Browse files
committed
Added retry functionality
1 parent 41ef2df commit 5d16a03

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
2+
## v1.2.1 7/25/24
3+
- Add retry for fetching Avro schemas
4+
25
## v1.2.0 7/17/24
3-
- Generalized Avro functions and separated encoding/decoding behavior.
6+
- Generalized Avro functions and separated encoding/decoding behavior
47

58
## v1.1.6 7/12/24
69
- Add put functionality to Oauth2 Client

src/nypl_py_utils/classes/avro_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from avro.io import BinaryDecoder, BinaryEncoder, DatumReader, DatumWriter
66
from io import BytesIO
77
from nypl_py_utils.functions.log_helper import create_log
8+
from requests.adapters import HTTPAdapter, Retry
89
from requests.exceptions import JSONDecodeError, RequestException
910

1011

@@ -16,6 +17,12 @@ class AvroClient:
1617

1718
def __init__(self, platform_schema_url):
1819
self.logger = create_log("avro_encoder")
20+
retry_policy = Retry(total=2, backoff_factor=4,
21+
status_forcelist=[500, 502, 503, 504],
22+
allowed_methods=frozenset(['GET']))
23+
self.session = requests.Session()
24+
self.session.mount("https://",
25+
HTTPAdapter(max_retries=retry_policy))
1926
self.schema = avro.schema.parse(
2027
self.get_json_schema(platform_schema_url))
2128

@@ -27,7 +34,9 @@ def get_json_schema(self, platform_schema_url):
2734
self.logger.info(
2835
"Fetching Avro schema from {}".format(platform_schema_url))
2936
try:
30-
response = requests.get(platform_schema_url)
37+
38+
response = self.session.get(url=platform_schema_url,
39+
timeout=300)
3140
response.raise_for_status()
3241
except RequestException as e:
3342
self.logger.error(

tests/test_avro_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
from nypl_py_utils.classes.avro_client import (
5-
AvroClientError, AvroDecoder, AvroEncoder)
5+
AvroClient, AvroClientError, AvroDecoder, AvroEncoder)
66
from requests.exceptions import ConnectTimeout
77

88
_TEST_SCHEMA = {'data': {'schema': json.dumps({
@@ -25,17 +25,17 @@ class TestAvroClient:
2525
@pytest.fixture
2626
def test_avro_encoder_instance(self, requests_mock):
2727
requests_mock.get(
28-
'https://test_schema_url', text=json.dumps(_TEST_SCHEMA))
29-
return AvroEncoder('https://test_schema_url')
28+
"https://test_schema_url", text=json.dumps(_TEST_SCHEMA))
29+
return AvroEncoder("https://test_schema_url")
3030

3131
@pytest.fixture
3232
def test_avro_decoder_instance(self, requests_mock):
3333
requests_mock.get(
34-
'https://test_schema_url', text=json.dumps(_TEST_SCHEMA))
35-
return AvroDecoder('https://test_schema_url')
34+
"https://test_schema_url", text=json.dumps(_TEST_SCHEMA))
35+
return AvroDecoder("https://test_schema_url")
3636

37-
def test_get_json_schema(self, test_avro_encoder_instance,
38-
test_avro_decoder_instance):
37+
def test_get_json_schema_success(self, test_avro_encoder_instance,
38+
test_avro_decoder_instance):
3939
assert test_avro_encoder_instance.schema == _TEST_SCHEMA['data'][
4040
'schema']
4141
assert test_avro_decoder_instance.schema == _TEST_SCHEMA['data'][

0 commit comments

Comments
 (0)