Added support for JSONRPC requests coming from the server side

dark_clearing_improvements
Esmeralda Gallardo 2022-10-19 09:59:29 -03:00
parent cc1694760c
commit db0b59abaa
No known key found for this signature in database
GPG Key ID: AD67FBB417CAC71B
2 changed files with 26 additions and 11 deletions

View File

@ -98,6 +98,11 @@ class JSONRPCResult(Struct):
usDiff: int usDiff: int
testnet: bool testnet: bool
class JSONRPCChannel(Struct):
jsonrpc: str = '2.0'
method: str
params: dict
class KLinesResult(Struct): class KLinesResult(Struct):
close: list[float] close: list[float]

View File

@ -174,7 +174,9 @@ class JSONRPCResult(Struct):
async def open_jsonrpc_session( async def open_jsonrpc_session(
url: str, url: str,
start_id: int = 0, start_id: int = 0,
dtype: type = JSONRPCResult response_type: type = JSONRPCResult,
request_type: Optional[type] = None,
request_hook: Optional[Callable] = None
) -> Callable[[str, dict], dict]: ) -> Callable[[str, dict], dict]:
async with ( async with (
@ -221,18 +223,26 @@ async def open_jsonrpc_session(
field, then sets the event to wakeup original sender tasks. field, then sets the event to wakeup original sender tasks.
''' '''
async for msg in ws: async for msg in ws:
msg = dtype(**msg) try:
msg = response_type(**msg)
if msg.id not in rpc_results: if msg.id not in rpc_results:
log.warning(f'Wasn\'t expecting ws msg: {json.dumps(msg, indent=4)}') log.warning(f'Wasn\'t expecting ws msg: {json.dumps(msg, indent=4)}')
res = rpc_results.setdefault(
msg.id,
{'result': None, 'event': trio.Event()}
)
res['result'] = msg
res['event'].set()
except TypeError:
if request_type == None:
raise
await request_hook(request_type(**msg))
res = rpc_results.setdefault(
msg.id,
{'result': None, 'event': trio.Event()}
)
res['result'] = msg
res['event'].set()
n.start_soon(recv_task) n.start_soon(recv_task)