@@ -31,6 +31,27 @@ def handle_error(resp):
3131 resp .raise_for_status ()
3232
3333
34+ def timeout_wrapper (func ):
35+ def timeout (* args , ** kwargs ):
36+ kwargs .setdefault ("timeout" , 60 )
37+ return func (* args , ** kwargs )
38+ return timeout
39+
40+
41+ def retry_and_timeout_wrapper (func ):
42+ def retry_logic_with_timeout (* args , ** kwargs ):
43+ kwargs .setdefault ("timeout" , 60 )
44+ retries = 0
45+ while retries < MAX_RETRIES :
46+ try :
47+ return func (* args , ** kwargs )
48+ except requests .exceptions .ReadTimeout :
49+ retries += 1
50+ if retries == MAX_RETRIES :
51+ raise
52+
53+ return retry_logic_with_timeout
54+
3455class IndexClient (object ):
3556
3657 def __init__ (self , baseurl , version = "v0" , auth = None ):
@@ -182,16 +203,8 @@ def list_with_params(self, limit=float("inf"), start=None, page_size=100, params
182203 reformatted_params .update ({"negate_params" : json .dumps (negate_params )})
183204 yielded = 0
184205 while True :
185- tries = 0
186- while tries < MAX_RETRIES :
187- try :
188- resp = self ._get ("index" , params = reformatted_params , timeout = 60 )
189- handle_error (resp )
190- break
191- except requests .exceptions .ReadTimeout :
192- tries += 1
193- if tries == MAX_RETRIES :
194- raise
206+ resp = self ._get ("index" , params = reformatted_params , timeout = 60 )
207+ handle_error (resp )
195208 json_str = resp .json ()
196209 if not json_str ["records" ]:
197210 return
@@ -305,21 +318,25 @@ def list_versions(self, did):
305318 versions .append (Document (self , version ["did" ], version ))
306319 return versions
307320
321+ @retry_and_timeout_wrapper
308322 def _get (self , * path , ** kwargs ):
309323 resp = requests .get (self .url_for (* path ), ** kwargs )
310324 handle_error (resp )
311325 return resp
312326
327+ @timeout_wrapper
313328 def _post (self , * path , ** kwargs ):
314329 resp = requests .post (self .url_for (* path ), ** kwargs )
315330 handle_error (resp )
316331 return resp
317332
333+ @timeout_wrapper
318334 def _put (self , * path , ** kwargs ):
319335 resp = requests .put (self .url_for (* path ), ** kwargs )
320336 handle_error (resp )
321337 return resp
322338
339+ @timeout_wrapper
323340 def _delete (self , * path , ** kwargs ):
324341 resp = requests .delete (self .url_for (* path ), ** kwargs )
325342 handle_error (resp )
0 commit comments