From 15dbcc26058d3b7bf4f984c9b20e6bd7b17852b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 12 Jan 2025 20:08:43 +0200 Subject: [PATCH 1/4] Fix oauth/token path --- trakt/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trakt/api.py b/trakt/api.py index d12d31e5..31d9f1f3 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -230,7 +230,7 @@ def refresh_token(self): } try: - response = self.client.post('/oauth/token', data) + response = self.client.post('oauth/token', data) except OAuthException: self.logger.debug( "Rejected - Unable to refresh expired OAuth token, " From b6ed4b42ee020427f22b1873e7e8db9c4eba4731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 12 Jan 2025 20:32:37 +0200 Subject: [PATCH 2/4] Add max retries to token refresh Suggestion posted by coderabbit: - https://github.com/glensc/python-pytrakt/pull/59#pullrequestreview-2545486419 --- trakt/api.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/trakt/api.py b/trakt/api.py index 31d9f1f3..960c3416 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -172,12 +172,16 @@ class TokenAuth(AuthBase): #: The OAuth2 Redirect URI for your OAuth Application REDIRECT_URI: str = 'urn:ietf:wg:oauth:2.0:oob' + #: How many times to attempt token auth refresh before failing + MAX_RETRIES = 3 + def __init__(self, client: HttpClient, config: AuthConfig): super().__init__() self.config = config self.client = client # OAuth token validity checked self.OAUTH_TOKEN_VALID = None + self.refresh_attempts = 0 self.logger = logging.getLogger('trakt.api.token_auth') def __call__(self, r): @@ -220,6 +224,11 @@ def validate_token(self): def refresh_token(self): """Request Trakt API for a new valid OAuth token using refresh_token""" + if self.refresh_attempts >= self.MAX_RETRIES: + self.logger.error("Max token refresh attempts reached. Manual intervention required.") + return + self.refresh_attempts += 1 + self.logger.info("OAuth token has expired, refreshing now...") data = { 'client_id': self.config.CLIENT_ID, @@ -231,6 +240,7 @@ def refresh_token(self): try: response = self.client.post('oauth/token', data) + self.refresh_attempts = 0 except OAuthException: self.logger.debug( "Rejected - Unable to refresh expired OAuth token, " From 29921e717f79a99b39503594d7dc753dbb1fbfe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 13 Jan 2022 11:18:51 +0200 Subject: [PATCH 3/4] Add api test --- tests/test_api.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/test_api.py diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 00000000..1d83b7aa --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,17 @@ +from trakt.api import HttpClient +from trakt.core import api +from trakt.tv import TVShow + + +def test_api_singleton(): + """Test that api() returns the same HttpClient instance when called multiple times.""" + api1 = api() + api2 = api() + assert isinstance(api1, HttpClient), "api() should return an HttpClient instance" + assert api1 == api2, "Multiple calls to api() should return the same instance" + + +def test_tvshow_properties(): + show = TVShow("Game of Thrones") + assert show.title == "Game of Thrones" + assert show.certification == "TV-MA" From 02f49acd6d4bd5f829b527c73d31602321983849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 12 Jan 2025 20:50:59 +0200 Subject: [PATCH 4/4] Set max retries to 1 Getting banned by trakt api if doing more than once --- trakt/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trakt/api.py b/trakt/api.py index 960c3416..53b0b0af 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -173,7 +173,7 @@ class TokenAuth(AuthBase): REDIRECT_URI: str = 'urn:ietf:wg:oauth:2.0:oob' #: How many times to attempt token auth refresh before failing - MAX_RETRIES = 3 + MAX_RETRIES = 1 def __init__(self, client: HttpClient, config: AuthConfig): super().__init__()