mdrs-client-python/mdrsclient/api/utils.py

20 lines
694 B
Python
Raw Normal View History

2023-05-01 20:00:32 +09:00
from mdrsclient.api.user import UserApi
from mdrsclient.connection import MDRSConnection
2023-05-01 20:00:32 +09:00
from mdrsclient.exceptions import UnauthorizedException
def token_check(connection: MDRSConnection) -> None:
try:
connection.lock.acquire()
if connection.token is not None:
if connection.token.is_refresh_required:
user_api = UserApi(connection)
try:
connection.token = user_api.refresh(connection.token)
except UnauthorizedException:
connection.logout()
elif connection.token.is_expired:
connection.logout()
finally:
connection.lock.release()