From 840451481665822f5e8f54f6e286ad4efc27c5bd Mon Sep 17 00:00:00 2001 From: calvin Date: Sun, 9 Jul 2023 20:12:22 +0800 Subject: [PATCH 1/3] Add async functions support --- jsonrpc/base.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/jsonrpc/base.py b/jsonrpc/base.py index f9d4196..d822b29 100644 --- a/jsonrpc/base.py +++ b/jsonrpc/base.py @@ -1,3 +1,4 @@ +from inspect import isawaitable from .utils import JSONSerializable @@ -85,3 +86,9 @@ def data(self, value): @property def json(self): return self.serialize(self.data) + + @property + async def async_json(self): + if isawaitable(self.data.get('result')): + self._data['result']=await self.data['result'] + return self.serialize(self.data) From 006ac6deb38f0a69f148e75ea1d6b092e89f2aa5 Mon Sep 17 00:00:00 2001 From: calvinweb <38599774+calvinweb@users.noreply.github.com> Date: Sun, 9 Jul 2023 20:25:13 +0800 Subject: [PATCH 2/3] Update base.py --- jsonrpc/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonrpc/base.py b/jsonrpc/base.py index d822b29..ce33abb 100644 --- a/jsonrpc/base.py +++ b/jsonrpc/base.py @@ -90,5 +90,5 @@ def json(self): @property async def async_json(self): if isawaitable(self.data.get('result')): - self._data['result']=await self.data['result'] + self._data['result'] = await self.data['result'] return self.serialize(self.data) From dda9c926cae7636ff80324e89dd2c94e7cdf67fc Mon Sep 17 00:00:00 2001 From: calvin Date: Sun, 9 Jul 2023 20:52:49 +0800 Subject: [PATCH 3/3] Add tests --- jsonrpc/tests/test_jsonrpc2.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/jsonrpc/tests/test_jsonrpc2.py b/jsonrpc/tests/test_jsonrpc2.py index 32c1639..e3c2273 100644 --- a/jsonrpc/tests/test_jsonrpc2.py +++ b/jsonrpc/tests/test_jsonrpc2.py @@ -625,7 +625,17 @@ def test_data_result(self): "result": "", "id": None, }) - + def test_async_json_result(self): + # Make a coroutine + import asyncio + async def test(): return "" + r= JSONRPC20Response(result=test()) + r=asyncio.run(r.async_json) + self.assertEqual(json.loads(r), { + "jsonrpc": "2.0", + "result": "", + "id": None, + }) def test_data_result_id_none(self): r = JSONRPC20Response(result="", _id=None) self.assertEqual(json.loads(r.json), r.data)