(url: str, headers: dict[str, str], payload: dict[str, Any])
| 332 | |
| 333 | |
| 334 | def http_post_json(url: str, headers: dict[str, str], payload: dict[str, Any]) -> dict[str, Any]: |
| 335 | data = json.dumps(payload).encode("utf-8") |
| 336 | request = urllib.request.Request(url, data=data, method="POST") |
| 337 | request.add_header("Content-Type", "application/json") |
| 338 | request.add_header("User-Agent", USER_AGENT) |
| 339 | request.add_header("Accept", "application/json") |
| 340 | for key, value in headers.items(): |
| 341 | request.add_header(key, value) |
| 342 | try: |
| 343 | with urllib.request.urlopen(request, timeout=60) as response: |
| 344 | body = response.read().decode("utf-8") |
| 345 | except urllib.error.HTTPError as err: |
| 346 | detail = err.read().decode("utf-8", errors="replace") |
| 347 | raise RuntimeError(f"{url} returned HTTP {err.code}: {detail}") from err |
| 348 | if not body: |
| 349 | return {} |
| 350 | return json.loads(body) |
| 351 | |
| 352 | |
| 353 | def publish_to_devto(post: Post, body_markdown: str, api_key: str, draft: bool = False) -> dict[str, Any]: |
no test coverage detected