From 702600c091cde08fe70973370c61806cd050dcc0 Mon Sep 17 00:00:00 2001 From: Joshua Ferguson Date: Sat, 19 Oct 2024 09:17:28 -0500 Subject: [PATCH] raise InvalidApiKey if authorization fails --- tvdb_v4_official.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/tvdb_v4_official.py b/tvdb_v4_official.py index ab29507..5de2694 100644 --- a/tvdb_v4_official.py +++ b/tvdb_v4_official.py @@ -6,6 +6,10 @@ from urllib.error import HTTPError +class InvalidAPIKey(Exception): + pass + + class Auth: def __init__(self, url, apikey, pin=""): loginInfo = {"apikey": apikey} @@ -20,8 +24,10 @@ def __init__(self, url, apikey, pin=""): res = json.load(response) self.token = res["data"]["token"] except HTTPError as e: - res = json.load(e) - raise Exception("Code:{}, {}".format(e, res["message"])) + if e.code == HTTPStatus.UNAUTHORIZED: + raise InvalidAPIKey("Invalid API key") + else: + raise e def get_token(self): return self.token @@ -201,11 +207,17 @@ def get_series_episodes( lang: str = None, meta=None, if_modified_since=None, - **kwargs + **kwargs, ) -> dict: """Returns a series episodes dictionary""" url = self.url.construct( - "series", id, "episodes/" + season_type, lang, page=page, meta=meta, **kwargs + "series", + id, + "episodes/" + season_type, + lang, + page=page, + meta=meta, + **kwargs, ) return self.request.make_request(url, if_modified_since) @@ -449,5 +461,5 @@ def get_user(self) -> dict: def get_user_favorites(self) -> dict: """Returns a user info dictionary""" - url = self.url.construct('user/favorites') - return self.request.make_request(url) \ No newline at end of file + url = self.url.construct("user/favorites") + return self.request.make_request(url)