forked from Neoteroi/essentials-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
32 lines (23 loc) · 883 Bytes
/
web.py
File metadata and controls
32 lines (23 loc) · 883 Bytes
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
from typing import Any
import httpx
http_client = httpx.Client(verify=False, timeout=20)
class FailedRequestError(Exception):
def __init__(self, message: str) -> None:
super().__init__(
f"Failed request: {message}. "
"Inspect the inner exception (__context__) for more information."
)
@property
def inner_exception(self):
return self.__context__
def ensure_success(response: httpx.Response) -> None:
if response.status_code < 200 or response.status_code > 399:
raise FailedRequestError(
"Response status does not indicate success: "
f"{response.status_code} {response.reason_phrase}"
)
def http_get(url: str) -> Any:
try:
return http_client.get(url)
except httpx.HTTPError as http_error:
raise FailedRequestError(str(http_error)) from http_error