|
4 | 4 |
|
5 | 5 | import os |
6 | 6 | from collections.abc import Mapping |
7 | | -from typing import Any, Generic, Literal, Optional, TypedDict, TypeVar, cast |
| 7 | +from typing import Any, Generic, Literal, TypedDict, TypeVar, Union, cast |
8 | 8 |
|
9 | 9 | import httpx |
10 | 10 | from httpx import URL |
11 | 11 | from pydantic import BaseModel, ConfigDict, Field, ValidationError, field_validator |
| 12 | +from typing_extensions import TypeAlias |
12 | 13 |
|
13 | 14 | from .exceptions import ( |
14 | 15 | PdfRestApiError, |
|
23 | 24 | API_KEY_ENV_VAR = "PDFREST_API_KEY" |
24 | 25 | DEFAULT_TIMEOUT_SECONDS = 10.0 |
25 | 26 |
|
26 | | -HttpMethod = Literal["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"] |
27 | | -QueryParamValue = str | int | float | bool | None |
28 | | -TimeoutTypes = float | httpx.Timeout | None |
| 27 | +HttpMethod: TypeAlias = Literal[ |
| 28 | + "GET", |
| 29 | + "POST", |
| 30 | + "PUT", |
| 31 | + "PATCH", |
| 32 | + "DELETE", |
| 33 | + "OPTIONS", |
| 34 | + "HEAD", |
| 35 | +] |
| 36 | +QueryParamValue: TypeAlias = Union[str, int, float, bool, None] |
| 37 | +TimeoutTypes: TypeAlias = Union[float, httpx.Timeout, None] |
29 | 38 |
|
30 | 39 |
|
31 | 40 | ClientType = TypeVar("ClientType", httpx.Client, httpx.AsyncClient) |
@@ -154,7 +163,7 @@ def __init__( |
154 | 163 | timeout: TimeoutTypes = DEFAULT_TIMEOUT_SECONDS, |
155 | 164 | headers: Mapping[str, str] | None = None, |
156 | 165 | ) -> None: |
157 | | - resolved_api_key = (api_key or os.getenv(API_KEY_ENV_VAR, "")).strip() |
| 166 | + resolved_api_key = (api_key or os.getenv(API_KEY_ENV_VAR) or "").strip() |
158 | 167 | if not resolved_api_key: |
159 | 168 | msg = "API key was not provided and the PDFREST_API_KEY environment variable is not set." |
160 | 169 | raise PdfRestConfigurationError(msg) |
@@ -233,19 +242,28 @@ def _prepare_request( |
233 | 242 | raise PdfRestConfigurationError(str(exc)) from exc |
234 | 243 | return request |
235 | 244 |
|
| 245 | + def _message_from_payload(self, payload: Any) -> str: |
| 246 | + if isinstance(payload, str): |
| 247 | + return payload |
| 248 | + if isinstance(payload, Mapping): |
| 249 | + m = cast(Mapping[str, object], payload) |
| 250 | + candidate = m.get("error", m.get("message")) |
| 251 | + if isinstance(candidate, str): |
| 252 | + return candidate |
| 253 | + return "pdfRest returned an error" |
| 254 | + |
236 | 255 | def _handle_response(self, response: httpx.Response) -> Any: |
237 | 256 | if response.is_success: |
238 | 257 | return self._decode_json(response) |
239 | 258 | error_payload: Any = None |
240 | | - message: str | None = None |
241 | 259 | try: |
242 | 260 | error_payload = response.json() |
243 | | - if isinstance(error_payload, Mapping): |
244 | | - message = cast(Optional[str], error_payload.get("message")) |
245 | 261 | except ValueError: |
246 | 262 | error_payload = response.text |
247 | 263 | raise PdfRestApiError( |
248 | | - response.status_code, message=message, response_content=error_payload |
| 264 | + response.status_code, |
| 265 | + message=self._message_from_payload(error_payload), |
| 266 | + response_content=error_payload, |
249 | 267 | ) |
250 | 268 |
|
251 | 269 | def _decode_json(self, response: httpx.Response) -> Any: |
|
0 commit comments