4040from .log import logger
4141import asyncio
4242
43+ from .rpc import RpcHandlerParams
4344
4445class PublishTrackError (Exception ):
4546 def __init__ (self , message : str ) -> None :
@@ -118,7 +119,7 @@ def __init__(
118119 self ._room_queue = room_queue
119120 self ._track_publications : dict [str , LocalTrackPublication ] = {} # type: ignore
120121 self ._rpc_handlers : Dict [
121- str , Callable [[str , str , str , float ], Union [Awaitable [str ], str ]]
122+ str , Callable [[RpcHandlerParams ], Union [Awaitable [str ], str ]]
122123 ] = {}
123124
124125 @property
@@ -291,7 +292,7 @@ async def perform_rpc(
291292 def register_rpc_method (
292293 self ,
293294 method : str ,
294- handler : Callable [[str , str , str , float ], Union [Awaitable [str ], str ]],
295+ handler : Callable [[RpcHandlerParams ], Union [Awaitable [str ], str ]],
295296 ) -> None :
296297 """
297298 Establishes the participant as a receiver for calls of the specified RPC method.
@@ -308,20 +309,15 @@ def register_rpc_method(
308309 RpcError: On failure. Details in `message`.
309310
310311 Example:
311- async def greet_handler(request_id: str, caller_identity: str, payload: str, response_timeout: float ) -> str:
312- print(f"Received greeting from {caller_identity}: {payload}")
313- return f"Hello, {caller_identity}!"
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}!"
314315
315316 await room.local_participant.register_rpc_method('greet', greet_handler)
316317
317- The handler receives the following parameters:
318- - `request_id`: A unique identifier for this RPC request
319- - `caller_identity`: The identity of the RemoteParticipant who initiated the RPC call
320- - `payload`: The data sent by the caller (as a string)
321- - `response_timeout`: The maximum time available to return a response
322-
323318 The handler should return a string or a coroutine that resolves to a string.
324- If unable to respond within `response_timeout`, the request will result in an error on the caller's side.
319+
320+ If unable to respond within `response_timeout`, the caller will hang up and receive an error on their side.
325321
326322 You may raise errors of type `RpcError` with a string `message` in the handler,
327323 and they will be received on the caller's side with the message intact.
@@ -344,16 +340,16 @@ def rpc_method(self, method: str):
344340
345341 Example:
346342 @local_participant.rpc_method("greet")
347- async def greet_handler(request_id: str, caller_identity: str, payload: str, response_timeout: float ) -> str:
348- print(f"Received greeting from {caller_identity}: {payload}")
349- return f"Hello, {caller_identity}!"
343+ async def greet_handler(params: RpcHandlerParams ) -> str:
344+ print(f"Received greeting from {params. caller_identity}: {params. payload}")
345+ return f"Hello, {params. caller_identity}!"
350346
351347 See Also:
352348 `register_rpc_method` for more details
353349 """
354350
355351 def decorator (
356- handler : Callable [[str , str , str , float ], Union [Awaitable [str ], str ]],
352+ handler : Callable [[RpcHandlerParams ], Union [Awaitable [str ], str ]],
357353 ):
358354 self .register_rpc_method (method , handler )
359355 return handler
@@ -386,6 +382,8 @@ async def _handle_rpc_method_invocation(
386382 ) -> None :
387383 response_error : Optional [RpcError ] = None
388384 response_payload : Optional [str ] = None
385+
386+ params = RpcHandlerParams (request_id , caller_identity , payload , response_timeout )
389387
390388 handler = self ._rpc_handlers .get (method )
391389
@@ -394,13 +392,11 @@ async def _handle_rpc_method_invocation(
394392 else :
395393 try :
396394 if asyncio .iscoroutinefunction (handler ):
397- async_handler = handler # type: Callable[[str, str, str, float ], Awaitable[str]]
395+ async_handler = handler # type: Callable[[RpcHandlerParams ], Awaitable[str]]
398396
399397 async def run_handler ():
400398 try :
401- return await async_handler (
402- request_id , caller_identity , payload , response_timeout
403- )
399+ return await async_handler (params )
404400 except asyncio .CancelledError :
405401 # This will be caught by the outer try-except if it's due to timeout
406402 raise
@@ -416,10 +412,8 @@ async def run_handler():
416412 RpcError .ErrorCode .RECIPIENT_DISCONNECTED
417413 )
418414 else :
419- sync_handler = handler # type: Callable[[str, str, str, float], str]
420- response_payload = sync_handler (
421- request_id , caller_identity , payload , response_timeout
422- )
415+ sync_handler = handler # type: Callable[[RpcHandlerParams], str]
416+ response_payload = sync_handler (params )
423417 except RpcError as error :
424418 response_error = error
425419 except Exception as error :
0 commit comments