(
method: str,
url: str,
headers: Dict[str, str],
payload: Optional[Dict[str, Any]],
timeout: int,
)
| 4320 | |
| 4321 | @staticmethod |
| 4322 | async def _stdlib_json_http_request( |
| 4323 | method: str, |
| 4324 | url: str, |
| 4325 | headers: Dict[str, str], |
| 4326 | payload: Optional[Dict[str, Any]], |
| 4327 | timeout: int, |
| 4328 | ) -> tuple[int, Optional[Any], str]: |
| 4329 | req_headers = dict(headers or {}) |
| 4330 | req_headers.setdefault("Accept", "application/json") |
| 4331 | request_method = (method or "GET").upper() |
| 4332 | request_data: Optional[bytes] = None |
| 4333 | |
| 4334 | if payload is not None: |
| 4335 | req_headers["Content-Type"] = "application/json; charset=utf-8" |
| 4336 | if request_method != "GET": |
| 4337 | request_data = json.dumps(payload).encode("utf-8") |
| 4338 | |
| 4339 | def do_request() -> tuple[int, str]: |
| 4340 | request = urllib.request.Request( |
| 4341 | url=url, |
| 4342 | data=request_data, |
| 4343 | headers=req_headers, |
| 4344 | method=request_method, |
| 4345 | ) |
| 4346 | opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) |
| 4347 | try: |
| 4348 | with opener.open(request, timeout=max(1.0, float(timeout))) as response: |
| 4349 | status_code = int(getattr(response, "status", 0) or response.getcode() or 0) |
| 4350 | body = response.read() |
| 4351 | charset = response.headers.get_content_charset() or "utf-8" |
| 4352 | return status_code, body.decode(charset, errors="replace") |
| 4353 | except urllib.error.HTTPError as exc: |
| 4354 | body = exc.read() |
| 4355 | charset = exc.headers.get_content_charset() if exc.headers else None |
| 4356 | return int(getattr(exc, "code", 0) or 0), body.decode(charset or "utf-8", errors="replace") |
| 4357 | |
| 4358 | try: |
| 4359 | status_code, text = await asyncio.to_thread(do_request) |
| 4360 | except Exception as e: |
| 4361 | raise RuntimeError(f"remote_browser 请求失败: {e}") from e |
| 4362 | |
| 4363 | return status_code, FlowClient._parse_json_response_text(text), text |
| 4364 | |
| 4365 | @staticmethod |
| 4366 | async def _sync_json_http_request( |
no test coverage detected