(
method: str,
url: str,
token: str,
body: bytes | None = None,
headers: dict[str, str] | None = None,
)
| 29 | |
| 30 | |
| 31 | def api_request( |
| 32 | method: str, |
| 33 | url: str, |
| 34 | token: str, |
| 35 | body: bytes | None = None, |
| 36 | headers: dict[str, str] | None = None, |
| 37 | ) -> Any: |
| 38 | req = request.Request( |
| 39 | url=url, |
| 40 | method=method, |
| 41 | data=body, |
| 42 | headers=build_headers(token, headers), |
| 43 | ) |
| 44 | try: |
| 45 | with request.urlopen(req) as resp: |
| 46 | raw = resp.read() |
| 47 | content_type = resp.headers.get("Content-Type", "") |
| 48 | if "application/json" in content_type: |
| 49 | return json.loads(raw.decode("utf-8")) |
| 50 | return {"status": resp.status, "body": raw.decode("utf-8")} |
| 51 | except error.HTTPError as exc: |
| 52 | raw = exc.read().decode("utf-8", errors="replace") |
| 53 | try: |
| 54 | payload = json.loads(raw) |
| 55 | except json.JSONDecodeError: |
| 56 | payload = {"message": raw} |
| 57 | payload["_http_status"] = exc.code |
| 58 | raise SystemExit( |
| 59 | f"Box API error {exc.code}:\n{json.dumps(payload, indent=2, sort_keys=True)}" |
| 60 | ) |
| 61 | |
| 62 | |
| 63 | def dump_json(payload: Any) -> None: |
no test coverage detected