Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix token regeneration hooks not applying to the active request #440

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
:orphan:

2.9.0
=======
- TwitchIO
- Additions
- Added :class:`~twitchio.AdSchedule`
- Added the new ad-related methods for :class:`~twitchio.PartialUser`:
- :func:`~twitchio.PartialUser.fetch_ad_schedule`
- :func:`~twitchio.PartialUser.snooze_ad`
- Added :func:`~twitchio.PartialUser.fetch_moderated_channels` to :class:`~twitchio.PartialUser`

- Bug fixes
- Fixed ``event_token_expired`` not applying to the current request.

- ext.eventsub
- Bug fixes
- Fixed a crash where a Future could be None, causing unintentional errors.


2.8.2
======
- ext.commands
Expand Down
42 changes: 24 additions & 18 deletions twitchio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,8 @@ async def request(self, route: Route, *, paginate=True, limit=100, full_body=Fal
raise errors.NoClientID("A Client ID is required to use the Twitch API")
headers = route.headers or {}

if force_app_token and "Authorization" not in headers:
if not self.client_secret:
raise errors.NoToken(
"An app access token is required for this route, please provide a client id and client secret"
)
if self.app_token is None:
await self._generate_login()
headers["Authorization"] = f"Bearer {self.app_token}"
elif not self.token and not self.client_secret and "Authorization" not in headers:
raise errors.NoToken(
"Authorization is required to use the Twitch API. Pass token and/or client_secret to the Client constructor"
)
if "Authorization" not in headers:
if not self.token:
await self._generate_login()
headers["Authorization"] = f"Bearer {self.token}"
await self._apply_auth(headers, force_app_token, False)

headers["Client-ID"] = self.client_id

if not self.session:
Expand Down Expand Up @@ -165,7 +151,7 @@ def get_limit():
q = [("after", cursor), *q]
q = [("first", get_limit()), *q]
path = path.with_query(q)
body, is_text = await self._request(route, path, headers)
body, is_text = await self._request(route, path, headers, force_app_token=force_app_token)
if is_text:
return body
if full_body:
Expand All @@ -182,7 +168,26 @@ def get_limit():
is_finished = reached_limit() if limit is not None else True if paginate else True
return data

async def _request(self, route, path, headers, utilize_bucket=True):
async def _apply_auth(self, headers: dict, force_app_token: bool, force_apply: bool) -> None:
if force_app_token and "Authorization" not in headers:
if not self.client_secret:
raise errors.NoToken(
"An app access token is required for this route, please provide a client id and client secret"
)
if self.app_token is None:
await self._generate_login()
headers["Authorization"] = f"Bearer {self.app_token}"
elif not self.token and not self.client_secret and "Authorization" not in headers:
raise errors.NoToken(
"Authorization is required to use the Twitch API. Pass token and/or client_secret to the Client constructor"
)
if "Authorization" not in headers or force_apply:
if not self.token:
await self._generate_login()

headers["Authorization"] = f"Bearer {self.token}"

async def _request(self, route, path, headers, utilize_bucket=True, force_app_token: bool = False):
reason = None

for attempt in range(5):
Expand Down Expand Up @@ -224,6 +229,7 @@ async def _request(self, route, path, headers, utilize_bucket=True):
if "Invalid OAuth token" in message_json.get("message", ""):
try:
await self._generate_login()
await self._apply_auth(headers, force_app_token, True)
continue
except:
raise errors.Unauthorized(
Expand Down
Loading