Skip to content
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

Refresh login only when needed #50

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pori_python/graphkb/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,24 @@ def request(self, endpoint: str, method: str = "GET", **kwargs) -> Dict:
# about catching OSError as well as ConnectionError:
# https://stackoverflow.com/questions/74253820
attempts = range(15)
need_refresh_login = False
for attempt in attempts:
if attempt > 0:
time.sleep(2) # wait between retries
try:
self.refresh_login()

if need_refresh_login:
self.refresh_login()
need_refresh_login = False

self.request_count += 1
resp = requests.request(
method, url, headers=self.headers, timeout=timeout, **kwargs
)
if resp.status_code == 401 or resp.status_code == 403:
logger.debug(f"/{endpoint} - {resp.status_code} - retrying")
# try to re-login if the token expired
need_refresh_login = True
continue
else:
break
Expand Down