Added support for JSONRPC requests coming from the server side
parent
cc1694760c
commit
db0b59abaa
|
@ -98,6 +98,11 @@ class JSONRPCResult(Struct):
|
|||
usDiff: int
|
||||
testnet: bool
|
||||
|
||||
class JSONRPCChannel(Struct):
|
||||
jsonrpc: str = '2.0'
|
||||
method: str
|
||||
params: dict
|
||||
|
||||
|
||||
class KLinesResult(Struct):
|
||||
close: list[float]
|
||||
|
|
|
@ -174,7 +174,9 @@ class JSONRPCResult(Struct):
|
|||
async def open_jsonrpc_session(
|
||||
url: str,
|
||||
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]:
|
||||
|
||||
async with (
|
||||
|
@ -221,7 +223,8 @@ async def open_jsonrpc_session(
|
|||
field, then sets the event to wakeup original sender tasks.
|
||||
'''
|
||||
async for msg in ws:
|
||||
msg = dtype(**msg)
|
||||
try:
|
||||
msg = response_type(**msg)
|
||||
|
||||
if msg.id not in rpc_results:
|
||||
log.warning(f'Wasn\'t expecting ws msg: {json.dumps(msg, indent=4)}')
|
||||
|
@ -234,6 +237,13 @@ async def open_jsonrpc_session(
|
|||
res['result'] = msg
|
||||
res['event'].set()
|
||||
|
||||
except TypeError:
|
||||
if request_type == None:
|
||||
raise
|
||||
await request_hook(request_type(**msg))
|
||||
|
||||
|
||||
|
||||
|
||||
n.start_soon(recv_task)
|
||||
yield json_rpc
|
||||
|
|
Loading…
Reference in New Issue