From be02e9ed60a0c4f45194de970ac7ba6619476b5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Feb 2025 11:50:34 +0200 Subject: [PATCH 1/4] Use TOKEN_REFRESH_MARGIN constant --- trakt/api.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/trakt/api.py b/trakt/api.py index fc1d602..32a4b36 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -168,6 +168,9 @@ class TokenAuth(AuthBase): #: How many times to attempt token auth refresh before failing MAX_RETRIES = 1 + # Time margin before token expiry when refresh should be triggered + TOKEN_REFRESH_MARGIN = timedelta(minutes=10) + logger = logging.getLogger(__name__) def __init__(self, client: HttpClient, config: AuthConfig): @@ -214,7 +217,8 @@ def validate_token(self): current = datetime.now(tz=timezone.utc) expires_at = datetime.fromtimestamp(self.config.OAUTH_EXPIRES_AT, tz=timezone.utc) - if expires_at - current > timedelta(minutes=10): + margin = expires_at - current + if margin > self.TOKEN_REFRESH_MARGIN: self.OAUTH_TOKEN_VALID = True else: self.refresh_token() From b42c7d3bdf6e49e3fe1153080a9d5c550bff33ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Feb 2025 11:50:57 +0200 Subject: [PATCH 2/4] Update validate_token doc --- trakt/api.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/trakt/api.py b/trakt/api.py index 32a4b36..de05b9c 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -213,7 +213,12 @@ def get_token(self): ] def validate_token(self): - """Check if current OAuth token has not expired""" + """Check if current OAuth token has not expired + + The token is considered valid if it expires in more than TOKEN_REFRESH_MARGIN + (default: 10 minutes). This margin ensures the token doesn't expire during + critical operations while also maximizing the token's useful lifetime. + """ current = datetime.now(tz=timezone.utc) expires_at = datetime.fromtimestamp(self.config.OAUTH_EXPIRES_AT, tz=timezone.utc) From 6b5aac27bbc011727dfc1270f693e03def27ead2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Feb 2025 11:51:22 +0200 Subject: [PATCH 3/4] Log expiring message --- trakt/api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/trakt/api.py b/trakt/api.py index de05b9c..ba2ccdb 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -226,6 +226,7 @@ def validate_token(self): if margin > self.TOKEN_REFRESH_MARGIN: self.OAUTH_TOKEN_VALID = True else: + self.logger.debug("Token expires in %s, refreshing (margin: %s)", margin, self.TOKEN_REFRESH_MARGIN) self.refresh_token() def refresh_token(self): From 35084a7735b0465de9c30f285e6e09b4b2a70ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Feb 2025 12:49:35 +0200 Subject: [PATCH 4/4] Ensure timedelta is calculated on each request The TokenAuth class instance may be long lived --- trakt/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trakt/api.py b/trakt/api.py index ba2ccdb..e730631 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -169,7 +169,7 @@ class TokenAuth(AuthBase): MAX_RETRIES = 1 # Time margin before token expiry when refresh should be triggered - TOKEN_REFRESH_MARGIN = timedelta(minutes=10) + TOKEN_REFRESH_MARGIN = {'minutes': 10} logger = logging.getLogger(__name__) @@ -223,7 +223,7 @@ def validate_token(self): current = datetime.now(tz=timezone.utc) expires_at = datetime.fromtimestamp(self.config.OAUTH_EXPIRES_AT, tz=timezone.utc) margin = expires_at - current - if margin > self.TOKEN_REFRESH_MARGIN: + if margin > timedelta(**self.TOKEN_REFRESH_MARGIN): self.OAUTH_TOKEN_VALID = True else: self.logger.debug("Token expires in %s, refreshing (margin: %s)", margin, self.TOKEN_REFRESH_MARGIN)