-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bug fixes] Further checking for the api wrapper response (#1499)
* further checking for the api response * make sure to pop the request_callers field first * comment
- Loading branch information
1 parent
bfe4f09
commit 47cf9db
Showing
1 changed file
with
31 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
class ApiWrapper(object): | ||
def __init__(self, api): | ||
self._api = api | ||
self.request_callers = [] | ||
self.reset_auth() | ||
|
||
def reset_auth(self): | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
DayBr3ak
Author
Contributor
|
||
if try_cnt >= max_retry: | ||
|
@@ -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) | ||
|
||
|
||
|
2 comments
on commit 47cf9db
There was a problem hiding this comment.
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 π
There was a problem hiding this comment.
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??
I keep getting this message between my log every 5 seconds now :)