from dataclasses import dataclass from typing import Generic, TypeVar, Optional T = TypeVar('T') @dataclass class TickerData: symbol: str tickDirection: str price24hPcnt: str lastPrice: str prevPrice24h: str highPrice24h: str lowPrice24h: str prevPrice1h: str markPrice: str indexPrice: str openInterest: str openInterestValue: str turnover24h: str volume24h: str nextFundingTime: str fundingRate: str bid1Price: str bid1Size: str ask1Price: str ask1Size: str @dataclass class Message(Generic[T]): topic: str type: str data: Optional[T] = None cs: Optional[int] = None ts: Optional[int] = None @staticmethod def from_json(json_dict: dict) -> 'Message[T]': if json_dict['topic'].startswith("tickers"): data = TickerData(**json_dict['data']) else: data = None json_dict.pop('data', None) return Message(**json_dict, data=data)