Skip to content

Commit

Permalink
Added retry
Browse files Browse the repository at this point in the history
  • Loading branch information
fatimarahman committed Jul 30, 2024
1 parent a042e6e commit 5cec18d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/nypl_py_utils/classes/avro_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import avro.schema
import requests

Expand All @@ -16,8 +17,8 @@ class AvroClient:
"""

def __init__(self, platform_schema_url):
self.logger = create_log("avro_encoder")
retry_policy = Retry(total=2, backoff_factor=4,
self.logger = create_log("avro_client")
retry_policy = Retry(total=3, backoff_factor=45,
status_forcelist=[500, 502, 503, 504],
allowed_methods=frozenset(['GET']))
self.session = requests.Session()
Expand All @@ -38,7 +39,7 @@ def get_json_schema(self, platform_schema_url):
response = self.session.get(url=platform_schema_url,
timeout=300)
response.raise_for_status()
except RequestException as e:
except Exception as e:
self.logger.error(
"Failed to retrieve schema from {url}: {error}".format(
url=platform_schema_url, error=e
Expand All @@ -48,7 +49,7 @@ def get_json_schema(self, platform_schema_url):
"Failed to retrieve schema from {url}: {error}".format(
url=platform_schema_url, error=e
)
) from None
)

try:
json_response = response.json()
Expand Down
26 changes: 18 additions & 8 deletions tests/test_avro_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import pytest
from unittest.mock import patch

from nypl_py_utils.classes.avro_client import (
AvroClientError, AvroDecoder, AvroEncoder)
AvroClient, AvroClientError, AvroDecoder, AvroEncoder)
from requests import session
from requests.exceptions import ConnectTimeout

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

def test_get_json_schema_success(self, test_avro_encoder_instance,
test_avro_decoder_instance):
assert test_avro_encoder_instance.schema == _TEST_SCHEMA['data'][
'schema']
assert test_avro_decoder_instance.schema == _TEST_SCHEMA['data'][
'schema']
assert test_avro_encoder_instance.schema == _TEST_SCHEMA["data"][
"schema"]
assert test_avro_decoder_instance.schema == _TEST_SCHEMA["data"][
"schema"]

def test_request_error(self, requests_mock):
requests_mock.get('https://test_schema_url', exc=ConnectTimeout)
def test_get_json_schema_error(self, requests_mock):
requests_mock.get("https://test_schema_url", exc=ConnectTimeout)
with pytest.raises(AvroClientError):
AvroEncoder('https://test_schema_url')
AvroEncoder("https://test_schema_url")

def test_get_json_schema_success_on_retry(self, requests_mock):
requests_mock.get("https://test_schema_url",
[{"exc": ConnectionError},
{"text": str(_TEST_SCHEMA), "status_code": 200}])

test_avro_client = AvroClient("https://test_schema_url")
assert test_avro_client.get_json_schema == _TEST_SCHEMA["data"]["schema"]

def test_bad_json_error(self, requests_mock):
requests_mock.get(
Expand Down

0 comments on commit 5cec18d

Please sign in to comment.