From da2c93d65c1209f3bc1bbc8e796be1fb9aefabcf Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sun, 27 Aug 2023 13:16:12 -0400 Subject: [PATCH] Attempt to force refresh twitch auth token if 401 --- backend/handler/igdb_handler.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/backend/handler/igdb_handler.py b/backend/handler/igdb_handler.py index 00f218eb1..4f9fe5b44 100644 --- a/backend/handler/igdb_handler.py +++ b/backend/handler/igdb_handler.py @@ -49,9 +49,23 @@ class IGDBHandler: try: res = requests.post(url, data, headers=self.headers, timeout=timeout) res.raise_for_status() + return res.json() except (HTTPError, Timeout) as err: + if err.response.status_code != 401: + log.error(err) + return [] # All requests to the IGDB API return a list + + # Attempt to force a token refresh if the token is invalid + log.warning("Twitch token invalid: fetching a new one...") + token = self.twitch_auth._update_twitch_token() + self.headers["Authorization"] = f"Bearer {token}" + + try: + res = requests.post(url, data, headers=self.headers, timeout=timeout) + res.raise_for_status() + except (HTTPError, Timeout) as err: + # Log the error and return an empty list if the request fails again log.error(err) - # All requests to the IGDB API return a list return [] return res.json()