Skip to content

Commit 613cba4

Browse files
chillymoshEvieePy
andcommitted
Simplify subscriptions for broadcaster only events
Simplify and improve broadcaster-only EventSub subscriptions - Simplify subscription creation logic - Add default_auth for broadcaster-only subs - Fix callback URL param handling - Remove _subscribe method - Remove redundant webhook sub code Co-authored-by: EvieePy <[email protected]> Co-authored-by: chillymosh <[email protected]>
1 parent e8673e9 commit 613cba4

File tree

4 files changed

+163
-62
lines changed

4 files changed

+163
-62
lines changed

twitchio/client.py

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from typing import TYPE_CHECKING, Any, Literal, Self, Unpack
3232

3333
from .authentication import ManagedHTTPClient, Scopes, UserTokenPayload
34-
from .eventsub.enums import SubscriptionType, TransportMethod
34+
from .eventsub.enums import SubscriptionType
3535
from .eventsub.websockets import Websocket
3636
from .exceptions import HTTPException
3737
from .http import HTTPAsyncIterator
@@ -2107,33 +2107,11 @@ async def update_entitlements(
21072107
data = await self._http.patch_drop_entitlements(ids=ids, fulfillment_status=fulfillment_status, token_for=token_for)
21082108
return [EntitlementStatus(d) for d in data["data"]]
21092109

2110-
async def _subscribe(
2111-
self,
2112-
method: TransportMethod,
2113-
payload: SubscriptionPayload,
2114-
as_bot: bool = False,
2115-
token_for: str | None = None,
2116-
socket_id: str | None = None,
2117-
callback_url: str | None = None,
2118-
eventsub_secret: str | None = None,
2119-
) -> SubscriptionResponse | None:
2120-
if method is TransportMethod.WEBSOCKET:
2121-
return await self.subscribe_websocket(payload=payload, as_bot=as_bot, token_for=token_for, socket_id=socket_id)
2122-
2123-
elif method is TransportMethod.WEBHOOK:
2124-
return await self.subscribe_webhook(
2125-
payload=payload,
2126-
as_bot=as_bot,
2127-
token_for=token_for,
2128-
callback_url=callback_url,
2129-
eventsub_secret=eventsub_secret,
2130-
)
2131-
21322110
async def subscribe_websocket(
21332111
self,
21342112
payload: SubscriptionPayload,
21352113
*,
2136-
as_bot: bool = False,
2114+
as_bot: bool | None = None,
21372115
token_for: str | PartialUser | None = None,
21382116
socket_id: str | None = None,
21392117
) -> SubscriptionResponse | None:
@@ -2179,9 +2157,15 @@ async def subscribe_websocket(
21792157
HTTPException
21802158
An error was raised while making the subscription request to Twitch.
21812159
"""
2160+
defaults = payload.default_auth
2161+
2162+
if as_bot is None:
2163+
as_bot = defaults.get("as_bot", False)
2164+
if token_for is None:
2165+
token_for = defaults.get("token_for", None)
2166+
21822167
if as_bot and not self.bot_id:
21832168
raise ValueError("Client is missing 'bot_id'. Provide a 'bot_id' in the Client constructor.")
2184-
21852169
elif as_bot:
21862170
token_for = self.bot_id
21872171

@@ -2259,8 +2243,6 @@ async def subscribe_webhook(
22592243
self,
22602244
payload: SubscriptionPayload,
22612245
*,
2262-
as_bot: bool = False,
2263-
token_for: str | PartialUser | None = None,
22642246
callback_url: str | None = None,
22652247
eventsub_secret: str | None = None,
22662248
) -> SubscriptionResponse | None:
@@ -2284,19 +2266,6 @@ async def subscribe_webhook(
22842266
----------
22852267
payload: :class:`~twitchio.SubscriptionPayload`
22862268
The payload which should include the required conditions to subscribe to.
2287-
as_bot: bool
2288-
Whether to subscribe to this event using the user token associated with the provided
2289-
:attr:`Client.bot_id`. If this is set to `True` and `bot_id` has not been set, this method will
2290-
raise `ValueError`. Defaults to `False` on :class:`Client` but will default to `True` on
2291-
:class:`~twitchio.ext.commands.Bot`
2292-
token_for: str | PartialUser | None
2293-
An optional User ID, or PartialUser, that will be used to find an appropriate managed user token for this request.
2294-
2295-
If `as_bot` is `True`, this is always the token associated with the
2296-
:attr:`~.bot_id` account. Defaults to `None`.
2297-
2298-
See: :meth:`~.add_token` to add managed tokens to the client.
2299-
If this paramter is not provided or `None`, the default app token is used.
23002269
callback_url: str | None
23012270
An optional url to use as the webhook `callback_url` for this subscription. If you are using one of the built-in
23022271
web adapters, you should not need to set this. See: (web adapter docs link) for more info.
@@ -2315,21 +2284,12 @@ async def subscribe_webhook(
23152284
HTTPException
23162285
An error was raised while making the subscription request to Twitch.
23172286
"""
2318-
if as_bot and not self.bot_id:
2319-
raise ValueError("Client is missing 'bot_id'. Provide a 'bot_id' in the Client constructor.")
2320-
2321-
elif as_bot:
2322-
token_for = self.bot_id
2323-
2324-
if not token_for:
2325-
raise ValueError("A valid User Access Token must be passed to subscribe to eventsub over websocket.")
2326-
23272287
if not self._adapter and not callback_url:
23282288
raise ValueError(
23292289
"Either a 'twitchio.web' Adapter or 'callback_url' should be provided for webhook based eventsub."
23302290
)
23312291

2332-
callback: str | None = self._adapter.eventsub_url or callback_url
2292+
callback: str | None = callback_url or self._adapter.eventsub_url
23332293
if not callback:
23342294
raise ValueError(
23352295
"A callback URL must be provided when subscribing to events via Webhook. "
@@ -2343,9 +2303,6 @@ async def subscribe_webhook(
23432303
if not 10 <= len(secret) <= 100:
23442304
raise ValueError("The 'eventsub_secret' must be between 10 and 100 characters long.")
23452305

2346-
if isinstance(token_for, PartialUser):
2347-
token_for = token_for.id
2348-
23492306
type_ = SubscriptionType(payload.type)
23502307
version: str = payload.version
23512308
transport: SubscriptionCreateTransport = {"method": "webhook", "callback": callback, "secret": secret}
@@ -2355,7 +2312,7 @@ async def subscribe_webhook(
23552312
"version": version,
23562313
"condition": payload.condition,
23572314
"transport": transport,
2358-
"token_for": token_for,
2315+
"token_for": "",
23592316
}
23602317

23612318
try:

0 commit comments

Comments
 (0)