Files
easy-tdx/src/easy_tdx/web/deps.py
T
GitHubandClaude 46c5d3cc05 fix(web): guard MAC client None + filter _raw bytes from Ex responses
- get_mac_client() now raises TdxConnectionError (503) when MAC client
  is None, matching get_ex_client() behavior. Previously returned None
  causing AttributeError (500) on all 12 MAC endpoints.
- _records_to_df_resp() filters out internal _raw: bytes fields from
  Ex dataclass models. Previously asdict() included binary protocol
  data that is not JSON-serializable and would cause 500 errors.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-12 16:51:47 +08:00

36 lines
1.0 KiB
Python

"""Dependency injection for Web API routers."""
from __future__ import annotations
from typing import Any
from fastapi import Request
from easy_tdx.client import AsyncTdxClient
def get_client(request: Request) -> AsyncTdxClient:
"""从 app.state 获取共享的 AsyncTdxClient 实例。"""
client: AsyncTdxClient = request.app.state.tdx_client
return client
def get_mac_client(request: Request) -> Any:
"""从 app.state 获取共享的 AsyncMacClient 实例。"""
from easy_tdx.exceptions import TdxConnectionError
client: Any | None = request.app.state.mac_client
if client is None:
raise TdxConnectionError("MAC 客户端未连接")
return client
def get_ex_client(request: Request) -> Any:
"""从 app.state 获取共享的 AsyncExTdxClient 实例(可选)。"""
client: Any | None = request.app.state.ex_client
if client is None:
from easy_tdx.exceptions import TdxConnectionError
raise TdxConnectionError("扩展市场客户端未启用")
return client