async def listen_for_order_book_snapshots(self,
                                           ev_loop: asyncio.BaseEventLoop,
                                           output: asyncio.Queue):
     while True:
         try:
             trading_pairs: List[str] = self._trading_pairs
             async with aiohttp.ClientSession() as client:
                 for trading_pair in trading_pairs:
                     try:
                         snapshot: Dict[str, Any] = await self.get_snapshot(
                             client, trading_pair)
                         snapshot_message: OrderBookMessage = HuobiOrderBook.snapshot_message_from_exchange(
                             snapshot,
                             metadata={"trading_pair": trading_pair})
                         output.put_nowait(snapshot_message)
                         self.logger().debug(
                             f"Saved order book snapshot for {trading_pair}"
                         )
                         await asyncio.sleep(5.0)
                     except asyncio.CancelledError:
                         raise
                     except Exception:
                         self.logger().error("Unexpected error.",
                                             exc_info=True)
                         await asyncio.sleep(5.0)
                 this_hour: pd.Timestamp = pd.Timestamp.utcnow().replace(
                     minute=0, second=0, microsecond=0)
                 next_hour: pd.Timestamp = this_hour + pd.Timedelta(hours=1)
                 delta: float = next_hour.timestamp() - time.time()
                 await asyncio.sleep(delta)
         except asyncio.CancelledError:
             raise
         except Exception:
             self.logger().error("Unexpected error.", exc_info=True)
             await asyncio.sleep(5.0)
 async def get_new_order_book(self, trading_pair: str) -> OrderBook:
     async with aiohttp.ClientSession() as client:
         snapshot: Dict[str, Any] = await self.get_snapshot(
             client, trading_pair)
         snapshot_msg: OrderBookMessage = HuobiOrderBook.snapshot_message_from_exchange(
             snapshot, metadata={"trading_pair": trading_pair})
         order_book: OrderBook = self.order_book_create_function()
         order_book.apply_snapshot(snapshot_msg.bids, snapshot_msg.asks,
                                   snapshot_msg.update_id)
         return order_book
 async def get_new_order_book(self, trading_pair: str) -> OrderBook:
     snapshot: Dict[str, Any] = await self.get_snapshot(trading_pair)
     timestamp = snapshot["tick"]["ts"]
     snapshot_msg: OrderBookMessage = HuobiOrderBook.snapshot_message_from_exchange(
         msg=snapshot,
         timestamp=timestamp,
         metadata={"trading_pair": trading_pair},
     )
     order_book: OrderBook = self.order_book_create_function()
     order_book.apply_snapshot(snapshot_msg.bids, snapshot_msg.asks, snapshot_msg.update_id)
     return order_book
 async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue):
     message_queue = self._message_queue[self.ORDERBOOK_CHANNEL_SUFFIX]
     while True:
         try:
             msg: Dict[str, Any] = await message_queue.get()
             timestamp = msg["tick"]["ts"]
             order_book_message: OrderBookMessage = HuobiOrderBook.diff_message_from_exchange(
                 msg=msg,
                 timestamp=timestamp
             )
             output.put_nowait(order_book_message)
         except asyncio.CancelledError:
             raise
         except Exception:
             self.logger().error("Unexpected error with WebSocket connection. Retrying after 30 seconds...",
                                 exc_info=True)
             await self._sleep(30.0)
    async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop,
                                output: asyncio.Queue):
        while True:
            try:
                trading_pairs: List[str] = self._trading_pairs
                async with websockets.connect(HUOBI_WS_URI) as ws:
                    ws: websockets.WebSocketClientProtocol = ws
                    for trading_pair in trading_pairs:
                        subscribe_request: Dict[str, Any] = {
                            "sub":
                            f"market.{convert_to_exchange_trading_pair(trading_pair)}.trade.detail",
                            "id":
                            convert_to_exchange_trading_pair(trading_pair)
                        }
                        await ws.send(json.dumps(subscribe_request))

                    async for raw_msg in self._inner_messages(ws):
                        # Huobi compresses their ws data
                        encoded_msg: bytes = gzip.decompress(raw_msg)
                        # Huobi's data value for id is a large int too big for ujson to parse
                        msg: Dict[str, Any] = json.loads(
                            encoded_msg.decode('utf-8'))
                        if "ping" in msg:
                            await ws.send(
                                f'{{"op":"pong","ts": {str(msg["ping"])}}}')
                        elif "subbed" in msg:
                            pass
                        elif "ch" in msg:
                            trading_pair = msg["ch"].split(".")[1]
                            for data in msg["tick"]["data"]:
                                trade_message: OrderBookMessage = HuobiOrderBook.trade_message_from_exchange(
                                    data,
                                    metadata={"trading_pair": trading_pair})
                                output.put_nowait(trade_message)
                        else:
                            self.logger().debug(
                                f"Unrecognized message received from Huobi websocket: {msg}"
                            )
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().error(
                    "Unexpected error with WebSocket connection. Retrying after 30 seconds...",
                    exc_info=True)
                await asyncio.sleep(30.0)
 async def listen_for_order_book_snapshots(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue):
     while True:
         await self._sleep(self.ORDER_BOOK_SNAPSHOT_DELAY)
         try:
             for trading_pair in self._trading_pairs:
                 snapshot: Dict[str, Any] = await self.get_snapshot(trading_pair)
                 snapshot_message: OrderBookMessage = HuobiOrderBook.snapshot_message_from_exchange(
                     snapshot,
                     timestamp=snapshot["tick"]["ts"],
                     metadata={"trading_pair": trading_pair},
                 )
                 output.put_nowait(snapshot_message)
                 self.logger().debug(f"Saved order book snapshot for {trading_pair}")
         except asyncio.CancelledError:
             raise
         except Exception:
             self.logger().error("Unexpected error listening for orderbook snapshots. Retrying in 5 secs...", exc_info=True)
             await self._sleep(5.0)
 async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue):
     message_queue = self._message_queue[self.TRADE_CHANNEL_SUFFIX]
     while True:
         try:
             msg: Dict[str, Any] = await message_queue.get()
             trading_pair = msg["ch"].split(".")[1]
             timestamp = msg["tick"]["ts"]
             for data in msg["tick"]["data"]:
                 trade_message: OrderBookMessage = HuobiOrderBook.trade_message_from_exchange(
                     msg=data,
                     timestamp=timestamp,
                     metadata={"trading_pair": convert_from_exchange_trading_pair(trading_pair)}
                 )
                 output.put_nowait(trade_message)
         except asyncio.CancelledError:
             raise
         except Exception:
             self.logger().error("Unexpected error with WebSocket connection. Retrying after 30 seconds...",
                                 exc_info=True)
             await self._sleep(30.0)