22from datetime import datetime , timedelta
33
44import requests
5+ from requests .adapters import HTTPAdapter
56from requests .exceptions import RequestException
7+ from requests .packages .urllib3 .util .retry import Retry
68
79from .exceptions import WowApiException , WowApiOauthException
810
9-
1011logger = logging .getLogger ('wowapi' )
1112logger .addHandler (logging .NullHandler ())
1213
@@ -15,17 +16,29 @@ class WowApi(object):
1516
1617 __base_url = '{0}.api.blizzard.com'
1718
18- def __init__ (self , client_id , client_secret ):
19+ def __init__ (self , client_id , client_secret , retry_conn_failures = False ):
1920 self ._client_id = client_id
2021 self ._client_secret = client_secret
2122
2223 self ._session = requests .Session ()
2324
25+ # Use default retry setup
26+ if retry_conn_failures :
27+ self .retry_conn_failures ()
28+
2429 self ._access_tokens = {}
2530
2631 def _utcnow (self ):
2732 return datetime .utcnow ()
2833
34+ def retry_conn_failures (self , total = 5 , backoff_factor = 1 ,
35+ status_forcelist = [443 , 500 , 502 , 503 , 504 ]):
36+ # Allows a user to control how retries function
37+ retries = Retry (total = total , backoff_factor = backoff_factor ,
38+ status_forcelist = status_forcelist )
39+ self ._session .mount ('http://' , HTTPAdapter (max_retries = retries ))
40+ self ._session .mount ('https://' , HTTPAdapter (max_retries = retries ))
41+
2942 def _get_client_credentials (self , region ):
3043 path = '/oauth/token?grant_type=client_credentials&client_id={0}&client_secret={1}' .format (
3144 self ._client_id , self ._client_secret
0 commit comments