@@ -10,9 +10,9 @@ class Oauth2ApiClient:
1010 """
1111 Client for interacting with an Oauth2 authenticated API such as NYPL's
1212 Platform API endpoints. Note with_retries is a boolean flag which
13- determines if empty get requests will be retried 3 times or until
14- they are successful. This is to address a known issue with the Sierra
15- API where empty responses are returned intermittently.
13+ determines if empty get requests will be retried self.MAX_RETRIES times or
14+ until they are successful. This is to address a known issue with the Sierra
15+ API where empty and misformed responses are returned intermittently.
1616 """
1717
1818 def __init__ (self , client_id = None , client_secret = None , base_url = None ,
@@ -32,6 +32,8 @@ def __init__(self, client_id=None, client_secret=None, base_url=None,
3232
3333 self .with_retries = with_retries
3434
35+ self .MAX_RETRIES = 3
36+
3537 def get (self , request_path , ** kwargs ):
3638 """
3739 Issue an HTTP GET on the given request_path
@@ -40,7 +42,7 @@ def get(self, request_path, **kwargs):
4042 if resp .json () is None and self .with_retries is True :
4143 retries = \
4244 kwargs .get ('retries' , 0 ) + 1
43- if retries < 3 :
45+ if retries < self . MAX_RETRIES :
4446 self .logger .warning (
4547 f'Retrying get request due to empty response from\
4648 Oauth2 Client using path: { request_path } . \
@@ -50,8 +52,9 @@ def get(self, request_path, **kwargs):
5052 resp = self .get (request_path , ** kwargs )
5153 else :
5254 resp = Response ()
53- resp .message = 'Oauth2 Client: Request failed after 3 \
54- empty responses received from Oauth2 Client'
55+ resp .message = f'Oauth2 Client: Request failed after \
56+ { self .MAX_RETRIES } empty responses received \
57+ from Oauth2 Client'
5558 resp .status_code = 500
5659 return resp
5760
@@ -98,7 +101,7 @@ def _do_http_method(self, method, request_path, **kwargs):
98101 # Raise error after 3 successive token refreshes
99102 kwargs ['_do_http_method_token_refreshes' ] = \
100103 kwargs .get ('_do_http_method_token_refreshes' , 0 ) + 1
101- if kwargs ['_do_http_method_token_refreshes' ] > 3 :
104+ if kwargs ['_do_http_method_token_refreshes' ] > self . MAX_RETRIES :
102105 raise Oauth2ApiClientError ('Exhausted token refreshes' ) \
103106 from None
104107
0 commit comments