Perform the outbound HTTP/HTTPS request and return a relay-JSON dict.
(
url: str, method: str, headers: dict[str, str], body: bytes
)
| 185 | |
| 186 | |
| 187 | def _relay_request( |
| 188 | url: str, method: str, headers: dict[str, str], body: bytes |
| 189 | ) -> dict: |
| 190 | """Perform the outbound HTTP/HTTPS request and return a relay-JSON dict.""" |
| 191 | request = urllib.request.Request(url, method=method, headers=headers) |
| 192 | if body: |
| 193 | request.data = body |
| 194 | |
| 195 | try: |
| 196 | with _NO_REDIRECT_OPENER.open(request, timeout=_OUTBOUND_TIMEOUT) as resp: |
| 197 | data = resp.read(_MAX_RESPONSE_BODY) |
| 198 | return { |
| 199 | "s": resp.status, |
| 200 | "h": _collect_headers(resp.headers), |
| 201 | "b": base64.b64encode(data).decode(), |
| 202 | } |
| 203 | except urllib.error.HTTPError as exc: |
| 204 | data = exc.read(_MAX_RESPONSE_BODY) if exc.fp else b"" |
| 205 | return { |
| 206 | "s": exc.code, |
| 207 | "h": _collect_headers(exc.headers) if exc.headers else {}, |
| 208 | "b": base64.b64encode(data).decode(), |
| 209 | } |
| 210 | |
| 211 | |
| 212 | # --------------------------------------------------------------------------- |
no test coverage detected