Skip to content

Commit

Permalink
Don't treat key as expired until repeated failures
Browse files Browse the repository at this point in the history
Wait for a key to receive code 400 ten consecutive times before raising
ExpiredHashkeyException. Increase hash retries from 2 to 3.
  • Loading branch information
Noctem committed Jul 8, 2017
1 parent ae01961 commit 8c78f21
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion aiopogo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__title__ = 'aiopogo'
__version__ = '2.0.2'
__version__ = '2.0.3'
__author__ = 'David Christenson'
__license__ = 'MIT License'
__copyright__ = 'Copyright (c) 2017 David Christenson <https://github.com/Noctem>'
Expand Down
45 changes: 26 additions & 19 deletions aiopogo/hash_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,32 @@ async def hash(self, timestamp, latitude, longitude, accuracy, authticket, sessi
}

# request hashes from hashing server
for attempt in range(2):
for attempt in range(3):
try:
async with self._session.post("http://pokehash.buddyauth.com/api/v137_1/hash", headers=headers, json=payload) as resp:
if resp.status == 400:
response = await resp.text()
if response == 'Unauthorized':
if self.multi:
self.log.warning(
'{:.10}... expired, removing from rotation.'.format(
self.instance_token))
self.remove_token(self.instance_token)
self.instance_token = self.auth_token
if attempt < 1:
headers = {'X-AuthToken': self.instance_token}
continue
return await self.hash(timestamp, latitude, longitude, accuracy, authticket, sessiondata, requests)
raise ExpiredHashKeyException("{:.10}... appears to have expired.".format(self.instance_token))
raise BadHashRequestException('400 was returned from the hashing server with the message: {}'.format(response))
status['failures'] += 1

if status['failures'] < 10:
if attempt < 2:
await sleep(1.0)
continue
raise BadHashRequestException('400 was returned from the hashing server.')

if self.multi:
self.log.warning(
'{:.10}... expired, removing from rotation.'.format(
self.instance_token))
self.remove_token(self.instance_token)
self.instance_token = self.auth_token
if attempt < 2:
headers = {'X-AuthToken': self.instance_token}
continue
return await self.hash(timestamp, latitude, longitude, accuracy, authticket, sessiondata, requests)
raise ExpiredHashKeyException("{:.10}... appears to have expired.".format(self.instance_token))

resp.raise_for_status()
status['failures'] = 0

response = await resp.json(encoding='ascii', loads=json_loads)
headers = resp.headers
Expand All @@ -94,14 +101,14 @@ async def hash(self, timestamp, latitude, longitude, accuracy, authticket, sessi
except ValueError as e:
raise MalformedHashResponseException('Unable to parse JSON from hash server.') from e
except (TimeoutError, ServerConnectionError, ServerTimeoutError) as e:
if attempt < 1:
if attempt < 2:
self.log.info('Hashing request timed out.')
await sleep(1.5)
else:
raise HashingTimeoutException('Hashing request timed out.') from e
except ClientError as e:
error = '{} during hashing. {}'.format(e.__class__.__name__, e)
if attempt < 1:
if attempt < 2:
self.log.info(error)
else:
raise HashingOfflineException(error) from e
Expand Down Expand Up @@ -174,8 +181,8 @@ def set_token(cls, token):
cls._tokens = cycle(token)
cls.auth_token = cls._multi_token
cls.multi = len(token)
cls.key_statuses = {t: {} for t in token}
cls.key_statuses = {t: {'failures': 0} for t in token}
cls.key_status = cls._multi_status
else:
cls.auth_token = token
cls.key_status = {}
cls.key_status = {'failures': 0}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
author='David Christenson',
author_email='mail@noctem.xyz',
description='Asynchronous Pokemon API lib',
version='2.0.2',
version='2.0.3',
url='https://github.com/Noctem/aiopogo',
packages=find_packages(),
install_requires=[
Expand Down

0 comments on commit 8c78f21

Please sign in to comment.