|
7 | 7 |
|
8 | 8 |
|
9 | 9 | def make_request(method: Method, url: str, **kwargs) -> Any: |
10 | | - response_body = None |
11 | 10 | try: |
12 | | - r = requests.request(method.value, url=url, **kwargs) |
13 | | - r.raise_for_status() |
14 | | - response_body = r.json() |
15 | | - except requests.exceptions.RequestException as e: |
16 | | - exception_detail = r.json() |
| 11 | + response = requests.request(method.value, url=url, **kwargs) |
| 12 | + response.raise_for_status() |
| 13 | + except requests.exceptions.RequestException as err: |
| 14 | + response = err.response |
| 15 | + if not err.response: |
| 16 | + # Connection errors, timepouts, etc... |
| 17 | + raise BugoutResponseException("Network error", detail=str(err)) |
| 18 | + if response.headers.get("Content-Type") == "application/json": |
| 19 | + exception_detail = response.json()["detail"] |
| 20 | + else: |
| 21 | + exception_detail = response.text |
17 | 22 | raise BugoutResponseException( |
18 | 23 | "An exception occurred at Bugout API side", |
19 | | - status_code=r.status_code, |
20 | | - detail=exception_detail["detail"] |
21 | | - if exception_detail["detail"] is not None |
22 | | - else None, |
| 24 | + status_code=response.status_code, |
| 25 | + detail=exception_detail, |
23 | 26 | ) |
24 | 27 | except Exception as e: |
25 | | - raise BugoutUnexpectedResponse(f"{str(e)}") |
26 | | - return response_body |
| 28 | + raise BugoutUnexpectedResponse({str(e)}) |
| 29 | + return response.json() |
27 | 30 |
|
28 | 31 |
|
29 | 32 | def ping(url: str) -> Dict[str, Any]: |
|
0 commit comments