Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix webhook & add examples #100

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default the value here

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", ""),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

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
Loading