From d5db3ec0d191c7528d162e4cdd9282151d50b0c8 Mon Sep 17 00:00:00 2001 From: Garulf <535299+Garulf@users.noreply.github.com> Date: Mon, 19 Feb 2024 06:16:22 -0500 Subject: [PATCH] Update response --- pyflowlauncher/jsonrpc/models.py | 14 ++++++++++---- pyflowlauncher/jsonrpc/server.py | 17 ++++++++++++----- pyflowlauncher/plugin.py | 4 ++-- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pyflowlauncher/jsonrpc/models.py b/pyflowlauncher/jsonrpc/models.py index 4d4033c..562944e 100644 --- a/pyflowlauncher/jsonrpc/models.py +++ b/pyflowlauncher/jsonrpc/models.py @@ -1,7 +1,7 @@ from __future__ import annotations import sys -from typing import Any, List, Optional +from typing import Any, Dict, List, Optional if sys.version_info < (3, 11): from typing_extensions import NotRequired, TypedDict @@ -20,16 +20,22 @@ class BaseJsonRPCRequest(TypedDict): class JsonRPCRequest(BaseJsonRPCRequest): """Flow Launcher JsonRPC Request""" dontHideAfterAction: NotRequired[bool] - settings: NotRequired[dict] + settings: NotRequired[Dict] class BaseJsonRPCResult(TypedDict): """Standard JsonRPC Result""" - id: NotRequired[int] + id: NotRequired[Optional[int]] jsonrpc: str result: Any class JsonRPCResult(BaseJsonRPCResult): """Flow Launcher JsonRPC Result""" - SettingsChange: NotRequired[Optional[dict]] + SettingsChange: NotRequired[Optional[Dict]] + + +class PartialJsonRPCResult(TypedDict): + """Flow Launcher JsonRPC Result""" + SettingsChange: NotRequired[Optional[Dict]] + result: NotRequired[Dict] diff --git a/pyflowlauncher/jsonrpc/server.py b/pyflowlauncher/jsonrpc/server.py index 1c05602..9d2a487 100644 --- a/pyflowlauncher/jsonrpc/server.py +++ b/pyflowlauncher/jsonrpc/server.py @@ -3,7 +3,7 @@ import json from typing import Any, Dict, Optional -from .models import JsonRPCRequest, JsonRPCResult +from .models import JsonRPCRequest, JsonRPCResult, PartialJsonRPCResult from . import JSONRPC_VER, ids @@ -15,14 +15,21 @@ def parse_request(message: str) -> JsonRPCRequest: return request -def create_response(result: Any, id: int, SettingsChange: Optional[Dict] = None) -> JsonRPCResult: +def create_response(result: Any, SettingsChange: Optional[Dict] = None, id: Optional[int] = None) -> JsonRPCResult: return { "jsonrpc": JSONRPC_VER, "result": result, + "SettingsChange": SettingsChange, "id": id, - "SettingsChange": SettingsChange } -def response(result: Any, id: int, SettingsChange: Optional[Dict] = None) -> str: - return json.dumps(create_response(result, id, SettingsChange)) +def response_string(result: Any, id: Optional[int] = None, SettingsChange: Optional[Dict] = None) -> str: + return json.dumps(create_response(result, SettingsChange, id)) + + +def response(result: Any, SettingsChange: Optional[Dict] = None) -> PartialJsonRPCResult: + return { + "result": result, + "SettingsChange": SettingsChange + } diff --git a/pyflowlauncher/plugin.py b/pyflowlauncher/plugin.py index 9532bb5..f0353f2 100644 --- a/pyflowlauncher/plugin.py +++ b/pyflowlauncher/plugin.py @@ -62,8 +62,8 @@ def action(self, method: Method, parameters: Optional[Iterable] = None) -> JsonR async def run_async(self) -> None: request = server.parse_request(sys.argv[1]) - feedback = await self._event_handler.trigger_event(request["method"], *request["parameters"]) - print(server.response(feedback, request["id"])) + response = await self._event_handler.trigger_event(request["method"], *request["parameters"]) + print(server.response_string(**response, id=request["id"])) def run(self) -> None: if sys.version_info >= (3, 10, 0):