Attempt to force refresh twitch auth token if 401

This commit is contained in:
Georges-Antoine Assi
2023-08-27 13:16:12 -04:00
parent 542f13d279
commit da2c93d65c

View File

@@ -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()