2
2
from datetime import datetime , timedelta
3
3
4
4
import requests
5
+ from requests .adapters import HTTPAdapter
5
6
from requests .exceptions import RequestException
7
+ from requests .packages .urllib3 .util .retry import Retry
6
8
7
9
from .exceptions import WowApiException , WowApiOauthException
8
10
9
-
10
11
logger = logging .getLogger ('wowapi' )
11
12
logger .addHandler (logging .NullHandler ())
12
13
@@ -15,17 +16,29 @@ class WowApi(object):
15
16
16
17
__base_url = '{0}.api.blizzard.com'
17
18
18
- def __init__ (self , client_id , client_secret ):
19
+ def __init__ (self , client_id , client_secret , retry_conn_failures = False ):
19
20
self ._client_id = client_id
20
21
self ._client_secret = client_secret
21
22
22
23
self ._session = requests .Session ()
23
24
25
+ # Use default retry setup
26
+ if retry_conn_failures :
27
+ self .retry_conn_failures ()
28
+
24
29
self ._access_tokens = {}
25
30
26
31
def _utcnow (self ):
27
32
return datetime .utcnow ()
28
33
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
+
29
42
def _get_client_credentials (self , region ):
30
43
path = '/oauth/token?grant_type=client_credentials&client_id={0}&client_secret={1}' .format (
31
44
self ._client_id , self ._client_secret
0 commit comments