POST 请求 agent-im OpenAPI
(path: str, body: dict)
| 33 | |
| 34 | |
| 35 | def api_post(path: str, body: dict) -> dict: |
| 36 | """POST 请求 agent-im OpenAPI""" |
| 37 | url = f"{IM_BASE.rstrip('/')}{path}" |
| 38 | data = json.dumps(body).encode("utf-8") |
| 39 | req = urllib.request.Request( |
| 40 | url, |
| 41 | data=data, |
| 42 | method="POST", |
| 43 | headers=_headers(), |
| 44 | ) |
| 45 | try: |
| 46 | with urllib.request.urlopen(req, timeout=30) as resp: |
| 47 | return json.loads(resp.read().decode("utf-8")) |
| 48 | except urllib.error.HTTPError as e: |
| 49 | err_body = e.read().decode("utf-8") if e.fp else "" |
| 50 | print(f"API 错误 {e.code}: {err_body}", file=sys.stderr) |
| 51 | sys.exit(1) |
| 52 | except urllib.error.URLError as e: |
| 53 | print(f"网络错误: {e.reason}", file=sys.stderr) |
| 54 | sys.exit(1) |
| 55 | |
| 56 | |
| 57 | def api_get(path: str) -> dict: |
no test coverage detected