Skip to content

Commit fb68aa7

Browse files
committed
Add client kwarg to adapters.
1 parent 39ebdd6 commit fb68aa7

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

twitchio/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ def __init__(
159159
else:
160160
self._adapter = adapter()
161161

162-
self._adapter.client = self
162+
if not hasattr(self._adapter, "client"):
163+
self._adapter.client = self
163164

164165
# Own Client User. Set in login...
165166
self._fetch_self: bool = options.get("fetch_client_user", True)
@@ -206,7 +207,8 @@ async def set_adapter(self, adapter: BaseAdapter[Any]) -> None:
206207
await self._adapter.close(False)
207208

208209
self._adapter = adapter
209-
self._adapter.client = self
210+
if not hasattr(self._adapter, "client"):
211+
self._adapter.client = self
210212

211213
if self._setup_called and not self._adapter._running:
212214
await self._adapter.run()

twitchio/web/aio_adapter.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class AiohttpAdapter(BaseAdapter[BT], web.Application):
114114
E.g. ``http://localhost:4343/oauth/callback`` or ``https://mydomain.org/oauth/callback``.
115115
ssl_context: SSLContext | None
116116
An optional :class:`SSLContext` passed to the adapter. If SSL is setup via a front-facing web server such as NGINX, you should leave this as None.
117+
client: :class:`~twitchio.Client` | None
118+
An optional :class:`~twitchio.Client` or any derivative such as :class:`~twitchio.ext.commands.Bot` to set for this
119+
adapter. When ``None`` the client will be set automatically after initalization.
117120
118121
Examples
119122
--------
@@ -155,8 +158,13 @@ def __init__(
155158
oauth_path: str | None = None,
156159
redirect_path: str | None = None,
157160
ssl_context: SSLContext | None = None,
161+
client: BT | None = None,
158162
) -> None:
159163
super().__init__()
164+
165+
if client:
166+
self.client = client
167+
160168
self._runner: web.AppRunner | None = None
161169

162170
self._host: str = host or "localhost"

twitchio/web/starlette_adapter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class StarletteAdapter(BaseAdapter[BT], Starlette):
126126
timeout_graceful_shutdown: int
127127
An optional :class:`int` which is the maximum amount of time in seconds ``Uvicorn`` should wait before forcefully
128128
closing. Defaults to ``3``.
129+
client: :class:`~twitchio.Client` | None
130+
An optional :class:`~twitchio.Client` or any derivative such as :class:`~twitchio.ext.commands.Bot` to set for this
131+
adapter. When ``None`` the client will be set automatically after initalization.
129132
130133
Examples
131134
--------
@@ -171,7 +174,11 @@ def __init__(
171174
ssl_certfile: str | PathLike[str] | None = None,
172175
timeout_keep_alive: int = 5,
173176
timeout_graceful_shutdown: int = 3,
177+
client: BT | None = None,
174178
) -> None:
179+
if client:
180+
self.client = client
181+
175182
self._timeout_keep_alive = timeout_keep_alive
176183
self._timeout_graceful_shutdown = timeout_graceful_shutdown
177184
self._host: str = host or "localhost"

0 commit comments

Comments
 (0)