Skip to content

Commit

Permalink
Fixed bool argument error in auth ticket check
Browse files Browse the repository at this point in the history
  • Loading branch information
tejado committed Aug 1, 2016
1 parent 2ef445b commit d0e1447
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
6 changes: 6 additions & 0 deletions pgoapi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ class EmptySubrequestChainException(Exception):
pass

class ServerSideRequestThrottlingException(Exception):
pass

class ServerSideAccessForbiddenException(Exception):
pass

class UnexpectedResponseException(Exception):
pass
40 changes: 26 additions & 14 deletions pgoapi/rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from importlib import import_module

from pgoapi.protobuf_to_dict import protobuf_to_dict
from pgoapi.exceptions import NotLoggedInException, ServerBusyOrOfflineException, ServerSideRequestThrottlingException
from pgoapi.exceptions import NotLoggedInException, ServerBusyOrOfflineException, ServerSideRequestThrottlingException, ServerSideAccessForbiddenException, UnexpectedResponseException
from pgoapi.utilities import f2i, h2f, to_camel_case, get_time_ms, get_format_time_diff

from . import protos
Expand Down Expand Up @@ -105,7 +105,25 @@ def request(self, endpoint, subrequests, player_position):

response_dict = self._parse_main_response(response, subrequests)

if ('auth_ticket' in response_dict) and ('expire_timestamp_ms' in response_dict['auth_ticket']) and (self._auth_provider.is_new_ticket(response_dict['auth_ticket']['expire_timestamp_ms'])):
self.check_authentication(response_dict)

"""
some response validations
"""
if isinstance(response_dict, dict) and 'status_code' in response_dict:
sc = response_dict['status_code']
if sc == 102:
raise NotLoggedInException()
elif sc == 52:
raise ServerSideRequestThrottlingException("Request throttled by server... slow down man")

return response_dict

def check_authentication(self, response_dict):
if isinstance(response_dict, dict) and ('auth_ticket' in response_dict) and \
('expire_timestamp_ms' in response_dict['auth_ticket']) and \
(self._auth_provider.is_new_ticket(response_dict['auth_ticket']['expire_timestamp_ms'])):

had_ticket = self._auth_provider.has_ticket()

auth_ticket = response_dict['auth_ticket']
Expand All @@ -120,15 +138,6 @@ def request(self, endpoint, subrequests, player_position):
else:
self.log.debug('Received auth ticket valid for %02d:%02d:%02d hours (%s < %s)', h, m, s, now_ms, auth_ticket['expire_timestamp_ms'])

if isinstance(response_dict, dict) and 'status_code' in response_dict:
sc = response_dict['status_code']
if sc == 102:
raise NotLoggedInException()
elif sc == 52:
raise ServerSideRequestThrottlingException("Request throttled by server... slow down man")

return response_dict

def _build_main_request(self, subrequests, player_position = None):
self.log.debug('Generating main RPC request...')

Expand Down Expand Up @@ -219,10 +228,13 @@ def _build_sub_requests(self, mainrequest, subrequest_list):
def _parse_main_response(self, response_raw, subrequests):
self.log.debug('Parsing main RPC response...')

if response_raw.status_code != 200:
self.log.warning('Unexpected HTTP server response - needs 200 got %s', response_raw.status_code)
if response_raw.status_code == 403:
raise ServerSideAccessForbiddenException("Seems your IP Address is banned or something else went badly wrong...")
elif response_raw.status_code != 200:
error = 'Unexpected HTTP server response - needs 200 got {}'.format(response_raw.status_code)
self.log.warning(error)
self.log.debug('HTTP output: \n%s', response_raw.content.decode('utf-8'))
return False
raise UnexpectedResponseException(error)

if response_raw.content is None:
self.log.warning('Empty server response!')
Expand Down
4 changes: 2 additions & 2 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def main():
# ----------------------
response_dict = api.get_player()
print('Response dictionary (get_player): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
# sleep 200ms due to server-side throttling

# sleep due to server-side throttling
time.sleep(0.2)

# get player profile + inventory call (thread-safe/chaining example)
Expand Down

0 comments on commit d0e1447

Please sign in to comment.