coby API
This commit is contained in:
@ -6,6 +6,7 @@ and comprehensive error handling.
|
||||
import asyncio
|
||||
import logging
|
||||
import json
|
||||
import websockets
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Dict, List, Optional, Callable, Any
|
||||
from datetime import datetime, timedelta
|
||||
@ -70,6 +71,10 @@ class BaseExchangeConnector(ExchangeConnector):
|
||||
self.error_count = 0
|
||||
self.last_message_time: Optional[datetime] = None
|
||||
|
||||
# Callbacks
|
||||
self.data_callbacks = []
|
||||
self.status_callbacks = []
|
||||
|
||||
# Setup callbacks
|
||||
self.connection_manager.on_connect = self._on_connect
|
||||
self.connection_manager.on_disconnect = self._on_disconnect
|
||||
@ -367,6 +372,38 @@ class BaseExchangeConnector(ExchangeConnector):
|
||||
"""Get order book snapshot - must be implemented by subclasses"""
|
||||
raise NotImplementedError("Subclasses must implement get_orderbook_snapshot")
|
||||
|
||||
# Callback methods
|
||||
def add_data_callback(self, callback):
|
||||
"""Add callback for data updates"""
|
||||
self.data_callbacks.append(callback)
|
||||
|
||||
def add_status_callback(self, callback):
|
||||
"""Add callback for status updates"""
|
||||
self.status_callbacks.append(callback)
|
||||
|
||||
async def _notify_data_callbacks(self, data):
|
||||
"""Notify all data callbacks"""
|
||||
for callback in self.data_callbacks:
|
||||
try:
|
||||
if hasattr(data, 'symbol'):
|
||||
# Determine data type
|
||||
if isinstance(data, OrderBookSnapshot):
|
||||
await callback('orderbook', data)
|
||||
elif isinstance(data, TradeEvent):
|
||||
await callback('trade', data)
|
||||
else:
|
||||
await callback('data', data)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in data callback: {e}")
|
||||
|
||||
def _notify_status_callbacks(self, status):
|
||||
"""Notify all status callbacks"""
|
||||
for callback in self.status_callbacks:
|
||||
try:
|
||||
callback(self.exchange_name, status)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in status callback: {e}")
|
||||
|
||||
# Utility methods
|
||||
def get_stats(self) -> Dict[str, Any]:
|
||||
"""Get connector statistics"""
|
||||
|
@ -6,11 +6,15 @@ import json
|
||||
from typing import Dict, List, Optional, Any
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from ..models.core import OrderBookSnapshot, TradeEvent, PriceLevel
|
||||
from ..utils.logging import get_logger, set_correlation_id
|
||||
from ..utils.exceptions import ValidationError
|
||||
from ..utils.validation import validate_symbol, validate_price, validate_volume
|
||||
from .base_connector import BaseExchangeConnector
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from models.core import OrderBookSnapshot, TradeEvent, PriceLevel
|
||||
from utils.logging import get_logger, set_correlation_id
|
||||
from utils.exceptions import ValidationError
|
||||
from utils.validation import validate_symbol, validate_price, validate_volume
|
||||
from connectors.base_connector import BaseExchangeConnector
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
Reference in New Issue
Block a user