MCPcopy Create free account
hub / github.com/TheSmallHanCat/flow2api / _stdlib_json_http_request

Function _stdlib_json_http_request

src/api/admin.py:290–331  ·  view source on GitHub ↗
(
    method: str,
    url: str,
    headers: Dict[str, str],
    payload: Optional[Dict[str, Any]],
    timeout: int,
)

Source from the content-addressed store, hash-verified

288
289
290async def _stdlib_json_http_request(
291 method: str,
292 url: str,
293 headers: Dict[str, str],
294 payload: Optional[Dict[str, Any]],
295 timeout: int,
296) -> tuple[int, Optional[Any], str]:
297 req_headers = dict(headers or {})
298 req_headers.setdefault("Accept", "application/json")
299 request_method = (method or "GET").upper()
300 request_data: Optional[bytes] = None
301
302 if payload is not None:
303 req_headers["Content-Type"] = "application/json; charset=utf-8"
304 if request_method != "GET":
305 request_data = json.dumps(payload).encode("utf-8")
306
307 def do_request() -> tuple[int, str]:
308 request = urllib.request.Request(
309 url=url,
310 data=request_data,
311 headers=req_headers,
312 method=request_method,
313 )
314 opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
315 try:
316 with opener.open(request, timeout=max(1.0, float(timeout))) as response:
317 status_code = int(getattr(response, "status", 0) or response.getcode() or 0)
318 body = response.read()
319 charset = response.headers.get_content_charset() or "utf-8"
320 return status_code, body.decode(charset, errors="replace")
321 except urllib.error.HTTPError as exc:
322 body = exc.read()
323 charset = exc.headers.get_content_charset() if exc.headers else None
324 return int(getattr(exc, "code", 0) or 0), body.decode(charset or "utf-8", errors="replace")
325
326 try:
327 status_code, text = await asyncio.to_thread(do_request)
328 except Exception as e:
329 raise RuntimeError(f"远程打码服务请求失败: {e}") from e
330
331 return status_code, _parse_json_response_text(text), text
332
333
334async def _sync_json_http_request(

Callers 1

_sync_json_http_requestFunction · 0.85

Calls 1

Tested by

no test coverage detected