Skip to content

Commit

Permalink
[Bug fixes] Further checking for the api wrapper response (#1499)
Browse files Browse the repository at this point in the history
* further checking for the api response

* make sure to pop the request_callers field first

* comment
  • Loading branch information
DayBr3ak authored and douglascamata committed Jul 28, 2016
1 parent bfe4f09 commit 47cf9db
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions pokemongo_bot/api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class ApiWrapper(object):
def __init__(self, api):
self._api = api
self.request_callers = []
self.reset_auth()

def reset_auth(self):
Expand All @@ -24,17 +25,42 @@ def _can_call(self):
raise NotLoggedInException()
return True

def _pop_request_callers(self):
r = self.request_callers
self.request_callers = []
return [i.upper() for i in r]

def _is_response_valid(self, result, request_callers):
if not result or result is None or not isinstance(result, dict):
return False

if not 'responses' in result or not 'status_code' in result:
return False

if not isinstance(result['responses'], dict):
return False

# the response can still programatically be valid at this point
# but still be wrong. we need to check if the server did sent what we asked it
for request_caller in request_callers:
if not request_caller in result['responses']:
return False

return True


def call(self, max_retry=5):
request_callers = self._pop_request_callers()
if not self._can_call():
return False
return False # currently this is never ran, exceptions are raised before

api_req_method_list = self._api._req_method_list
result = None
try_cnt = 0
while True:
self._api._req_method_list = [req_method for req_method in api_req_method_list] # api internally clear this field after a call
result = self._api.call()
if result is None:
if not self._is_response_valid(result, request_callers):
try_cnt += 1
logger.log('Server seems to be busy or offline - try again - {}/{}'.format(try_cnt, max_retry), 'red')

This comment has been minimized.

Copy link
@edelsbrunner

edelsbrunner Jul 28, 2016

lala
I keep getting this message between my log every 5 seconds now :)

This comment has been minimized.

Copy link
@DayBr3ak

DayBr3ak Jul 28, 2016

Author Contributor

I see it can be annoying, but at least it proves it works :D

if try_cnt >= max_retry:
Expand All @@ -49,6 +75,9 @@ def login(self, provider, username, password):

# fallback
def __getattr__(self, func):
DEFAULT_ATTRS = ['_position_lat', '_position_lng', '_auth_provider', '_api_endpoint', 'set_position', 'get_position']
if func not in DEFAULT_ATTRS:
self.request_callers.append(func)
return getattr(self._api, func)


Expand Down

2 comments on commit 47cf9db

@klingan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, works great so far πŸ‘

@Takkwon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this also for the map cells error??

Please sign in to comment.