Skip to content

Commit 1c76fb2

Browse files
committed
make _check_token function more readable
1 parent 53a3380 commit 1c76fb2

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

mergin/client.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,35 @@ def user_agent_info(self):
192192
system_version = platform.mac_ver()[0]
193193
return f"{self.client_version} ({platform.system()}/{system_version})"
194194

195-
def _check_token(f):
196-
"""Wrapper for creating/renewing authorization token."""
195+
def validate_auth(self):
196+
"""Validate that client has valid auth token or can be logged in."""
197197

198-
def wrapper(self, *args, **kwargs):
199-
if self._auth_session:
200-
# Refresh auth token if it expired or will expire very soon
201-
delta = self._auth_session["expire"] - datetime.now(timezone.utc)
202-
if delta.total_seconds() < 5:
203-
self.log.info("Token has expired - refreshing...")
204-
if self._auth_params.get("login", None) and self._auth_params.get("password", None):
205-
self.log.info("Token has expired - refreshing...")
206-
self.login(self._auth_params["login"], self._auth_params["password"])
207-
else:
208-
raise AuthTokenExpiredError("Token has expired - please re-login")
209-
else:
210-
# Create a new authorization token
211-
self.log.info(f"No token - login user: {self._auth_params.get('login', None)}")
198+
if self._auth_session:
199+
# Refresh auth token if it expired or will expire very soon
200+
delta = self._auth_session["expire"] - datetime.now(timezone.utc)
201+
if delta.total_seconds() < 5:
202+
self.log.info("Token has expired - refreshing...")
212203
if self._auth_params.get("login", None) and self._auth_params.get("password", None):
204+
self.log.info("Token has expired - refreshing...")
213205
self.login(self._auth_params["login"], self._auth_params["password"])
214206
else:
215-
raise ClientError("Missing login or password")
207+
raise AuthTokenExpiredError("Token has expired - please re-login")
208+
else:
209+
# Create a new authorization token
210+
self.log.info(f"No token - login user: {self._auth_params.get('login', None)}")
211+
if self._auth_params.get("login", None) and self._auth_params.get("password", None):
212+
self.login(self._auth_params["login"], self._auth_params["password"])
213+
else:
214+
raise ClientError("Missing login or password")
215+
216+
@staticmethod
217+
def _check_token(f):
218+
"""Wrapper for creating/renewing authorization token.
219+
Every function that requires authentication should be decorated with this as @_check_token."""
220+
221+
def wrapper(self, *args, **kwargs):
222+
# functions that run prior to required function using this decorator
223+
self.validate_auth()
216224

217225
return f(self, *args, **kwargs)
218226

0 commit comments

Comments
 (0)