Skip to content

Commit

Permalink
Cleanup client startup
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Mar 16, 2024
1 parent eb34097 commit b061d24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 1 addition & 2 deletions twitchio/authentication/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def __init__(
*,
client_id: str,
client_secret: str,
app_token: str | None = None,
redirect_uri: str | None = None,
scopes: Scopes | None = None,
session: aiohttp.ClientSession | None = None,
Expand All @@ -72,7 +71,7 @@ def __init__(
)

self._tokens: TokenMapping = {}
self._app_token = app_token
self._app_token: str | None = None
self._validate_task: asyncio.Task[None] | None = None

async def _attempt_refresh_on_add(self, token: str, refresh: str) -> ValidateTokenPayload:
Expand Down
25 changes: 15 additions & 10 deletions twitchio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

from .types_.options import ClientOptions

from .authentication import ClientCredentialsPayload


class Client:
def __init__(
Expand All @@ -49,12 +51,10 @@ def __init__(
redirect_uri: str | None = options.get("redirect_uri", None)
scopes: Scopes | None = options.get("scopes", None)
session: aiohttp.ClientSession | None = options.get("session", None)
app_token: str | None = options.get("app_token", None)

self._http = ManagedHTTPClient(
client_id=client_id,
client_secret=client_secret,
app_token=app_token,
redirect_uri=redirect_uri,
scopes=scopes,
session=session,
Expand All @@ -68,24 +68,29 @@ def __init__(

async def setup_hook(self) -> None: ...

async def __aenter__(self) -> Self:
if not self._http._app_token:
payload = await self._http.client_credentials_token()
self._http._app_token = payload.access_token
async def login(self, *, token: str | None = None) -> None:
if not token:
payload: ClientCredentialsPayload = await self._http.client_credentials_token()
token = payload.access_token

self._http._app_token = token
await self.setup_hook()

async def __aenter__(self) -> Self:
return self

async def __aexit__(self, *_: Any) -> None:
await self.close()

async def start(self, with_adapter: bool = True) -> None:
# TODO: Temp logic for testing...
await self.setup_hook()
async def start(self, token: str | None = None, *, with_adapter: bool = True) -> None:
await self.login(token=token)

if with_adapter:
await self._adapter.run()

async def block(self) -> None:
await self._block()

async def _block(self) -> None:
# TODO: Temp METHOD for testing...

try:
Expand Down
1 change: 0 additions & 1 deletion twitchio/types_/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ class ClientOptions(TypedDict, total=False):
redirect_uri: str | None
scopes: Scopes | None
session: aiohttp.ClientSession | None
app_token: str | None
adapter: type[StarletteAdapter | AiohttpAdapter] | None

0 comments on commit b061d24

Please sign in to comment.