Skip to content

Commit 516b347

Browse files
wip use type aliases, tighten up typing
1 parent bb660d1 commit 516b347

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ files = "src"
6060
[tool.pyright]
6161
venvPath = "."
6262
venv = ".venv"
63+
pythonVersion = "3.9"
64+
typeCheckingMode = "strict"
6365

6466
[tool.pytest.ini_options]
6567
minversion = "7.4"

src/pdfrest/client.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import os
66
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
88

99
import httpx
1010
from httpx import URL
1111
from pydantic import BaseModel, ConfigDict, Field, ValidationError, field_validator
12+
from typing_extensions import TypeAlias
1213

1314
from .exceptions import (
1415
PdfRestApiError,
@@ -23,9 +24,17 @@
2324
API_KEY_ENV_VAR = "PDFREST_API_KEY"
2425
DEFAULT_TIMEOUT_SECONDS = 10.0
2526

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]
2938

3039

3140
ClientType = TypeVar("ClientType", httpx.Client, httpx.AsyncClient)
@@ -154,7 +163,7 @@ def __init__(
154163
timeout: TimeoutTypes = DEFAULT_TIMEOUT_SECONDS,
155164
headers: Mapping[str, str] | None = None,
156165
) -> 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()
158167
if not resolved_api_key:
159168
msg = "API key was not provided and the PDFREST_API_KEY environment variable is not set."
160169
raise PdfRestConfigurationError(msg)
@@ -233,19 +242,28 @@ def _prepare_request(
233242
raise PdfRestConfigurationError(str(exc)) from exc
234243
return request
235244

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+
236255
def _handle_response(self, response: httpx.Response) -> Any:
237256
if response.is_success:
238257
return self._decode_json(response)
239258
error_payload: Any = None
240-
message: str | None = None
241259
try:
242260
error_payload = response.json()
243-
if isinstance(error_payload, Mapping):
244-
message = cast(Optional[str], error_payload.get("message"))
245261
except ValueError:
246262
error_payload = response.text
247263
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,
249267
)
250268

251269
def _decode_json(self, response: httpx.Response) -> Any:

0 commit comments

Comments
 (0)