Skip to content

Commit

Permalink
fix webhook & add examples (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom authored Nov 14, 2023
1 parent 8b48eb5 commit 0c461f3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
18 changes: 18 additions & 0 deletions examples/api.py
Original file line number Diff line number Diff line change
@@ -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())
23 changes: 23 additions & 0 deletions examples/webhook.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 7 additions & 8 deletions livekit-api/livekit/api/access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 0c461f3

Please sign in to comment.