Repository navigation
Expand file tree
/
Copy pathgithub_api.py
More file actions
87 lines (78 loc) · 3.35 KB
/
Copy pathgithub_api.py
File metadata and controls
87 lines (78 loc) · 3.35 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""GitHub JSON requests using Workers fetch in production and httpx locally."""
import asyncio
import json
import logging
from typing import Any
from urllib.parse import urlencode, urlsplit
logger = logging.getLogger(__name__)
def log_event(level, event, **fields):
record = {"event": event, **fields}
try:
from js import Object, console
from pyodide.ffi import to_js
except (ImportError, ModuleNotFoundError):
logger.log(level, json.dumps(record, separators=(",", ":")))
else:
emit = console.error if level >= logging.ERROR else console.warn if level >= logging.WARNING else console.log
emit(to_js(record, dict_converter=Object.fromEntries))
async def request_json(
url: str,
*,
headers: dict[str, str],
method: str = "GET",
body: dict[str, Any] | None = None,
form: dict[str, str] | None = None,
timeout: float = 15,
) -> tuple[int, dict[str, str], Any]:
"""Return status, lowercase response headers, and decoded JSON.
Cloudflare Python Workers currently drops the User-Agent header when
requests are sent through httpx. Its native JavaScript ``fetch`` API preserves
request headers. A CPython/httpx fallback keeps local development simple.
"""
try:
from js import AbortController, Object, fetch as js_fetch
from pyodide.ffi import to_js
except (ImportError, ModuleNotFoundError):
import httpx
async with httpx.AsyncClient(timeout=timeout) as client:
if form is not None:
response = await client.request(method, url, headers=headers, data=form)
else:
response = await client.request(method, url, headers=headers, json=body)
status = response.status_code
response_headers = {name.lower(): value for name, value in response.headers.items()}
text = response.text
else:
worker_headers = dict(headers)
controller = AbortController.new()
options: dict[str, Any] = {"method": method, "headers": worker_headers, "signal": controller.signal}
if form is not None:
worker_headers.setdefault("Content-Type", "application/x-www-form-urlencoded")
options["body"] = urlencode(form)
elif body is not None:
worker_headers.setdefault("Content-Type", "application/json")
options["body"] = json.dumps(body, separators=(",", ":"))
try:
response = await js_fetch(url, to_js(options, dict_converter=Object.fromEntries))
status = response.status
response_headers = {
name: response.headers.get(name, "")
for name in ("content-type", "server", "cf-ray", "x-github-request-id", "x-ratelimit-remaining")
}
text = await response.text()
except asyncio.CancelledError:
controller.abort()
raise
try:
payload = json.loads(text)
except (TypeError, ValueError):
payload = {"message": text[:1000]}
if status >= 400:
host = urlsplit(url).hostname
provider = {
"api.github.com": "github",
"api.linear.app": "linear",
"generativelanguage.googleapis.com": "gemini",
}.get(host, "other")
log_event(logging.ERROR, "upstream_http_error", provider=provider, http_status=status)
return status, response_headers, payload