-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] Api Wrapper to handle connection issues #1459
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9da1192
add an api wrapper managing (trying to) handle connections error, nee…
DayBr3ak 1a10ea5
refine error testing
DayBr3ak cd6d958
import fix
DayBr3ak abd2aec
sleep less, lazy bum
DayBr3ak 959a927
change retry parameter as an optional argument
DayBr3ak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# api_wrapper.py | ||
|
||
from pgoapi import PGoApi | ||
from pgoapi.exceptions import NotLoggedInException | ||
from human_behaviour import sleep | ||
import logger | ||
|
||
class ApiWrapper(object): | ||
def __init__(self, api): | ||
self._api = api | ||
self.reset_auth() | ||
|
||
def reset_auth(self): | ||
self._api._auth_token = None | ||
self._api._auth_provider = None | ||
self._api._api_endpoint = None | ||
|
||
# wrap api methods | ||
def _can_call(self): | ||
if not self._api._req_method_list or len(self._api._req_method_list) == 0: | ||
raise RuntimeError('Trying to call the api without setting any request') | ||
if self._api._auth_provider is None or not self._api._auth_provider.is_login(): | ||
logger.log('Not logged in!', 'red') | ||
raise NotLoggedInException() | ||
return True | ||
|
||
def call(self, max_retry=5): | ||
if not self._can_call(): | ||
return False | ||
|
||
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: | ||
try_cnt += 1 | ||
logger.log('Server seems to be busy or offline - try again - {}/{}'.format(try_cnt, max_retry), 'red') | ||
if try_cnt >= max_retry: | ||
raise ServerBusyOrOfflineException() | ||
sleep(1) | ||
else: | ||
break | ||
return result | ||
|
||
def login(self, provider, username, password): | ||
return self._api.login(provider, username, password) | ||
|
||
# fallback | ||
def __getattr__(self, func): | ||
return getattr(self._api, func) | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Worth wrapping in a
try...except
block perhaps? Exceptions can be raised from here still.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.
What else can be raised here if I may ask?
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.
Well I am not familiar with the underlying API. I'd say whenever you call a function, there's a risk of an Exception being raised (maybe not even in the current API release). So it's a matter of deciding if you want to harden your API wrapper to handle those errors or not.
Any of the core maintainers care to share their opinions?