-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.py
More file actions
64 lines (48 loc) · 1.72 KB
/
http_client.py
File metadata and controls
64 lines (48 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
from typing import Any
import requests
from config import get_timeout
def build_headers(api_key: str, json_request: bool = False) -> dict[str, str]:
headers = {
"X-API-KEY": api_key,
"Accept": "application/json",
}
if json_request:
headers["Content-Type"] = "application/json"
return headers
def parse_json_response(response: requests.Response) -> Any:
try:
return response.json()
except ValueError as exc:
raise RuntimeError(f"Non-JSON response received: {response.text}") from exc
def get_json(url: str, api_key: str) -> Any:
try:
response = requests.get(
url,
headers=build_headers(api_key),
timeout=get_timeout(),
)
except requests.RequestException as exc:
raise RuntimeError(f"Network error during GET {url}: {exc}") from exc
data = parse_json_response(response)
if not response.ok:
raise RuntimeError(
f"GET {url} failed (status={response.status_code}) (body={json.dumps(data)})"
)
return data
def post_json(url: str, api_key: str, payload: dict[str, Any]) -> Any:
try:
response = requests.post(
url,
headers=build_headers(api_key, json_request=True),
json=payload,
timeout=get_timeout(),
)
except requests.RequestException as exc:
raise RuntimeError(f"Network error during POST {url}: {exc}") from exc
data = parse_json_response(response)
if not response.ok:
raise RuntimeError(
f"POST {url} failed (status={response.status_code}) (body={json.dumps(data)})"
)
return data