From 0c461f37b33873276b1580b9ad525f7f2e309733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Monnom?= Date: Mon, 13 Nov 2023 23:18:22 -0800 Subject: [PATCH] fix webhook & add examples (#100) --- examples/api.py | 18 ++++++++++++++++++ examples/webhook.py | 23 +++++++++++++++++++++++ livekit-api/livekit/api/access_token.py | 15 +++++++-------- 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 examples/api.py create mode 100644 examples/webhook.py diff --git a/examples/api.py b/examples/api.py new file mode 100644 index 00000000..bdb6348a --- /dev/null +++ b/examples/api.py @@ -0,0 +1,18 @@ +from livekit import api +import asyncio + + +async def main(): + # will automatically use the LIVEKIT_API_KEY and LIVEKIT_API_SECRET env vars + lkapi = api.LiveKitAPI("http://localhost:7880") + room_info = await lkapi.room.create_room( + api.CreateRoomRequest(name="my-room"), + ) + print(room_info) + room_list = await lkapi.room.list_rooms(api.ListRoomsRequest()) + print(room_list) + await lkapi.aclose() + + +if __name__ == "__main__": + asyncio.get_event_loop().run_until_complete(main()) diff --git a/examples/webhook.py b/examples/webhook.py new file mode 100644 index 00000000..5e756b9b --- /dev/null +++ b/examples/webhook.py @@ -0,0 +1,23 @@ +from livekit import api +from aiohttp import web + + +async def handle_webhook(request): + token_verifier = api.TokenVerifier() + webhook_receiver = api.WebhookReceiver(token_verifier) + + auth_token = request.headers.get("Authorization") + if not auth_token: + return web.Response(status=401) + + body = await request.read() + event = webhook_receiver.receive(body.decode("utf-8"), auth_token) + print("received event:", event) + + return web.Response(status=200) + + +if __name__ == "__main__": + app = web.Application() + app.router.add_post("/", handle_webhook) + web.run_app(app, port=3000) diff --git a/livekit-api/livekit/api/access_token.py b/livekit-api/livekit/api/access_token.py index e689df46..6ccc9a5c 100644 --- a/livekit-api/livekit/api/access_token.py +++ b/livekit-api/livekit/api/access_token.py @@ -155,21 +155,20 @@ def verify(self, token: str) -> Claims: leeway=self._leeway.total_seconds(), ) - video_dict = {camel_to_snake(k): v for k, v in claims["video"].items()} + video_dict = claims.get("video", dict()) + video_dict = {camel_to_snake(k): v for k, v in video_dict.items()} video_dict = { k: v for k, v in video_dict.items() if k in VideoGrants.__dataclass_fields__ } video = VideoGrants(**video_dict) - c = Claims( - identity=claims["sub"], - name=claims["name"], + return Claims( + identity=claims.get("sub", ""), + name=claims.get("name", ""), video=video, - metadata=claims["metadata"], - sha256=claims["sha256"], + metadata=claims.get("metadata", ""), + sha256=claims.get("sha256", ""), ) - c.identity = claims["sub"] - return c def camel_to_snake(t: str):