Skip to content

Commit 0c461f3

Browse files
authored
fix webhook & add examples (#100)
1 parent 8b48eb5 commit 0c461f3

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

examples/api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from livekit import api
2+
import asyncio
3+
4+
5+
async def main():
6+
# will automatically use the LIVEKIT_API_KEY and LIVEKIT_API_SECRET env vars
7+
lkapi = api.LiveKitAPI("http://localhost:7880")
8+
room_info = await lkapi.room.create_room(
9+
api.CreateRoomRequest(name="my-room"),
10+
)
11+
print(room_info)
12+
room_list = await lkapi.room.list_rooms(api.ListRoomsRequest())
13+
print(room_list)
14+
await lkapi.aclose()
15+
16+
17+
if __name__ == "__main__":
18+
asyncio.get_event_loop().run_until_complete(main())

examples/webhook.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from livekit import api
2+
from aiohttp import web
3+
4+
5+
async def handle_webhook(request):
6+
token_verifier = api.TokenVerifier()
7+
webhook_receiver = api.WebhookReceiver(token_verifier)
8+
9+
auth_token = request.headers.get("Authorization")
10+
if not auth_token:
11+
return web.Response(status=401)
12+
13+
body = await request.read()
14+
event = webhook_receiver.receive(body.decode("utf-8"), auth_token)
15+
print("received event:", event)
16+
17+
return web.Response(status=200)
18+
19+
20+
if __name__ == "__main__":
21+
app = web.Application()
22+
app.router.add_post("/", handle_webhook)
23+
web.run_app(app, port=3000)

livekit-api/livekit/api/access_token.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,20 @@ def verify(self, token: str) -> Claims:
155155
leeway=self._leeway.total_seconds(),
156156
)
157157

158-
video_dict = {camel_to_snake(k): v for k, v in claims["video"].items()}
158+
video_dict = claims.get("video", dict())
159+
video_dict = {camel_to_snake(k): v for k, v in video_dict.items()}
159160
video_dict = {
160161
k: v for k, v in video_dict.items() if k in VideoGrants.__dataclass_fields__
161162
}
162163
video = VideoGrants(**video_dict)
163164

164-
c = Claims(
165-
identity=claims["sub"],
166-
name=claims["name"],
165+
return Claims(
166+
identity=claims.get("sub", ""),
167+
name=claims.get("name", ""),
167168
video=video,
168-
metadata=claims["metadata"],
169-
sha256=claims["sha256"],
169+
metadata=claims.get("metadata", ""),
170+
sha256=claims.get("sha256", ""),
170171
)
171-
c.identity = claims["sub"]
172-
return c
173172

174173

175174
def camel_to_snake(t: str):

0 commit comments

Comments
 (0)