Skip to content

Commit 70a8ed1

Browse files
tehmufifnmanlockwooddev
authored andcommitted
Add functionality to retry failed connections if user desires (#10)
#10 - Add retry functionality
1 parent 1a7dea5 commit 70a8ed1

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@
2222
## 2.2.0 (25-12-2018)
2323

2424
* Add new Game Data API's
25+
26+
## 2.2.1 (16-02-2019)
27+
28+
* Add functionality to retry failed connections

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup, find_packages
44

55

6-
__version__ = '2.2.0'
6+
__version__ = '2.2.1'
77

88

99
def read(*parts):

wowapi/api.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from datetime import datetime, timedelta
33

44
import requests
5+
from requests.adapters import HTTPAdapter
56
from requests.exceptions import RequestException
7+
from requests.packages.urllib3.util.retry import Retry
68

79
from .exceptions import WowApiException, WowApiOauthException
810

9-
1011
logger = logging.getLogger('wowapi')
1112
logger.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

Comments
 (0)