|
1 | 1 | """Definition for sync client for BotX API."""
|
2 | 2 | from dataclasses import field
|
3 | 3 | from http import HTTPStatus
|
| 4 | +from json import JSONDecodeError |
4 | 5 | from typing import Any, List, TypeVar
|
5 | 6 |
|
6 | 7 | import httpx
|
|
11 | 12 | from botx.clients.methods.base import BotXMethod, ErrorHandlersInMethod
|
12 | 13 | from botx.clients.types.http import HTTPRequest, HTTPResponse
|
13 | 14 | from botx.converters import optional_sequence_to_list
|
14 |
| -from botx.exceptions import BotXAPIError, BotXAPIRouteDeprecated |
| 15 | +from botx.exceptions import ( |
| 16 | + BotXAPIError, |
| 17 | + BotXAPIRouteDeprecated, |
| 18 | + BotXConnectError, |
| 19 | + BotXJSONDecodeError, |
| 20 | +) |
15 | 21 | from botx.shared import BotXDataclassConfig
|
16 | 22 |
|
17 | 23 | ResponseT = TypeVar("ResponseT")
|
@@ -90,18 +96,33 @@ def execute(self, request: HTTPRequest) -> HTTPResponse:
|
90 | 96 |
|
91 | 97 | Returns:
|
92 | 98 | HTTP response from API.
|
| 99 | +
|
| 100 | + Raises: |
| 101 | + BotXConnectError: raised if unable to connect to service. |
| 102 | + BotXJSONDecodeError: raised if service returned invalid body. |
93 | 103 | """
|
94 |
| - response = self.http_client.request( |
95 |
| - request.method, |
96 |
| - request.url, |
97 |
| - headers=request.headers, |
98 |
| - params=request.query_params, |
99 |
| - json=request.json_body, |
100 |
| - ) |
| 104 | + try: |
| 105 | + response = self.http_client.request( |
| 106 | + request.method, |
| 107 | + request.url, |
| 108 | + headers=request.headers, |
| 109 | + params=request.query_params, |
| 110 | + json=request.json_body, |
| 111 | + ) |
| 112 | + except httpx.HTTPError as httpx_exc: |
| 113 | + raise BotXConnectError( |
| 114 | + url=request.url, |
| 115 | + method=request.method, |
| 116 | + ) from httpx_exc |
| 117 | + |
| 118 | + try: |
| 119 | + json_body = response.json() |
| 120 | + except JSONDecodeError as exc: |
| 121 | + raise BotXJSONDecodeError(url=request.url, method=request.method) from exc |
101 | 122 |
|
102 | 123 | return HTTPResponse(
|
103 | 124 | status_code=response.status_code,
|
104 |
| - json_body=response.json(), |
| 125 | + json_body=json_body, |
105 | 126 | )
|
106 | 127 |
|
107 | 128 |
|
|
0 commit comments