new spectra format (#1302)
This commit is contained in:
+5
-16
@@ -15,10 +15,11 @@ from typing import Any, Awaitable, Callable, Dict, List, Optional
|
|||||||
|
|
||||||
# Third-Party Library Imports
|
# Third-Party Library Imports
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import zstandard as zstd
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from websockets.asyncio.client import connect as wsconnect
|
from websockets.asyncio.client import connect as wsconnect
|
||||||
|
|
||||||
|
from spectra_ws_payload import SpectraPayloadError, decode_spectra_ws_payload
|
||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@@ -74,7 +75,6 @@ async def ws_replay_listener(callback: Callable[[List[Dict[str, Any]]], Awaitabl
|
|||||||
"""
|
"""
|
||||||
auth_value = API_KEY if API_KEY.startswith("Bearer ") else f"Bearer {API_KEY}"
|
auth_value = API_KEY if API_KEY.startswith("Bearer ") else f"Bearer {API_KEY}"
|
||||||
headers = {"Authorization": auth_value}
|
headers = {"Authorization": auth_value}
|
||||||
decompressor = zstd.ZstdDecompressor()
|
|
||||||
reconnect_delay = 1
|
reconnect_delay = 1
|
||||||
|
|
||||||
# Start queue processor as background task
|
# Start queue processor as background task
|
||||||
@@ -86,23 +86,12 @@ async def ws_replay_listener(callback: Callable[[List[Dict[str, Any]]], Awaitabl
|
|||||||
logger.info(f"WebSocket connected to {label}")
|
logger.info(f"WebSocket connected to {label}")
|
||||||
async for message in ws:
|
async for message in ws:
|
||||||
try:
|
try:
|
||||||
if isinstance(message, str):
|
data = decode_spectra_ws_payload(message)
|
||||||
raw = message.encode("utf-8")
|
|
||||||
else:
|
|
||||||
raw = bytes(message)
|
|
||||||
|
|
||||||
text: str
|
|
||||||
try:
|
|
||||||
text = decompressor.decompress(raw, max_output_size=64 * 1024 * 1024).decode("utf-8")
|
|
||||||
except zstd.ZstdError:
|
|
||||||
text = raw.decode("utf-8")
|
|
||||||
|
|
||||||
data = json.loads(text)
|
|
||||||
replays = normalize_ws_message(data)
|
replays = normalize_ws_message(data)
|
||||||
if replays:
|
if replays:
|
||||||
await _replay_queue.put(replays)
|
await _replay_queue.put(replays)
|
||||||
except json.JSONDecodeError:
|
except SpectraPayloadError as e:
|
||||||
logger.warning("Invalid JSON from WS frame")
|
logger.warning(f"Invalid Spectra WS frame: {e}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error processing WS message: {e}")
|
logger.error(f"Error processing WS message: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user