66import atexit
77import string
88from pathlib import Path
9+ import time
910
1011script_dir = os .path .dirname (os .path .abspath (__file__ ))
1112cache_dir = os .path .join (str (Path .home ()), '.cache' , 'raft' )
@@ -80,6 +81,8 @@ def __init__(self, endpoint, client_id, tenant_id, secret):
8081 self .tenant_id = tenant_id
8182 self .secret = secret
8283
84+ self .retry_status_code = [503 ]
85+
8386 def auth_header (self ):
8487 token = get_auth_token (self .client_id , self .tenant_id , self .secret )
8588 if 'error_description' in token :
@@ -88,27 +91,51 @@ def auth_header(self):
8891 'Authorization' : f"{ token ['token_type' ]} { token ['access_token' ]} "
8992 }
9093
91- def post (self , relative_url , json_data ):
92- return requests .post (
94+ def post (self , relative_url , json_data , seconds_to_wait = 10 ):
95+ response = requests .post (
9396 self .endpoint + relative_url ,
9497 json = json_data ,
9598 headers = self .auth_header ())
99+ if (response .status_code in self .retry_status_code and
100+ seconds_to_wait > 0.0 ):
101+ time .sleep (2.0 )
102+ return self .post (relative_url , json_data , seconds_to_wait - 2.0 )
103+ else :
104+ return response
96105
97- def put (self , relative_url , json_data ):
98- return requests .put (
106+ def put (self , relative_url , json_data , seconds_to_wait = 10 ):
107+ response = requests .put (
99108 self .endpoint + relative_url ,
100109 json = json_data ,
101110 headers = self .auth_header ())
111+ if (response .status_code in self .retry_status_code and
112+ seconds_to_wait > 0.0 ):
113+ time .sleep (2.0 )
114+ return self .put (relative_url , json_data , seconds_to_wait - 2.0 )
115+ else :
116+ return response
102117
103- def delete (self , relative_url ):
104- return requests .delete (
118+ def delete (self , relative_url , seconds_to_wait = 10 ):
119+ response = requests .delete (
105120 self .endpoint + relative_url ,
106121 headers = self .auth_header ())
122+ if (response .status_code in self .retry_status_code and
123+ seconds_to_wait > 0.0 ):
124+ time .sleep (2.0 )
125+ return self .delete (relative_url , seconds_to_wait - 2.0 )
126+ else :
127+ return response
107128
108- def get (self , relative_url ):
109- return requests .get (
129+ def get (self , relative_url , seconds_to_wait = 10 ):
130+ response = requests .get (
110131 self .endpoint + relative_url ,
111132 headers = self .auth_header ())
133+ if (response .status_code in self .retry_status_code and
134+ seconds_to_wait > 0.0 ):
135+ time .sleep (2.0 )
136+ return self .get (relative_url , seconds_to_wait - 2.0 )
137+ else :
138+ return response
112139
113140
114141class RaftDefinitions ():
0 commit comments