Skip to content

Commit b0f575a

Browse files
committed
data
1 parent 8a42147 commit b0f575a

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

examples/rpc.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import json
44
import asyncio
55
from dotenv import load_dotenv
6-
from livekit.rtc.rpc import RpcHandlerParams
6+
from livekit.rtc.rpc import RpcInvocationData
7+
78
load_dotenv(dotenv_path=".env.local", override=False)
89
LIVEKIT_API_KEY = os.getenv("LIVEKIT_API_KEY")
910
LIVEKIT_API_SECRET = os.getenv("LIVEKIT_API_SECRET")
@@ -90,20 +91,20 @@ async def main():
9091
def register_receiver_methods(greeters_room: rtc.Room, math_genius_room: rtc.Room):
9192
@greeters_room.local_participant.rpc_method("arrival")
9293
async def arrival_method(
93-
params: RpcHandlerParams,
94+
data: RpcInvocationData,
9495
):
95-
print(f'[Greeter] Oh {params.caller_identity} arrived and said "{params.payload}"')
96+
print(f'[Greeter] Oh {data.caller_identity} arrived and said "{data.payload}"')
9697
await asyncio.sleep(2)
9798
return "Welcome and have a wonderful day!"
9899

99100
@math_genius_room.local_participant.rpc_method("square-root")
100101
async def square_root_method(
101-
params: RpcHandlerParams,
102+
data: RpcInvocationData,
102103
):
103-
json_data = json.loads(params.payload)
104+
json_data = json.loads(data.payload)
104105
number = json_data["number"]
105106
print(
106-
f"[Math Genius] I guess {params.caller_identity} wants the square root of {number}. I've only got {params.response_timeout} seconds to respond but I think I can pull it off."
107+
f"[Math Genius] I guess {data.caller_identity} wants the square root of {number}. I've only got {data.response_timeout} seconds to respond but I think I can pull it off."
107108
)
108109

109110
print("[Math Genius] *doing math*…")
@@ -115,25 +116,27 @@ async def square_root_method(
115116

116117
@math_genius_room.local_participant.rpc_method("divide")
117118
async def divide_method(
118-
params: RpcHandlerParams,
119+
data: RpcInvocationData,
119120
):
120-
json_data = json.loads(params.payload)
121+
json_data = json.loads(data.payload)
121122
dividend = json_data["dividend"]
122123
divisor = json_data["divisor"]
123124
print(
124-
f"[Math Genius] {params.caller_identity} wants to divide {dividend} by {divisor}."
125+
f"[Math Genius] {data.caller_identity} wants to divide {dividend} by {divisor}."
125126
)
126127

127128
result = dividend / divisor
128129
return json.dumps({"result": result})
129130

130131
@math_genius_room.local_participant.rpc_method("long-calculation")
131132
async def long_calculation_method(
132-
params: RpcHandlerParams,
133+
data: RpcInvocationData,
133134
):
134-
print(f"[Math Genius] Starting a very long calculation for {params.caller_identity}")
135135
print(
136-
f"[Math Genius] This will take 30 seconds even though you're only giving me {params.response_timeout} seconds"
136+
f"[Math Genius] Starting a very long calculation for {data.caller_identity}"
137+
)
138+
print(
139+
f"[Math Genius] This will take 30 seconds even though you're only giving me {data.response_timeout} seconds"
137140
)
138141
await asyncio.sleep(30)
139142
return json.dumps({"result": "Calculation complete!"})

livekit-rtc/livekit/rtc/participant.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
from .log import logger
4141
import asyncio
4242

43-
from .rpc import RpcHandlerParams
43+
from .rpc import RpcInvocationData
44+
4445

4546
class PublishTrackError(Exception):
4647
def __init__(self, message: str) -> None:
@@ -119,7 +120,7 @@ def __init__(
119120
self._room_queue = room_queue
120121
self._track_publications: dict[str, LocalTrackPublication] = {} # type: ignore
121122
self._rpc_handlers: Dict[
122-
str, Callable[[RpcHandlerParams], Union[Awaitable[str], str]]
123+
str, Callable[[RpcInvocationData], Union[Awaitable[str], str]]
123124
] = {}
124125

125126
@property
@@ -292,7 +293,7 @@ async def perform_rpc(
292293
def register_rpc_method(
293294
self,
294295
method: str,
295-
handler: Callable[[RpcHandlerParams], Union[Awaitable[str], str]],
296+
handler: Callable[[RpcInvocationData], Union[Awaitable[str], str]],
296297
) -> None:
297298
"""
298299
Establishes the participant as a receiver for calls of the specified RPC method.
@@ -309,14 +310,14 @@ def register_rpc_method(
309310
RpcError: On failure. Details in `message`.
310311
311312
Example:
312-
async def greet_handler(params: RpcHandlerParams) -> str:
313-
print(f"Received greeting from {params.caller_identity}: {params.payload}")
314-
return f"Hello, {params.caller_identity}!"
313+
async def greet_handler(data: RpcInvocationData) -> str:
314+
print(f"Received greeting from {data.caller_identity}: {data.payload}")
315+
return f"Hello, {data.caller_identity}!"
315316
316317
await room.local_participant.register_rpc_method('greet', greet_handler)
317318
318319
The handler should return a string or a coroutine that resolves to a string.
319-
320+
320321
If unable to respond within `response_timeout`, the caller will hang up and receive an error on their side.
321322
322323
You may raise errors of type `RpcError` with a string `message` in the handler,
@@ -340,16 +341,16 @@ def rpc_method(self, method: str):
340341
341342
Example:
342343
@local_participant.rpc_method("greet")
343-
async def greet_handler(params: RpcHandlerParams) -> str:
344-
print(f"Received greeting from {params.caller_identity}: {params.payload}")
344+
async def greet_handler(data: RpcInvocationData) -> str:
345+
print(f"Received greeting from {data.caller_identity}: {data.payload}")
345346
return f"Hello, {params.caller_identity}!"
346347
347348
See Also:
348349
`register_rpc_method` for more details
349350
"""
350351

351352
def decorator(
352-
handler: Callable[[RpcHandlerParams], Union[Awaitable[str], str]],
353+
handler: Callable[[RpcInvocationData], Union[Awaitable[str], str]],
353354
):
354355
self.register_rpc_method(method, handler)
355356
return handler
@@ -382,8 +383,10 @@ async def _handle_rpc_method_invocation(
382383
) -> None:
383384
response_error: Optional[RpcError] = None
384385
response_payload: Optional[str] = None
385-
386-
params = RpcHandlerParams(request_id, caller_identity, payload, response_timeout)
386+
387+
params = RpcInvocationData(
388+
request_id, caller_identity, payload, response_timeout
389+
)
387390

388391
handler = self._rpc_handlers.get(method)
389392

@@ -392,7 +395,7 @@ async def _handle_rpc_method_invocation(
392395
else:
393396
try:
394397
if asyncio.iscoroutinefunction(handler):
395-
async_handler = handler # type: Callable[[RpcHandlerParams], Awaitable[str]]
398+
async_handler = handler # type: Callable[[RpcInvocationData], Awaitable[str]]
396399

397400
async def run_handler():
398401
try:
@@ -412,7 +415,7 @@ async def run_handler():
412415
RpcError.ErrorCode.RECIPIENT_DISCONNECTED
413416
)
414417
else:
415-
sync_handler = handler # type: Callable[[RpcHandlerParams], str]
418+
sync_handler = handler # type: Callable[[RpcInvocationData], str]
416419
response_payload = sync_handler(params)
417420
except RpcError as error:
418421
response_error = error

livekit-rtc/livekit/rtc/rpc.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717
from ._proto import rpc_pb2 as proto_rpc
1818
from dataclasses import dataclass
1919

20+
2021
@dataclass
21-
class RpcHandlerParams:
22-
"""Parameters passed to method handler for incoming RPC invocations
23-
22+
class RpcInvocationData:
23+
"""Data passed to method handler for incoming RPC invocations
24+
2425
Attributes:
2526
request_id (str): The unique request ID. Will match at both sides of the call, useful for debugging or logging.
2627
caller_identity (str): The unique participant identity of the caller.
2728
payload (str): The payload of the request. User-definable format, typically JSON.
2829
response_timeout (float): The maximum time the caller will wait for a response.
2930
"""
31+
3032
request_id: str
3133
caller_identity: str
3234
payload: str

0 commit comments

Comments
 (0)