@@ -31,10 +31,16 @@ def __init__(
31
31
)
32
32
self .account_key = account_key
33
33
34
- def _get_api_from_relative_path (self , path , params = None , headers = None ):
34
+ def _get_api_from_relative_path (
35
+ self , path , params = None , headers = None , backoff_index = None
36
+ ):
35
37
full_url = urljoin (self .base_url , path )
36
38
logger .info ("Exec: %s" , full_url )
37
39
40
+ backoff_delays = [5 , 10 , 20 , 60 , 180 ]
41
+ if backoff_index is None :
42
+ backoff_index = 0
43
+
38
44
# Insert account switch key
39
45
if self .account_key is not None :
40
46
if params is None :
@@ -45,6 +51,33 @@ def _get_api_from_relative_path(self, path, params=None, headers=None):
45
51
if sleepy_seconds :
46
52
time .sleep (sleepy_seconds )
47
53
response = self .session .get (full_url , params = params , headers = headers )
54
+ # Retry once for temporary 500 errors
55
+ if response .status_code == 500 :
56
+ logger .error (f"Received 500 response for 'GET { full_url } '. Retrying..." )
57
+ response = self .session .get (full_url , params = params , headers = headers )
58
+
59
+ # Back off for rate limit 429
60
+ if response .status_code == 429 :
61
+ # Increase bacoff for next attempt
62
+ next_backoff_index = backoff_index + 1
63
+ if next_backoff_index > len (backoff_delays ):
64
+ logger .error (
65
+ f"Received 429 response for 'GET { full_url } ' and backoff limit exceeded."
66
+ )
67
+ else :
68
+ backoff = backoff_delays [backoff_index ]
69
+ logger .error (
70
+ f"Received 429 response for 'GET { full_url } '. Waiting for { backoff } seconds before retrying"
71
+ )
72
+ time .sleep (backoff )
73
+ response = self ._get_api_from_relative_path (
74
+ path ,
75
+ params = params ,
76
+ headers = headers ,
77
+ backoff_index = next_backoff_index ,
78
+ )
79
+
80
+ # Return body if 200
48
81
if response .status_code == 200 :
49
82
return response .json ()
50
83
self .error_count += 1
0 commit comments