mirror of
https://ghfast.top/https://github.com/aeroxw/easy_tdx_max.git
synced 2026-09-12 16:54:20 +08:00
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:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user