(
self,
account: AccountRecord,
session: AccountSessionState,
method: str,
path: str,
*,
json_body: Any | None = None,
params: dict[str, Any] | None = None,
allow_fallback_proxy: bool = False,
)
| 142 | def _request( |
| 143 | self, |
| 144 | account: AccountRecord, |
| 145 | session: AccountSessionState, |
| 146 | method: str, |
| 147 | path: str, |
| 148 | *, |
| 149 | json_body: Any | None = None, |
| 150 | params: dict[str, Any] | None = None, |
| 151 | ) -> ApiCallResult: |
| 152 | token = account.token.strip() or account.cookies.get("bigmodel_token_production", "").strip() |
| 153 | if not token: |
| 154 | raise BadRequestError("账号缺少 token,没法请求 BigModel") |
| 155 | |
| 156 | headers = { |
| 157 | "Authorization": token, |
| 158 | "Bigmodel-Organization": session.org_id or account.org_id, |
| 159 | "Bigmodel-Project": session.project_id or account.project_id, |
| 160 | } |
| 161 | headers = {key: value for key, value in headers.items() if value} |
| 162 | payload = self.http_client.request_json( |
| 163 | method, |
| 164 | self._build_url(path), |
| 165 | headers=headers, |
| 166 | cookies=account.cookies, |
| 167 | json_body=json_body, |
| 168 | params=params, |
| 169 | proxy_url=account.proxy_url or None, |
| 170 | user_agent=resolve_user_agent(account.user_agent, account.browser_impersonate), |
| 171 | browser_impersonate=account.browser_impersonate or None, |
| 172 | ) |
| 173 | if not isinstance(payload, dict): |
| 174 | raise UpstreamRequestError( |
| 175 | "上游返回结构异常", |
| 176 | details={"path": path, "payload_type": type(payload).__name__}, |
| 177 | ) |
| 178 | |
| 179 | code = payload.get("code") |
| 180 | if code not in (None, 0, 200): |
| 181 | # preview 555 (系统繁忙) 不抛异常,让上层循环重试 |
| 182 | if code == 555 and path.endswith("/preview"): |
| 183 | return ApiCallResult(data=payload.get("data"), raw=payload) |
| 184 | raise UpstreamRequestError( |
| 185 | str(payload.get("msg") or payload.get("message") or "上游业务返回失败"), |
| 186 | details={"path": path, "payload": payload}, |
| 187 | ) |
| 188 | |
| 189 | if "data" in payload: |
| 190 | return ApiCallResult(data=payload.get("data"), raw=payload) |
| 191 | if "result" in payload: |
| 192 | return ApiCallResult(data=payload.get("result"), raw=payload) |
| 193 | return ApiCallResult(data=payload, raw=payload) |
| 194 | |
| 195 | def _build_url(self, path: str) -> str: |
| 196 | return f"{self.settings.bigmodel_api_base}/{path.lstrip('/')}" |
| 197 | |
| 198 | |
| 199 | @lru_cache(maxsize=1) |
| 200 | def get_bigmodel_client() -> BigModelClient: |
no test coverage detected