2525 from django .shortcuts import SupportsGetAbsoluteUrl
2626
2727 from modernrpc .handler import RpcHandler
28- from modernrpc .types import AuthPredicateType
29-
28+ from modernrpc .types import AuthPredicateType , FuncOrCoro
3029
3130logger = logging .getLogger (__name__ )
3231
3332RpcErrorHandler = Callable [[BaseException , RpcRequestContext ], None ]
3433
3534
3635class RegistryMixin :
36+ """
37+ Common logic for both RpcNamespace and RpcServer classes.
38+ Provide methods to register RPC procedures into an internal registry.
39+ """
40+
3741 def __init__ (self , auth : AuthPredicateType = NOT_SET ) -> None :
3842 self ._registry : dict [str , ProcedureWrapper ] = {}
3943 self .auth = auth
4044
4145 def register_procedure (
4246 self ,
43- procedure : Callable | None = None ,
47+ procedure : FuncOrCoro | None = None ,
4448 name : str | None = None ,
4549 protocol : Protocol = Protocol .ALL ,
4650 auth : AuthPredicateType = NOT_SET ,
@@ -61,7 +65,7 @@ def register_procedure(
6165 :raises ValueError: If a procedure can't be registered
6266 """
6367
64- def decorated (func : Callable ) -> Callable :
68+ def decorated (func : FuncOrCoro ) -> FuncOrCoro :
6569 if name and name .startswith ("rpc." ):
6670 raise ValueError (
6771 'According to JSON-RPC specs, method names starting with "rpc." are reserved for system extensions '
@@ -92,10 +96,13 @@ def procedures(self) -> dict[str, ProcedureWrapper]:
9296 return self ._registry
9397
9498
95- class RpcNamespace (RegistryMixin ): ...
99+ class RpcNamespace (RegistryMixin ):
100+ """Registry for RPC procedures belonging to a given namespace."""
96101
97102
98103class RpcServer (RegistryMixin ):
104+ """Base class to store all remote procedures for a given entry point"""
105+
99106 def __init__ (
100107 self ,
101108 register_system_procedures : bool = True ,
@@ -122,6 +129,7 @@ def __init__(
122129 self .default_encoding = default_encoding
123130
124131 def register_namespace (self , namespace : RpcNamespace , name : str | None = None ) -> None :
132+ """Register all procedures from given namespace into the top-level server."""
125133 if name :
126134 prefix = name + "."
127135 logger .debug (
@@ -157,6 +165,7 @@ def get_procedure_wrapper(self, name: str, protocol: Protocol) -> ProcedureWrapp
157165 raise RPCMethodNotFound (name ) from None
158166
159167 def get_request_handler (self , request : HttpRequest ) -> RpcHandler | None :
168+ """Return the first handler that can handle the given request, or None if no handler can handle it."""
160169 handler_klass = first_true (self .handler_klasses , pred = lambda cls : cls .can_handle (request ))
161170 try :
162171 return handler_klass ()
@@ -217,6 +226,7 @@ def check_request(self, request: HttpRequest) -> HttpResponse | None:
217226
218227 @staticmethod
219228 def build_response (handler : RpcHandler , result_data : str | tuple [int , str ]) -> HttpResponse :
229+ """Build an HttpResponse instance from the given handler and result data."""
220230 if isinstance (result_data , tuple ) and len (result_data ) == 2 :
221231 status , result_data = result_data
222232 else :
0 commit comments