22
33
44import importlib
5+ import time
56
67try :
78 from urlparse import urljoin
@@ -25,11 +26,23 @@ def build(credentials, api_version='0.3'):
2526 NotImplementedError ('Grant type not implemented.' )
2627
2728
29+ def retry_request (make_request , retry_status_list , tries ):
30+ for i in range (tries ):
31+ response = make_request ()
32+ if response .status_code not in retry_status_list :
33+ break
34+ wait = 2 ** i
35+ time .sleep (wait )
36+ response .raise_for_status ()
37+ return response
38+
39+
2840class BaseClient (object ):
2941
3042 def __init__ (self , client_id , client_secret ,
3143 grant_type = None , storage_class = config .TOKEN_STORAGE_CLASS ,
32- token_uri = config .TOKEN_URI , api_uri = config .MERGADO_API_URI ):
44+ token_uri = config .TOKEN_URI , api_uri = config .MERGADO_API_URI ,
45+ retry_status_list = (500 , 502 , 503 , 504 ), tries = 3 ):
3346
3447 self .client_id = client_id
3548 self .client_secret = client_secret
@@ -39,6 +52,8 @@ def __init__(self, client_id, client_secret,
3952 self .storage = self .TokenStorage ()
4053 self .token_uri = token_uri
4154 self .api_uri = api_uri
55+ self .retry_status_list = retry_status_list
56+ self .tries = tries
4257
4358 @property
4459 def _token_headers (self ):
@@ -70,9 +85,13 @@ def get_url(self, path):
7085 return urljoin (self .api_uri , path )
7186
7287 def request (self , method , path , ** options ):
73- response = http .request (method , self .get_url (path ),
74- headers = self ._token_headers , ** options )
75- response .raise_for_status ()
88+ def make_request ():
89+ return http .request (
90+ method , self .get_url (path ),
91+ headers = self ._token_headers , ** options )
92+
93+ response = retry_request (
94+ make_request , self .retry_status_list , self .tries )
7695 return response .json ()
7796
7897 def get (self , path , ** options ):
0 commit comments