Skip to content

Add async functions support #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jsonrpc/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from inspect import isawaitable
from .utils import JSONSerializable


Expand Down Expand Up @@ -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)
12 changes: 11 additions & 1 deletion jsonrpc/tests/test_jsonrpc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down