from __future__ import annotations import json from typing import Any from urllib import error, request class ProviderHTTPError(RuntimeError): def __init__(self, status_code: int, message: str): super().__init__(f"HTTP {status_code}: {message}") self.status_code = status_code self.message = message def post_json( *, url: str, payload: dict[str, Any], headers: dict[str, str], timeout_seconds: int, ) -> dict[str, Any]: body = json.dumps(payload).encode("utf-8") req = request.Request( url, data=body, method="POST", headers={**headers, "Content-Type": "application/json"}, ) try: with request.urlopen(req, timeout=timeout_seconds) as resp: raw = resp.read().decode("utf-8", errors="replace") return json.loads(raw) except error.HTTPError as exc: text = exc.read().decode("utf-8", errors="replace") raise ProviderHTTPError(exc.code, text[:500]) from exc