Remove cryptofeeds/asyncio from deribit backend
Add hook management to open_jsonrpc_session helperderibit_updates
parent
4c838474be
commit
1c833e7175
|
@ -52,13 +52,3 @@ __enable_modules__: list[str] = [
|
||||||
'feed',
|
'feed',
|
||||||
# 'broker',
|
# 'broker',
|
||||||
]
|
]
|
||||||
|
|
||||||
# passed to ``tractor.ActorNursery.start_actor()``
|
|
||||||
_spawn_kwargs = {
|
|
||||||
'infect_asyncio': True,
|
|
||||||
}
|
|
||||||
|
|
||||||
# annotation to let backend agnostic code
|
|
||||||
# know if ``brokerd`` should be spawned with
|
|
||||||
# ``tractor``'s aio mode.
|
|
||||||
_infect_asyncio: bool = True
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ Deribit backend.
|
||||||
'''
|
'''
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import time
|
import time
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from contextlib import asynccontextmanager as acm
|
from contextlib import asynccontextmanager as acm
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
@ -31,15 +30,6 @@ from typing import (
|
||||||
Callable,
|
Callable,
|
||||||
)
|
)
|
||||||
|
|
||||||
from cryptofeed import FeedHandler
|
|
||||||
from cryptofeed.defines import (
|
|
||||||
DERIBIT,
|
|
||||||
L1_BOOK,
|
|
||||||
TRADES,
|
|
||||||
OPTION,
|
|
||||||
CALL,
|
|
||||||
PUT,
|
|
||||||
)
|
|
||||||
import pendulum
|
import pendulum
|
||||||
import trio
|
import trio
|
||||||
from trio_typing import TaskStatus
|
from trio_typing import TaskStatus
|
||||||
|
@ -49,8 +39,6 @@ from tractor.trionics import (
|
||||||
broadcast_receiver,
|
broadcast_receiver,
|
||||||
maybe_open_context
|
maybe_open_context
|
||||||
)
|
)
|
||||||
from tractor import to_asyncio
|
|
||||||
from cryptofeed.symbols import Symbol
|
|
||||||
|
|
||||||
from piker.data.types import Struct
|
from piker.data.types import Struct
|
||||||
from piker.data._web_bs import (
|
from piker.data._web_bs import (
|
||||||
|
@ -59,16 +47,12 @@ from piker.data._web_bs import (
|
||||||
|
|
||||||
from piker import config
|
from piker import config
|
||||||
from piker.log import get_logger
|
from piker.log import get_logger
|
||||||
|
from piker._cacheables import open_cached_client
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
_spawn_kwargs = {
|
|
||||||
'infect_asyncio': True,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_url = 'https://www.deribit.com'
|
_url = 'https://www.deribit.com'
|
||||||
_ws_url = 'wss://www.deribit.com/ws/api/v2'
|
_ws_url = 'wss://www.deribit.com/ws/api/v2'
|
||||||
_testnet_ws_url = 'wss://test.deribit.com/ws/api/v2'
|
_testnet_ws_url = 'wss://test.deribit.com/ws/api/v2'
|
||||||
|
@ -142,70 +126,12 @@ def deribit_timestamp(when):
|
||||||
return int((when.timestamp() * 1000) + (when.microsecond / 1000))
|
return int((when.timestamp() * 1000) + (when.microsecond / 1000))
|
||||||
|
|
||||||
|
|
||||||
def str_to_cb_sym(name: str) -> Symbol:
|
def sym_fmt_piker_to_deribit(sym: str) -> str:
|
||||||
base, strike_price, expiry_date, option_type = name.split('-')
|
return sym.upper()
|
||||||
|
|
||||||
quote = base
|
|
||||||
|
|
||||||
if option_type == 'put':
|
|
||||||
option_type = PUT
|
|
||||||
elif option_type == 'call':
|
|
||||||
option_type = CALL
|
|
||||||
else:
|
|
||||||
raise Exception("Couldn\'t parse option type")
|
|
||||||
|
|
||||||
return Symbol(
|
|
||||||
base, quote,
|
|
||||||
type=OPTION,
|
|
||||||
strike_price=strike_price,
|
|
||||||
option_type=option_type,
|
|
||||||
expiry_date=expiry_date,
|
|
||||||
expiry_normalize=False)
|
|
||||||
|
|
||||||
|
|
||||||
def piker_sym_to_cb_sym(name: str) -> Symbol:
|
def sym_fmt_deribit_to_piker(sym: str):
|
||||||
base, expiry_date, strike_price, option_type = tuple(
|
return sym.lower()
|
||||||
name.upper().split('-'))
|
|
||||||
|
|
||||||
quote = base
|
|
||||||
|
|
||||||
if option_type == 'P':
|
|
||||||
option_type = PUT
|
|
||||||
elif option_type == 'C':
|
|
||||||
option_type = CALL
|
|
||||||
else:
|
|
||||||
raise Exception("Couldn\'t parse option type")
|
|
||||||
|
|
||||||
return Symbol(
|
|
||||||
base, quote,
|
|
||||||
type=OPTION,
|
|
||||||
strike_price=strike_price,
|
|
||||||
option_type=option_type,
|
|
||||||
expiry_date=expiry_date.upper())
|
|
||||||
|
|
||||||
|
|
||||||
def cb_sym_to_deribit_inst(sym: Symbol):
|
|
||||||
# cryptofeed normalized
|
|
||||||
cb_norm = ['F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z']
|
|
||||||
|
|
||||||
# deribit specific
|
|
||||||
months = [
|
|
||||||
'JAN', 'FEB', 'MAR',
|
|
||||||
'APR', 'MAY', 'JUN',
|
|
||||||
'JUL', 'AUG', 'SEP',
|
|
||||||
'OCT', 'NOV', 'DEC',
|
|
||||||
]
|
|
||||||
|
|
||||||
exp = sym.expiry_date
|
|
||||||
|
|
||||||
# YYMDD
|
|
||||||
# 01234
|
|
||||||
year, month, day = (
|
|
||||||
exp[:2], months[cb_norm.index(exp[2:3])], exp[3:])
|
|
||||||
|
|
||||||
otype = 'C' if sym.option_type == CALL else 'P'
|
|
||||||
|
|
||||||
return f'{sym.base}-{day}{month}{year}-{sym.strike_price}-{otype}'
|
|
||||||
|
|
||||||
|
|
||||||
def get_config() -> dict[str, Any]:
|
def get_config() -> dict[str, Any]:
|
||||||
|
@ -214,11 +140,6 @@ def get_config() -> dict[str, Any]:
|
||||||
|
|
||||||
section = conf.get('deribit')
|
section = conf.get('deribit')
|
||||||
|
|
||||||
# TODO: document why we send this, basically because logging params
|
|
||||||
# for cryptofeed
|
|
||||||
conf['log'] = {}
|
|
||||||
conf['log']['disabled'] = True
|
|
||||||
|
|
||||||
if section is None:
|
if section is None:
|
||||||
log.warning(f'No config section found for deribit in {path}')
|
log.warning(f'No config section found for deribit in {path}')
|
||||||
|
|
||||||
|
@ -227,7 +148,13 @@ def get_config() -> dict[str, Any]:
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
|
|
||||||
def __init__(self, json_rpc: Callable) -> None:
|
def __init__(
|
||||||
|
self,
|
||||||
|
json_rpc: Callable,
|
||||||
|
update_hooks: Callable,
|
||||||
|
update_types: Callable,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
self._pairs: dict[str, Any] = None
|
self._pairs: dict[str, Any] = None
|
||||||
|
|
||||||
config = get_config().get('deribit', {})
|
config = get_config().get('deribit', {})
|
||||||
|
@ -241,6 +168,8 @@ class Client:
|
||||||
self._key_secret = None
|
self._key_secret = None
|
||||||
|
|
||||||
self.json_rpc = json_rpc
|
self.json_rpc = json_rpc
|
||||||
|
self.update_hooks = update_hooks
|
||||||
|
self.update_types = update_types
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def currencies(self):
|
def currencies(self):
|
||||||
|
@ -287,7 +216,7 @@ class Client:
|
||||||
"""Place an order
|
"""Place an order
|
||||||
"""
|
"""
|
||||||
params = {
|
params = {
|
||||||
'instrument_name': symbol.upper(),
|
'instrument_name': sym_fmt_piker_to_deribit(symbol),
|
||||||
'amount': size,
|
'amount': size,
|
||||||
'type': 'limit',
|
'type': 'limit',
|
||||||
'price': price,
|
'price': price,
|
||||||
|
@ -328,7 +257,7 @@ class Client:
|
||||||
results = resp.result
|
results = resp.result
|
||||||
|
|
||||||
instruments = {
|
instruments = {
|
||||||
item['instrument_name'].lower(): item
|
sym_fmt_deribit_to_piker(item['instrument_name']): item
|
||||||
for item in results
|
for item in results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,8 +288,10 @@ class Client:
|
||||||
limit=limit
|
limit=limit
|
||||||
)
|
)
|
||||||
# repack in dict form
|
# repack in dict form
|
||||||
return {item[0]['instrument_name'].lower(): item[0]
|
return {
|
||||||
for item in matches}
|
sym_fmt_deribit_to_piker(item[0]['instrument_name']): item[0]
|
||||||
|
for item in matches
|
||||||
|
}
|
||||||
|
|
||||||
async def bars(
|
async def bars(
|
||||||
self,
|
self,
|
||||||
|
@ -387,7 +318,7 @@ class Client:
|
||||||
resp = await self.json_rpc(
|
resp = await self.json_rpc(
|
||||||
'public/get_tradingview_chart_data',
|
'public/get_tradingview_chart_data',
|
||||||
params={
|
params={
|
||||||
'instrument_name': instrument.upper(),
|
'instrument_name': sym_fmt_piker_to_deribit(instrument),
|
||||||
'start_timestamp': start_time,
|
'start_timestamp': start_time,
|
||||||
'end_timestamp': end_time,
|
'end_timestamp': end_time,
|
||||||
'resolution': '1'
|
'resolution': '1'
|
||||||
|
@ -420,13 +351,19 @@ class Client:
|
||||||
resp = await self.json_rpc(
|
resp = await self.json_rpc(
|
||||||
'public/get_last_trades_by_instrument',
|
'public/get_last_trades_by_instrument',
|
||||||
params={
|
params={
|
||||||
'instrument_name': instrument,
|
'instrument_name': sym_fmt_piker_to_deribit(instrument),
|
||||||
'count': count
|
'count': count
|
||||||
})
|
})
|
||||||
|
|
||||||
return LastTradesResult(**resp.result)
|
return LastTradesResult(**resp.result)
|
||||||
|
|
||||||
|
|
||||||
|
class JSONRPCSubRequest(Struct):
|
||||||
|
method: str
|
||||||
|
params: dict
|
||||||
|
jsonrpc: str = '2.0'
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def get_client(
|
async def get_client(
|
||||||
is_brokercheck: bool = False
|
is_brokercheck: bool = False
|
||||||
|
@ -435,11 +372,11 @@ async def get_client(
|
||||||
async with (
|
async with (
|
||||||
trio.open_nursery() as n,
|
trio.open_nursery() as n,
|
||||||
open_jsonrpc_session(
|
open_jsonrpc_session(
|
||||||
_testnet_ws_url,
|
_ws_url,
|
||||||
response_type=JSONRPCResult
|
response_type=JSONRPCResult
|
||||||
) as json_rpc
|
) as control_functions
|
||||||
):
|
):
|
||||||
client = Client(json_rpc)
|
client = Client(*control_functions)
|
||||||
|
|
||||||
_refresh_token: Optional[str] = None
|
_refresh_token: Optional[str] = None
|
||||||
_access_token: Optional[str] = None
|
_access_token: Optional[str] = None
|
||||||
|
@ -452,7 +389,7 @@ async def get_client(
|
||||||
|
|
||||||
https://docs.deribit.com/?python#authentication-2
|
https://docs.deribit.com/?python#authentication-2
|
||||||
"""
|
"""
|
||||||
renew_time = 10
|
renew_time = 240
|
||||||
access_scope = 'trade:read_write'
|
access_scope = 'trade:read_write'
|
||||||
_expiry_time = time.time()
|
_expiry_time = time.time()
|
||||||
got_access = False
|
got_access = False
|
||||||
|
@ -482,7 +419,7 @@ async def get_client(
|
||||||
'scope': access_scope
|
'scope': access_scope
|
||||||
}
|
}
|
||||||
|
|
||||||
resp = await json_rpc('public/auth', params)
|
resp = await client.json_rpc('public/auth', params)
|
||||||
result = resp.result
|
result = resp.result
|
||||||
|
|
||||||
_expiry_time = time.time() + result['expires_in']
|
_expiry_time = time.time() + result['expires_in']
|
||||||
|
@ -509,97 +446,68 @@ async def get_client(
|
||||||
n.cancel_scope.cancel()
|
n.cancel_scope.cancel()
|
||||||
|
|
||||||
|
|
||||||
@acm
|
|
||||||
async def open_feed_handler():
|
|
||||||
fh = FeedHandler(config=get_config())
|
|
||||||
yield fh
|
|
||||||
await to_asyncio.run_task(fh.stop_async)
|
|
||||||
|
|
||||||
|
|
||||||
@acm
|
|
||||||
async def maybe_open_feed_handler() -> trio.abc.ReceiveStream:
|
|
||||||
async with maybe_open_context(
|
|
||||||
acm_func=open_feed_handler,
|
|
||||||
key='feedhandler',
|
|
||||||
) as (cache_hit, fh):
|
|
||||||
yield fh
|
|
||||||
|
|
||||||
|
|
||||||
async def aio_price_feed_relay(
|
|
||||||
fh: FeedHandler,
|
|
||||||
instrument: Symbol,
|
|
||||||
from_trio: asyncio.Queue,
|
|
||||||
to_trio: trio.abc.SendChannel,
|
|
||||||
) -> None:
|
|
||||||
async def _trade(data: dict, receipt_timestamp):
|
|
||||||
to_trio.send_nowait(('trade', {
|
|
||||||
'symbol': cb_sym_to_deribit_inst(
|
|
||||||
str_to_cb_sym(data.symbol)).lower(),
|
|
||||||
'last': data,
|
|
||||||
'broker_ts': time.time(),
|
|
||||||
'data': data.to_dict(),
|
|
||||||
'receipt': receipt_timestamp
|
|
||||||
}))
|
|
||||||
|
|
||||||
async def _l1(data: dict, receipt_timestamp):
|
|
||||||
to_trio.send_nowait(
|
|
||||||
('l1', {
|
|
||||||
'symbol': cb_sym_to_deribit_inst(
|
|
||||||
str_to_cb_sym(data.symbol)).lower(),
|
|
||||||
'ticks': [
|
|
||||||
|
|
||||||
{'type': 'bid',
|
|
||||||
'price': float(data.bid_price),
|
|
||||||
'size': float(data.bid_size)},
|
|
||||||
|
|
||||||
{'type': 'bsize',
|
|
||||||
'price': float(data.bid_price),
|
|
||||||
'size': float(data.bid_size)},
|
|
||||||
|
|
||||||
{'type': 'ask',
|
|
||||||
'price': float(data.ask_price),
|
|
||||||
'size': float(data.ask_size)},
|
|
||||||
|
|
||||||
{'type': 'asize',
|
|
||||||
'price': float(data.ask_price),
|
|
||||||
'size': float(data.ask_size)}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
fh.add_feed(
|
|
||||||
DERIBIT,
|
|
||||||
channels=[TRADES, L1_BOOK],
|
|
||||||
symbols=[piker_sym_to_cb_sym(instrument)],
|
|
||||||
callbacks={
|
|
||||||
TRADES: _trade,
|
|
||||||
L1_BOOK: _l1
|
|
||||||
})
|
|
||||||
|
|
||||||
if not fh.running:
|
|
||||||
fh.run(
|
|
||||||
start_loop=False,
|
|
||||||
install_signal_handlers=False)
|
|
||||||
|
|
||||||
# sync with trio
|
|
||||||
to_trio.send_nowait(None)
|
|
||||||
|
|
||||||
await asyncio.sleep(float('inf'))
|
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def open_price_feed(
|
async def open_price_feed(
|
||||||
instrument: str
|
instrument: str
|
||||||
) -> trio.abc.ReceiveStream:
|
) -> trio.abc.ReceiveStream:
|
||||||
async with maybe_open_feed_handler() as fh:
|
|
||||||
async with to_asyncio.open_channel_from(
|
instrument_db = sym_fmt_piker_to_deribit(instrument)
|
||||||
partial(
|
|
||||||
aio_price_feed_relay,
|
trades_chan = f'trades.{instrument_db}.raw'
|
||||||
fh,
|
book_chan = f'book.{instrument_db}.none.1.100ms'
|
||||||
instrument
|
|
||||||
)
|
channels = [trades_chan, book_chan]
|
||||||
) as (first, chan):
|
|
||||||
yield chan
|
send_chann, recv_chann = trio.open_memory_channel(0)
|
||||||
|
async def sub_hook(msg):
|
||||||
|
chan = msg.params['channel']
|
||||||
|
data = msg.params['data']
|
||||||
|
if chan == trades_chan:
|
||||||
|
await send_chann.send((
|
||||||
|
'trade', {
|
||||||
|
'symbol': instrument,
|
||||||
|
'last': data['price'],
|
||||||
|
'brokerd_ts': time.time(),
|
||||||
|
'ticks': [{
|
||||||
|
'type': 'trade',
|
||||||
|
'price': data['price'],
|
||||||
|
'size': data['amount'],
|
||||||
|
'broker_ts': data['timestamp']
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
))
|
||||||
|
|
||||||
|
elif chan == book_chan:
|
||||||
|
bid, bsize = data['bids'][0]
|
||||||
|
ask, asize = data['asks'][0]
|
||||||
|
await send_chann.send((
|
||||||
|
'l1', {
|
||||||
|
'symbol': instrument,
|
||||||
|
'ticks': [
|
||||||
|
{'type': 'bid', 'price': bid, 'size': bsize},
|
||||||
|
{'type': 'bsize', 'price': bid, 'size': bsize},
|
||||||
|
{'type': 'ask', 'price': ask, 'size': asize},
|
||||||
|
{'type': 'asize', 'price': ask, 'size': asize}
|
||||||
|
]}
|
||||||
|
))
|
||||||
|
|
||||||
|
async with open_cached_client('deribit') as client:
|
||||||
|
|
||||||
|
client.update_hooks({
|
||||||
|
'request': sub_hook
|
||||||
|
})
|
||||||
|
client.update_types({
|
||||||
|
'request': JSONRPCSubRequest
|
||||||
|
})
|
||||||
|
|
||||||
|
resp = await client.json_rpc(
|
||||||
|
'private/subscribe', {'channels': channels})
|
||||||
|
|
||||||
|
assert resp.result == channels
|
||||||
|
|
||||||
|
log.info(f'Subscribed to {channels}')
|
||||||
|
|
||||||
|
yield recv_chann
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
|
|
|
@ -40,16 +40,9 @@ from piker.brokers._util import (
|
||||||
from .api import (
|
from .api import (
|
||||||
Client,
|
Client,
|
||||||
Trade,
|
Trade,
|
||||||
piker_sym_to_cb_sym,
|
|
||||||
cb_sym_to_deribit_inst,
|
|
||||||
maybe_open_price_feed
|
maybe_open_price_feed
|
||||||
)
|
)
|
||||||
|
|
||||||
_spawn_kwargs = {
|
|
||||||
'infect_asyncio': True,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -107,10 +100,7 @@ async def stream_quotes(
|
||||||
|
|
||||||
sym = symbols[0]
|
sym = symbols[0]
|
||||||
|
|
||||||
async with (
|
async with open_cached_client('deribit') as client:
|
||||||
open_cached_client('deribit') as client,
|
|
||||||
send_chan as send_chan
|
|
||||||
):
|
|
||||||
|
|
||||||
init_msgs = {
|
init_msgs = {
|
||||||
# pass back token, and bool, signalling if we're the writer
|
# pass back token, and bool, signalling if we're the writer
|
||||||
|
@ -118,22 +108,19 @@ async def stream_quotes(
|
||||||
sym: {
|
sym: {
|
||||||
'symbol_info': {
|
'symbol_info': {
|
||||||
'asset_type': 'option',
|
'asset_type': 'option',
|
||||||
'price_tick_size': 0.0005
|
'price_tick_size': 0.0005,
|
||||||
|
'lot_tick_size': 0.1
|
||||||
},
|
},
|
||||||
'shm_write_opts': {'sum_tick_vml': False},
|
'shm_write_opts': {'sum_tick_vml': False},
|
||||||
'fqsn': sym,
|
'fqsn': sym,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
nsym = piker_sym_to_cb_sym(sym)
|
|
||||||
|
last_trades = (await client.last_trades(sym, count=1)).trades
|
||||||
|
|
||||||
async with maybe_open_price_feed(sym) as stream:
|
async with maybe_open_price_feed(sym) as stream:
|
||||||
|
|
||||||
await client.cache_symbols()
|
|
||||||
|
|
||||||
last_trades = (await client.last_trades(
|
|
||||||
cb_sym_to_deribit_inst(nsym), count=1)).trades
|
|
||||||
|
|
||||||
if len(last_trades) == 0:
|
if len(last_trades) == 0:
|
||||||
last_trade = None
|
last_trade = None
|
||||||
async for typ, quote in stream:
|
async for typ, quote in stream:
|
||||||
|
|
|
@ -187,7 +187,6 @@ over a NoBsWs.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
class JSONRPCResult(Struct):
|
class JSONRPCResult(Struct):
|
||||||
id: int
|
id: int
|
||||||
jsonrpc: str = '2.0'
|
jsonrpc: str = '2.0'
|
||||||
|
@ -202,9 +201,32 @@ async def open_jsonrpc_session(
|
||||||
response_type: type = JSONRPCResult,
|
response_type: type = JSONRPCResult,
|
||||||
request_type: Optional[type] = None,
|
request_type: Optional[type] = None,
|
||||||
request_hook: Optional[Callable] = None,
|
request_hook: Optional[Callable] = None,
|
||||||
error_hook: Optional[Callable] = None,
|
error_hook: Optional[Callable] = None
|
||||||
) -> Callable[[str, dict], dict]:
|
) -> Callable[[str, dict], dict]:
|
||||||
|
|
||||||
|
# xor: this two params need to be passed together or not at all
|
||||||
|
if bool(request_type) ^ bool(request_hook):
|
||||||
|
raise ValueError(
|
||||||
|
'Need to path both a request_type and request_hook')
|
||||||
|
|
||||||
|
hook_table = {
|
||||||
|
'request': request_hook,
|
||||||
|
'error': error_hook
|
||||||
|
}
|
||||||
|
|
||||||
|
types_table = {
|
||||||
|
'response': response_type,
|
||||||
|
'request': request_type
|
||||||
|
}
|
||||||
|
|
||||||
|
def update_hooks(new_hooks: dict):
|
||||||
|
nonlocal hook_table
|
||||||
|
hook_table.update(new_hooks)
|
||||||
|
|
||||||
|
def update_types(new_types: dict):
|
||||||
|
nonlocal types_table
|
||||||
|
types_table.update(new_types)
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
trio.open_nursery() as n,
|
trio.open_nursery() as n,
|
||||||
open_autorecon_ws(url) as ws
|
open_autorecon_ws(url) as ws
|
||||||
|
@ -257,8 +279,7 @@ async def open_jsonrpc_session(
|
||||||
'result': _,
|
'result': _,
|
||||||
'id': mid,
|
'id': mid,
|
||||||
} if res_entry := rpc_results.get(mid):
|
} if res_entry := rpc_results.get(mid):
|
||||||
|
res_entry['result'] = types_table['response'](**msg)
|
||||||
res_entry['result'] = response_type(**msg)
|
|
||||||
res_entry['event'].set()
|
res_entry['event'].set()
|
||||||
|
|
||||||
case {
|
case {
|
||||||
|
@ -269,24 +290,32 @@ async def open_jsonrpc_session(
|
||||||
f'Unexpected ws msg: {json.dumps(msg, indent=4)}'
|
f'Unexpected ws msg: {json.dumps(msg, indent=4)}'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
case {
|
||||||
|
'error': error,
|
||||||
|
'id': mid
|
||||||
|
} if res_entry := rpc_results.get(mid):
|
||||||
|
|
||||||
|
res_entry['result'] = types_table['response'](**msg)
|
||||||
|
res_entry['event'].set()
|
||||||
|
|
||||||
case {
|
case {
|
||||||
'method': _,
|
'method': _,
|
||||||
'params': _,
|
'params': _,
|
||||||
}:
|
}:
|
||||||
log.debug(f'Recieved\n{msg}')
|
log.info(f'Recieved\n{msg}')
|
||||||
if request_hook:
|
if hook_table['request']:
|
||||||
await request_hook(request_type(**msg))
|
await hook_table['request'](types_table['request'](**msg))
|
||||||
|
|
||||||
case {
|
case {
|
||||||
'error': error
|
'error': error,
|
||||||
}:
|
}:
|
||||||
log.warning(f'Recieved\n{error}')
|
log.warning(f'Recieved\n{error}')
|
||||||
if error_hook:
|
if hook_table['error']:
|
||||||
await error_hook(response_type(**msg))
|
await hook_table['error'](types_table['response'](**msg))
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
log.warning(f'Unhandled JSON-RPC msg!?\n{msg}')
|
log.warning(f'Unhandled JSON-RPC msg!?\n{msg}')
|
||||||
|
|
||||||
n.start_soon(recv_task)
|
n.start_soon(recv_task)
|
||||||
yield json_rpc
|
yield json_rpc, update_hooks, update_types
|
||||||
n.cancel_scope.cancel()
|
n.cancel_scope.cancel()
|
||||||
|
|
Loading…
Reference in New Issue