websockets: simplify JSON-RPC execution
To accommodate access to multiple protocols Moonraker will always require that the "params" field contain a dictionary, so reject any other type as invalid. There is no need to expand keyword arguments, simply pass the params dict to the callback. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
da864ab391
commit
4666458793
|
@ -599,18 +599,16 @@ class MQTTClient(APITransport, Subscribable):
|
||||||
request_method: str,
|
request_method: str,
|
||||||
callback: Callable[[WebRequest], Coroutine]
|
callback: Callable[[WebRequest], Coroutine]
|
||||||
) -> RPCCallback:
|
) -> RPCCallback:
|
||||||
async def func(**kwargs) -> Any:
|
async def func(args: Dict[str, Any]) -> Any:
|
||||||
self._check_timestamp(kwargs)
|
self._check_timestamp(args)
|
||||||
result = await callback(
|
result = await callback(WebRequest(endpoint, args, request_method))
|
||||||
WebRequest(endpoint, kwargs, request_method))
|
|
||||||
return result
|
return result
|
||||||
return func
|
return func
|
||||||
|
|
||||||
def _generate_remote_callback(self, endpoint: str) -> RPCCallback:
|
def _generate_remote_callback(self, endpoint: str) -> RPCCallback:
|
||||||
async def func(**kwargs) -> Any:
|
async def func(args: Dict[str, Any]) -> Any:
|
||||||
self._check_timestamp(kwargs)
|
self._check_timestamp(args)
|
||||||
result = await self.klippy.request(
|
result = await self.klippy.request(WebRequest(endpoint, args))
|
||||||
WebRequest(endpoint, kwargs))
|
|
||||||
return result
|
return result
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
|
@ -207,18 +207,13 @@ class JsonRPC:
|
||||||
method = self.methods.get(method_name, None)
|
method = self.methods.get(method_name, None)
|
||||||
if method is None:
|
if method is None:
|
||||||
return self.build_error(-32601, "Method not found", req_id)
|
return self.build_error(-32601, "Method not found", req_id)
|
||||||
|
params: Dict[str, Any] = {}
|
||||||
if 'params' in obj:
|
if 'params' in obj:
|
||||||
params = obj['params']
|
params = obj['params']
|
||||||
if isinstance(params, list):
|
if not isinstance(params, dict):
|
||||||
response = await self.execute_method(
|
return self.build_error(
|
||||||
method, req_id, conn, *params)
|
-32602, f"Invalid params:", req_id, True)
|
||||||
elif isinstance(params, dict):
|
response = await self.execute_method(method, req_id, conn, params)
|
||||||
response = await self.execute_method(
|
|
||||||
method, req_id, conn, **params)
|
|
||||||
else:
|
|
||||||
return self.build_error(-32600, "Invalid Request", req_id)
|
|
||||||
else:
|
|
||||||
response = await self.execute_method(method, req_id, conn)
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def process_response(
|
def process_response(
|
||||||
|
@ -247,17 +242,15 @@ class JsonRPC:
|
||||||
conn.resolve_pending_response(response_id, ret)
|
conn.resolve_pending_response(response_id, ret)
|
||||||
|
|
||||||
async def execute_method(self,
|
async def execute_method(self,
|
||||||
method: RPCCallback,
|
callback: RPCCallback,
|
||||||
req_id: Optional[int],
|
req_id: Optional[int],
|
||||||
conn: Optional[WebSocket],
|
conn: Optional[WebSocket],
|
||||||
*args,
|
params: Dict[str, Any]
|
||||||
**kwargs
|
|
||||||
) -> Optional[Dict[str, Any]]:
|
) -> Optional[Dict[str, Any]]:
|
||||||
|
if conn is not None:
|
||||||
|
params["_socket_"] = conn
|
||||||
try:
|
try:
|
||||||
if conn is not None:
|
result = await callback(params)
|
||||||
result = await method(conn, *args, **kwargs)
|
|
||||||
else:
|
|
||||||
result = await method(*args, **kwargs)
|
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
return self.build_error(
|
return self.build_error(
|
||||||
-32602, f"Invalid params:\n{e}", req_id, True)
|
-32602, f"Invalid params:\n{e}", req_id, True)
|
||||||
|
@ -351,9 +344,10 @@ class WebsocketManager(APITransport):
|
||||||
self.rpc.remove_method(jrpc_method)
|
self.rpc.remove_method(jrpc_method)
|
||||||
|
|
||||||
def _generate_callback(self, endpoint: str) -> RPCCallback:
|
def _generate_callback(self, endpoint: str) -> RPCCallback:
|
||||||
async def func(ws: WebSocket, **kwargs) -> Any:
|
async def func(args: Dict[str, Any]) -> Any:
|
||||||
|
ws: WebSocket = args.pop("_socket_")
|
||||||
result = await self.klippy.request(
|
result = await self.klippy.request(
|
||||||
WebRequest(endpoint, kwargs, conn=ws, ip_addr=ws.ip_addr,
|
WebRequest(endpoint, args, conn=ws, ip_addr=ws.ip_addr,
|
||||||
user=ws.current_user))
|
user=ws.current_user))
|
||||||
return result
|
return result
|
||||||
return func
|
return func
|
||||||
|
@ -363,32 +357,29 @@ class WebsocketManager(APITransport):
|
||||||
request_method: str,
|
request_method: str,
|
||||||
callback: Callable[[WebRequest], Coroutine]
|
callback: Callable[[WebRequest], Coroutine]
|
||||||
) -> RPCCallback:
|
) -> RPCCallback:
|
||||||
async def func(ws: WebSocket, **kwargs) -> Any:
|
async def func(args: Dict[str, Any]) -> Any:
|
||||||
|
ws: WebSocket = args.pop("_socket_")
|
||||||
result = await callback(
|
result = await callback(
|
||||||
WebRequest(endpoint, kwargs, request_method, ws,
|
WebRequest(endpoint, args, request_method, ws,
|
||||||
ip_addr=ws.ip_addr, user=ws.current_user))
|
ip_addr=ws.ip_addr, user=ws.current_user))
|
||||||
return result
|
return result
|
||||||
return func
|
return func
|
||||||
|
|
||||||
async def _handle_id_request(self,
|
async def _handle_id_request(self, args: Dict[str, Any]) -> Dict[str, int]:
|
||||||
ws: WebSocket,
|
ws: WebSocket = args["_socket_"]
|
||||||
**kwargs
|
|
||||||
) -> Dict[str, int]:
|
|
||||||
return {'websocket_id': ws.uid}
|
return {'websocket_id': ws.uid}
|
||||||
|
|
||||||
async def _handle_identify(self,
|
async def _handle_identify(self, args: Dict[str, Any]) -> Dict[str, int]:
|
||||||
ws: WebSocket,
|
ws: WebSocket = args["_socket_"]
|
||||||
**kwargs
|
|
||||||
) -> Dict[str, int]:
|
|
||||||
if ws.identified:
|
if ws.identified:
|
||||||
raise self.server.error(
|
raise self.server.error(
|
||||||
f"Connection already identified: {ws.client_data}"
|
f"Connection already identified: {ws.client_data}"
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
name = str(kwargs["client_name"])
|
name = str(args["client_name"])
|
||||||
version = str(kwargs["version"])
|
version = str(args["version"])
|
||||||
client_type: str = str(kwargs["type"]).lower()
|
client_type: str = str(args["type"]).lower()
|
||||||
url = str(kwargs["url"])
|
url = str(args["url"])
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
missing_key = str(e).split(":")[-1].strip()
|
missing_key = str(e).split(":")[-1].strip()
|
||||||
raise self.server.error(
|
raise self.server.error(
|
||||||
|
|
Loading…
Reference in New Issue