跳转至

cryptoservice.client

cryptoservice.client

Classes

BinanceClientFactory

Binance客户端工厂类.

Functions
create_client(api_key: str, api_secret: str) -> Client classmethod

创建或获取Binance客户端实例(单例模式)

PARAMETER DESCRIPTION
api_key

API密钥

TYPE: str

api_secret

API密钥对应的secret

TYPE: str

RETURNS DESCRIPTION
Client

Binance客户端实例

TYPE: Client

RAISES DESCRIPTION
MarketDataError

当客户端初始化失败时抛出

Source code in src/cryptoservice/client/client.py
@classmethod
def create_client(cls, api_key: str, api_secret: str) -> Client:
    """创建或获取Binance客户端实例(单例模式)

    Args:
        api_key: API密钥
        api_secret: API密钥对应的secret

    Returns:
        Client: Binance客户端实例

    Raises:
        MarketDataError: 当客户端初始化失败时抛出
    """
    if not cls._instance:
        try:
            if not api_key or not api_secret:
                raise ValueError("Missing Binance API credentials")
            cls._instance = Client(api_key, api_secret)
            logger.info("[green]Successfully created Binance client[/green]")
        except Exception as e:
            logger.error(f"[red]Failed to initialize Binance client: {e}[/red]")
            raise MarketDataError(f"Failed to initialize Binance client: {e}") from e
    return cls._instance
get_client() -> Client | None classmethod

获取现有的客户端实例.

Source code in src/cryptoservice/client/client.py
@classmethod
def get_client(cls) -> Client | None:
    """获取现有的客户端实例."""
    return cls._instance
reset_client() -> None classmethod

重置客户端实例.

Source code in src/cryptoservice/client/client.py
@classmethod
def reset_client(cls) -> None:
    """重置客户端实例."""
    cls._instance = None