Skip to content

Commit 0493b7f

Browse files
committed
Add keyword-arguments for loading and dumping tokens to Client.
1 parent 3584061 commit 0493b7f

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

twitchio/client.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def __init__(
147147

148148
self._login_called: bool = False
149149
self._has_closed: bool = False
150+
self._dump_tokens: bool = True
150151

151152
# Websockets for EventSub
152153
self._websockets: dict[str, dict[str, Websocket]] = defaultdict(dict)
@@ -256,7 +257,7 @@ async def setup_hook(self) -> None:
256257
"""
257258
...
258259

259-
async def login(self, *, token: str | None = None) -> None:
260+
async def login(self, *, token: str | None = None, load_tokens: bool = True, dump_tokens: bool = True) -> None:
260261
"""Method to login the client and generate or store an app token.
261262
262263
This method is called automatically when using :meth:`~.start`.
@@ -273,11 +274,18 @@ async def login(self, *, token: str | None = None) -> None:
273274
----------
274275
token: str | None
275276
An optional app token to use instead of generating one automatically.
277+
load_tokens: bool
278+
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
279+
login automatically. Defaults to ``True``.
280+
dump_tokens: bool
281+
Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens` during the
282+
:meth:`.close` automatically. Defaults to ``True``.
276283
"""
277284
if self._login_called:
278285
return
279286

280287
self._login_called = True
288+
self._dump_tokens = dump_tokens
281289

282290
if not token:
283291
payload: ClientCredentialsPayload = await self._http.client_credentials_token()
@@ -288,8 +296,9 @@ async def login(self, *, token: str | None = None) -> None:
288296

289297
self._http._app_token = token
290298

291-
async with self._http._token_lock:
292-
await self.load_tokens()
299+
if load_tokens:
300+
async with self._http._token_lock:
301+
await self.load_tokens()
293302

294303
await self.setup_hook()
295304

@@ -299,7 +308,14 @@ async def __aenter__(self) -> Self:
299308
async def __aexit__(self, *_: Any) -> None:
300309
await self.close()
301310

302-
async def start(self, token: str | None = None, *, with_adapter: bool = True) -> None:
311+
async def start(
312+
self,
313+
token: str | None = None,
314+
*,
315+
with_adapter: bool = True,
316+
load_tokens: bool = True,
317+
dump_tokens: bool = True,
318+
) -> None:
303319
"""|coro|
304320
305321
Method to login and run the `Client` asynchronously on an already running event loop.
@@ -317,7 +333,12 @@ async def start(self, token: str | None = None, *, with_adapter: bool = True) ->
317333
An optional app token to use instead of generating one automatically.
318334
with_adapter: bool
319335
Whether to start and run a web adapter. Defaults to `True`. See: ... for more information.
320-
336+
load_tokens: bool
337+
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
338+
:meth:`.login` automatically. Defaults to ``True``.
339+
dump_tokens: bool
340+
Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens` during the
341+
:meth:`.close` automatically. Defaults to ``True``.
321342
322343
Examples
323344
--------
@@ -335,7 +356,7 @@ async def main() -> None:
335356
await client.start()
336357
"""
337358
self.__waiter.clear()
338-
await self.login(token=token)
359+
await self.login(token=token, load_tokens=load_tokens, dump_tokens=dump_tokens)
339360

340361
if with_adapter:
341362
await self._adapter.run()
@@ -350,7 +371,14 @@ async def main() -> None:
350371
self._ready_event.clear()
351372
await self.close()
352373

353-
def run(self, token: str | None = None, *, with_adapter: bool = True) -> None:
374+
def run(
375+
self,
376+
token: str | None = None,
377+
*,
378+
with_adapter: bool = True,
379+
load_tokens: bool = True,
380+
dump_tokens: bool = True,
381+
) -> None:
354382
"""Method to login the client and create a continuously running event loop.
355383
356384
The behaviour of this method is similar to :meth:`~.start` but instead of being used in an already running
@@ -374,7 +402,12 @@ def run(self, token: str | None = None, *, with_adapter: bool = True) -> None:
374402
An optional app token to use instead of generating one automatically.
375403
with_adapter: bool
376404
Whether to start and run a web adapter. Defaults to `True`. See: ... for more information.
377-
405+
load_tokens: bool
406+
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
407+
:meth:`.login` automatically. Defaults to ``True``.
408+
dump_tokens: bool
409+
Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens` during the
410+
:meth:`.close` automatically. Defaults to ``True``.
378411
379412
Examples
380413
--------
@@ -387,7 +420,7 @@ def run(self, token: str | None = None, *, with_adapter: bool = True) -> None:
387420

388421
async def run() -> None:
389422
async with self:
390-
await self.start(token=token, with_adapter=with_adapter)
423+
await self.start(token=token, with_adapter=with_adapter, load_tokens=load_tokens, dump_tokens=dump_tokens)
391424

392425
try:
393426
asyncio.run(run())
@@ -429,8 +462,9 @@ async def close(self) -> None:
429462
for socket in sockets:
430463
await socket.close()
431464

432-
async with self._http._token_lock:
433-
await self.dump_tokens()
465+
if self._dump_tokens:
466+
async with self._http._token_lock:
467+
await self.dump_tokens()
434468

435469
self._http.cleanup()
436470
self.__waiter.set()
@@ -586,8 +620,9 @@ async def load_tokens(self, path: str | None = None, /) -> None:
586620
587621
.. note::
588622
589-
This method is always called by the client during :meth:`~.login` but **before**
590-
:meth:`~.setup_hook`.
623+
This method is called by the client during :meth:`~.login` but **before**
624+
:meth:`~.setup_hook` when the ``load_tokens`` keyword-argument
625+
is ``True`` in either, :meth:`.run`, :meth:`.start` or :meth:`.login` (Default).
591626
592627
You can override this method to implement your own token loading logic into the client, such as from a database.
593628
@@ -627,7 +662,8 @@ async def dump_tokens(self, path: str | None = None, /) -> None:
627662
628663
.. note::
629664
630-
This method is always called by the client when it is gracefully closed.
665+
This method is called by the client when it is gracefully closed and the ``dump_tokens`` keyword-argument
666+
is ``True`` in either, :meth:`.run`, :meth:`.start` or :meth:`.login` (Default).
631667
632668
.. note::
633669

0 commit comments

Comments
 (0)