1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
Mocking with twitch-cli
=======================
Twitch CLI is a tool provided by twitch which can be used to mock API calls and EventSub.
To get started, first install and set up ``twitch-cli`` as described here: https://dev.twitch.tv/docs/cli/
Basic setup
-----------
First, run ``twitch mock-api generate`` once and note down the Client ID and secret as well as the ID from the line reading `User ID 53100947 has all applicable units`.
To run the mock server, run ``twitch mock-api start``
Mocking App Authentication and API
----------------------------------
The following code example sets us up with app auth and uses the mock API to get user information:
.. code-block:: python
import asyncio
from twitchAPI.helper import first
from twitchAPI.twitch import Twitch
CLIENT_ID = 'GENERATED_CLIENT_ID'
CLIENT_SECRET = 'GENERATED_CLIENT_SECRET'
USER_ID = '53100947'
async def run():
twitch = await Twitch(CLIENT_ID,
CLIENT_SECRET,
base_url='http://localhost:8080/mock/',
auth_base_url='http://localhost:8080/auth/')
user = await first(twitch.get_users(user_ids=USER_ID))
print(user.login)
await twitch.close()
asyncio.run(run())
Mocking User Authentication
---------------------------
In the following example you see how to set up mocking with a user authentication.
Note that :const:`~twitchAPI.twitch.Twitch.auto_refresh_auth` has to be set to `False` since the mock API does not return a refresh token.
.. code-block:: python
import asyncio
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.helper import first
from twitchAPI.twitch import Twitch
CLIENT_ID = 'GENERATED_CLIENT_ID'
CLIENT_SECRET = 'GENERATED_CLIENT_SECRET'
USER_ID = '53100947'
async def run():
twitch = await Twitch(CLIENT_ID,
CLIENT_SECRET,
base_url='http://localhost:8080/mock/',
auth_base_url='http://localhost:8080/auth/')
twitch.auto_refresh_auth = False
auth = UserAuthenticator(twitch, [], auth_base_url='http://localhost:8080/auth/')
token = await auth.mock_authenticate(USER_ID)
await twitch.set_user_authentication(token, [])
user = await first(twitch.get_users())
print(user.login)
await twitch.close()
asyncio.run(run())
Mocking EventSub Webhook
------------------------
Since the EventSub subscription endpoints are not mocked in twitch-cli, we need to subscribe to events on the live api.
But we can then trigger events from within twitch-cli.
The following example subscribes to the ``channel.subscribe`` event and then prints the command to be used to trigger the event via twitch-cli to console.
.. code-block:: python
import asyncio
from twitchAPI.oauth import UserAuthenticationStorageHelper
from twitchAPI.eventsub.webhook import EventSubWebhook
from twitchAPI.object.eventsub import ChannelSubscribeEvent
from twitchAPI.helper import first
from twitchAPI.twitch import Twitch
from twitchAPI.type import AuthScope
CLIENT_ID = 'REAL_CLIENT_ID'
CLIENT_SECRET = 'REAL_CLIENT_SECRET'
EVENTSUB_URL = 'https://my.eventsub.url'
async def on_subscribe(data: ChannelSubscribeEvent):
print(f'{data.event.user_name} just subscribed!')
async def run():
twitch = await Twitch(CLIENT_ID,
CLIENT_SECRET)
auth = UserAuthenticationStorageHelper(twitch, [AuthScope.CHANNEL_READ_SUBSCRIPTIONS])
await auth.bind()
user = await first(twitch.get_users())
eventsub = EventSubWebhook(EVENTSUB_URL, 8080, twitch)
eventsub.start()
sub_id = await eventsub.listen_channel_subscribe(user.id, on_subscribe)
print(f'twitch event trigger channel.subscribe -F {EVENTSUB_URL}/callback -t {user.id} -u {sub_id} -s {eventsub.secret}')
try:
input('press ENTER to stop')
finally:
await eventsub.stop()
await twitch.close()
asyncio.run(run())
Mocking EventSub Websocket
--------------------------
For EventSub Websocket to work, you first have to run the following command to start a websocket server in addition to the API server: ``twitch event websocket start``
We once again mock both the app and user auth.
The following example subscribes to the ``channel.subscribe`` event and then prints the command to be used to trigger the event via twitch-cli to console.
.. code-block:: python
import asyncio
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.eventsub.websocket import EventSubWebsocket
from twitchAPI.object.eventsub import ChannelSubscribeEvent
from twitchAPI.helper import first
from twitchAPI.twitch import Twitch
from twitchAPI.type import AuthScope
CLIENT_ID = 'GENERATED_CLIENT_ID'
CLIENT_SECRET = 'GENERATED_CLIENT_SECRET'
USER_ID = '53100947'
async def on_subscribe(data: ChannelSubscribeEvent):
print(f'{data.event.user_name} just subscribed!')
async def run():
twitch = await Twitch(CLIENT_ID,
CLIENT_SECRET,
base_url='http://localhost:8080/mock/',
auth_base_url='http://localhost:8080/auth/')
twitch.auto_refresh_auth = False
auth = UserAuthenticator(twitch, [AuthScope.CHANNEL_READ_SUBSCRIPTIONS], auth_base_url='http://localhost:8080/auth/')
token = await auth.mock_authenticate(USER_ID)
await twitch.set_user_authentication(token, [AuthScope.CHANNEL_READ_SUBSCRIPTIONS])
user = await first(twitch.get_users())
eventsub = EventSubWebsocket(twitch,
connection_url='ws://127.0.0.1:8080/ws',
subscription_url='http://127.0.0.1:8080/')
eventsub.start()
sub_id = await eventsub.listen_channel_subscribe(user.id, on_subscribe)
print(f'twitch event trigger channel.subscribe -t {user.id} -u {sub_id} -T websocket')
try:
input('press ENTER to stop\n')
finally:
await eventsub.stop()
await twitch.close()
asyncio.run(run())
|