55from avro .io import BinaryDecoder , BinaryEncoder , DatumReader , DatumWriter
66from io import BytesIO
77from nypl_py_utils .functions .log_helper import create_log
8- from requests .exceptions import JSONDecodeError , RequestException
8+ from requests .adapters import HTTPAdapter , Retry
9+ from requests .exceptions import JSONDecodeError
910
1011
1112class AvroClient :
@@ -15,7 +16,13 @@ class AvroClient:
1516 """
1617
1718 def __init__ (self , platform_schema_url ):
18- self .logger = create_log ("avro_encoder" )
19+ self .logger = create_log ("avro_client" )
20+ retry_policy = Retry (total = 3 , backoff_factor = 45 ,
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,9 +34,11 @@ 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 = 60 )
3140 response .raise_for_status ()
32- except RequestException as e :
41+ except Exception as e :
3342 self .logger .error (
3443 "Failed to retrieve schema from {url}: {error}" .format (
3544 url = platform_schema_url , error = e
@@ -39,7 +48,7 @@ def get_json_schema(self, platform_schema_url):
3948 "Failed to retrieve schema from {url}: {error}" .format (
4049 url = platform_schema_url , error = e
4150 )
42- ) from None
51+ )
4352
4453 try :
4554 json_response = response .json ()
0 commit comments