From a111819667784f5908dd1adf729ed722b4fdca2e Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 14 Apr 2023 19:05:19 -0400 Subject: [PATCH] Few fixes after review to get running again B) - use `Struct.copy()` for frozen type - fix `BrokerConfig` delegation attr lookups - bit of linting according to `flake8` --- piker/brokers/kucoin.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/piker/brokers/kucoin.py b/piker/brokers/kucoin.py index fef7f5a3..60315030 100644 --- a/piker/brokers/kucoin.py +++ b/piker/brokers/kucoin.py @@ -21,14 +21,12 @@ Kucoin broker backend from typing import ( Any, Callable, - Optional, Literal, AsyncGenerator ) from contextlib import asynccontextmanager as acm from datetime import datetime import time -import math import base64 import hmac import hashlib @@ -168,7 +166,7 @@ class BrokerConfig(Struct, frozen=True): def get_config() -> BrokerConfig | None: - conf, _= config.load() + conf, _ = config.load() section = conf.get('kucoin') @@ -176,7 +174,7 @@ def get_config() -> BrokerConfig | None: log.warning('No config section found for kucoin in config') return None - return BrokerConfig(**section).typecast() + return BrokerConfig(**section).copy() class Client: @@ -209,8 +207,8 @@ class Client: passphrase = base64.b64encode( hmac.new( - self._key_secret.encode('utf-8'), - self._key_passphrase.encode('utf-8'), + self._config.key_secret.encode('utf-8'), + self._config.key_passphrase.encode('utf-8'), hashlib.sha256, ).digest() ) @@ -218,7 +216,7 @@ class Client: return { 'KC-API-SIGN': signature, 'KC-API-TIMESTAMP': str(pendulum.now().int_timestamp * 1000), - 'KC-API-KEY': self._key_id, + 'KC-API-KEY': self._config.key_id, 'KC-API-PASSPHRASE': passphrase, # XXX: Even if using the v1 api - this stays the same 'KC-API-KEY-VERSION': '2', @@ -249,7 +247,10 @@ class Client: f'Error making request to {api_url} -> {res.json()["msg"]}') return res.json()['msg'] - async def _get_ws_token(self, private: bool = False) -> tuple[str, int] | None: + async def _get_ws_token( + self, + private: bool = False, + ) -> tuple[str, int] | None: ''' Fetch ws token needed for sub access: https://docs.kucoin.com/#apply-connect-token @@ -275,7 +276,6 @@ class Client: f'Error making request for Kucoin ws token -> {data.json()["msg"]}' ) - async def _get_pairs( self, ) -> dict[str, KucoinMktPair]: @@ -462,6 +462,7 @@ async def stream_quotes( 'fqsn': sym, }, } + @acm async def subscribe(ws: wsproto.WSConnection): @@ -553,6 +554,7 @@ def make_sub(sym, connect_id, level='l1') -> dict[str, str | bool] | None: 'response': True, } + @trio_async_generator async def stream_messages(ws: NoBsWs, sym: str) -> AsyncGenerator[NoBsWs, dict]: timeouts = 0 @@ -569,7 +571,6 @@ async def stream_messages(ws: NoBsWs, sym: str) -> AsyncGenerator[NoBsWs, dict]: continue - if msg.get('subject'): msg = KucoinMsg(**msg) match msg.subject: