From 76a34685da4c5c6ff8b91d27dff73588b349f36d Mon Sep 17 00:00:00 2001 From: DevilXD Date: Sat, 10 Sep 2022 15:51:27 +0200 Subject: [PATCH] Redo some of the typing --- twitch.py | 21 ++++++++++++--------- websocket.py | 6 +++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/twitch.py b/twitch.py index 50e3c05..8e21282 100644 --- a/twitch.py +++ b/twitch.py @@ -807,10 +807,10 @@ class Twitch: login_form.clear() return self._access_token - async def check_login(self) -> None: + async def check_login(self) -> tuple[str, int]: if self._access_token is not None and self._user_id is not None: # we're all good - return + return (self._access_token, self._user_id) # looks like we're missing something login_form: LoginForm = self.gui.login logger.debug("Checking login") @@ -824,14 +824,16 @@ class Twitch: cookie = jar.filter_cookies(url) if not cookie: # no cookie - login - await self._login() - # store our auth token inside the cookie - cookie["auth-token"] = cast(str, self._access_token) + access_token: str = await self._login() + # store the auth token inside the cookie + cookie["auth-token"] = access_token elif self._access_token is None: # have cookie - get our access token - self._access_token = cookie["auth-token"].value + access_token = cookie["auth-token"].value logger.debug("Restoring session from cookie") - # validate our access token, by obtaining user_id + # store the auth token within the application + self._access_token = access_token + # validate the auth token, by obtaining user_id async with self.request( "GET", "https://id.twitch.tv/oauth2/validate", @@ -856,6 +858,7 @@ class Twitch: # update our cookie and save it jar.update_cookies(cookie, url) jar.save(COOKIES_PATH) + return (self._access_token, self._user_id) @asynccontextmanager async def request( @@ -901,13 +904,13 @@ class Twitch: async def gql_request(self, op: GQLOperation) -> JsonType: gql_logger.debug(f"GQL Request: {op}") - await self.check_login() + access_token, user_id = await self.check_login() async with self.request( "POST", "https://gql.twitch.tv/gql", json=op, headers={ - "Authorization": f"OAuth {self._access_token}", + "Authorization": f"OAuth {access_token}", "Client-Id": CLIENT_ID, } ) as response: diff --git a/websocket.py b/websocket.py index d72ee46..c098fe5 100644 --- a/websocket.py +++ b/websocket.py @@ -200,7 +200,7 @@ class Websocket: # nothing to do return self._topics_changed.clear() - await self._twitch.check_login() + access_token, user_id = await self._twitch.check_login() current: set[WebsocketTopic] = set(self.topics.values()) # handle removed topics removed = self._submitted.difference(current) @@ -212,7 +212,7 @@ class Websocket: "type": "UNLISTEN", "data": { "topics": topics_list, - "auth_token": self._twitch._access_token, + "auth_token": access_token, } } ) @@ -227,7 +227,7 @@ class Websocket: "type": "LISTEN", "data": { "topics": topics_list, - "auth_token": self._twitch._access_token, + "auth_token": access_token, } } )