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>
This commit is contained in:
GitHub
2026-06-12 16:51:47 +08:00
co-authored by Claude
parent d17895e92b
commit 46c5d3cc05
2 changed files with 8 additions and 3 deletions
+5 -1
View File
@@ -17,7 +17,11 @@ def get_client(request: Request) -> AsyncTdxClient:
def get_mac_client(request: Request) -> Any:
"""从 app.state 获取共享的 AsyncMacClient 实例。"""
client: Any = request.app.state.mac_client
from easy_tdx.exceptions import TdxConnectionError
client: Any | None = request.app.state.mac_client
if client is None:
raise TdxConnectionError("MAC 客户端未连接")
return client
+3 -2
View File
@@ -15,12 +15,13 @@ router = APIRouter(tags=["ex-market"])
def _records_to_df_resp(records: list[Any]) -> DataFrameResponse:
"""将 dataclass 列表转为 DataFrameResponse。"""
"""将 dataclass 列表转为 DataFrameResponse(过滤内部 _raw 字段)"""
import pandas as pd
if not records:
return DataFrameResponse(data=[], count=0)
df = pd.DataFrame([asdict(r) for r in records])
rows = [{k: v for k, v in asdict(r).items() if not k.startswith("_")} for r in records]
df = pd.DataFrame(rows)
return DataFrameResponse.from_dataframe(df)