(
base_url: str,
path: str,
*,
method: str = 'GET',
headers: dict[str, str] | None = None,
body: dict[str, Any] | None = None,
)
| 95 | |
| 96 | |
| 97 | def request_json( |
| 98 | base_url: str, |
| 99 | path: str, |
| 100 | *, |
| 101 | method: str = 'GET', |
| 102 | headers: dict[str, str] | None = None, |
| 103 | body: dict[str, Any] | None = None, |
| 104 | ) -> Any: |
| 105 | data = json.dumps(body).encode('utf-8') if body is not None else None |
| 106 | request = urllib.request.Request( |
| 107 | f'{base_url}{path}', |
| 108 | data=data, |
| 109 | headers=headers or {}, |
| 110 | method=method, |
| 111 | ) |
| 112 | try: |
| 113 | with urllib.request.urlopen(request, timeout=60) as response: |
| 114 | return json.load(response) |
| 115 | except urllib.error.HTTPError as exc: |
| 116 | error_body = exc.read().decode('utf-8', errors='replace') |
| 117 | raise RuntimeError( |
| 118 | f'{method} {base_url}{path} failed with HTTP {exc.code}: {error_body}' |
| 119 | ) from exc |
| 120 | except json.JSONDecodeError as exc: |
| 121 | raise RuntimeError( |
| 122 | f'Failed to parse JSON from {method} {base_url}{path}: {exc}' |
| 123 | ) from exc |
| 124 | except urllib.error.URLError as exc: |
| 125 | raise RuntimeError(f'{method} {base_url}{path} failed: {exc}') from exc |
| 126 | |
| 127 | |
| 128 | def fetch_issue(repository: str, issue_number: int) -> dict[str, Any]: |
no test coverage detected