Skip to content

Commit ffde7f9

Browse files
blank-pdf: Set default args
Assisted-by: Codex
1 parent f30b5a3 commit ffde7f9

3 files changed

Lines changed: 111 additions & 28 deletions

File tree

src/pdfrest/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3077,16 +3077,16 @@ def add_attachment_to_pdf(
30773077
def blank_pdf(
30783078
self,
30793079
*,
3080-
page_size: PdfPageSize,
3081-
page_count: int,
3080+
page_size: PdfPageSize = "letter",
3081+
page_count: int = 1,
30823082
page_orientation: PdfPageOrientation | None = None,
30833083
output: str | None = None,
30843084
extra_query: Query | None = None,
30853085
extra_headers: AnyMapping | None = None,
30863086
extra_body: Body | None = None,
30873087
timeout: TimeoutTypes | None = None,
30883088
) -> PdfRestFileBasedResponse:
3089-
"""Create a blank PDF with the specified size, count, and orientation."""
3089+
"""Create a blank PDF with configurable size, count, and orientation."""
30903090

30913091
payload: dict[str, Any] = {
30923092
"page_size": page_size,
@@ -4516,16 +4516,16 @@ async def add_attachment_to_pdf(
45164516
async def blank_pdf(
45174517
self,
45184518
*,
4519-
page_size: PdfPageSize,
4520-
page_count: int,
4519+
page_size: PdfPageSize = "letter",
4520+
page_count: int = 1,
45214521
page_orientation: PdfPageOrientation | None = None,
45224522
output: str | None = None,
45234523
extra_query: Query | None = None,
45244524
extra_headers: AnyMapping | None = None,
45254525
extra_body: Body | None = None,
45264526
timeout: TimeoutTypes | None = None,
45274527
) -> PdfRestFileBasedResponse:
4528-
"""Asynchronously create a blank PDF with the specified size."""
4528+
"""Asynchronously create a blank PDF with configurable size and count."""
45294529

45304530
payload: dict[str, Any] = {
45314531
"page_size": page_size,

src/pdfrest/models/_internal.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,25 +1402,29 @@ def _normalize_custom_page_size(cls, data: Any) -> Any:
14021402
if not isinstance(data, Mapping):
14031403
return data
14041404

1405-
request_data = cast(Mapping[str, Any], data)
1406-
page_size = request_data.get("page_size")
1407-
if not isinstance(page_size, Sequence) or isinstance(
1405+
normalized_data: dict[str, Any] = dict(cast(Mapping[str, Any], data))
1406+
page_size = normalized_data.get("page_size")
1407+
if isinstance(page_size, Sequence) and not isinstance(
14081408
page_size, (str, bytes, bytearray)
14091409
):
1410-
return request_data
1411-
1412-
custom_dimensions = list(page_size)
1413-
if len(custom_dimensions) != 2:
1414-
msg = (
1415-
"Custom page sizes must contain exactly two values: "
1416-
"custom_height and custom_width."
1417-
)
1418-
raise ValueError(msg)
1410+
custom_dimensions = list(page_size)
1411+
if len(custom_dimensions) != 2:
1412+
msg = (
1413+
"Custom page sizes must contain exactly two values: "
1414+
"custom_height and custom_width."
1415+
)
1416+
raise ValueError(msg)
1417+
normalized_data["page_size"] = "custom"
1418+
normalized_data["custom_height"] = custom_dimensions[0]
1419+
normalized_data["custom_width"] = custom_dimensions[1]
1420+
1421+
if (
1422+
normalized_data.get("page_size") is not None
1423+
and normalized_data.get("page_size") != "custom"
1424+
and normalized_data.get("page_orientation") is None
1425+
):
1426+
normalized_data["page_orientation"] = "portrait"
14191427

1420-
normalized_data: dict[str, Any] = dict(request_data)
1421-
normalized_data["page_size"] = "custom"
1422-
normalized_data["custom_height"] = custom_dimensions[0]
1423-
normalized_data["custom_width"] = custom_dimensions[1]
14241428
return normalized_data
14251429

14261430
@model_validator(mode="after")

tests/test_blank_pdf.py

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,91 @@ def handler(request: httpx.Request) -> httpx.Response:
150150
assert response.output_file.type == "application/pdf"
151151

152152

153+
def test_blank_pdf_defaults(monkeypatch: pytest.MonkeyPatch) -> None:
154+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
155+
output_id = str(PdfRestFileID.generate())
156+
157+
payload_dump = PdfBlankPayload.model_validate(
158+
{
159+
"page_size": "letter",
160+
"page_count": 1,
161+
"page_orientation": "portrait",
162+
}
163+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
164+
165+
seen: dict[str, int] = {"post": 0, "get": 0}
166+
167+
def handler(request: httpx.Request) -> httpx.Response:
168+
if request.method == "POST" and request.url.path == "/blank-pdf":
169+
seen["post"] += 1
170+
payload = json.loads(request.content.decode("utf-8"))
171+
assert payload == payload_dump
172+
return httpx.Response(200, json={"outputId": [output_id]})
173+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
174+
seen["get"] += 1
175+
return httpx.Response(
176+
200,
177+
json=build_file_info_payload(
178+
output_id,
179+
"blank-default.pdf",
180+
"application/pdf",
181+
),
182+
)
183+
msg = f"Unexpected request {request.method} {request.url}"
184+
raise AssertionError(msg)
185+
186+
transport = httpx.MockTransport(handler)
187+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
188+
response = client.blank_pdf()
189+
190+
assert seen == {"post": 1, "get": 1}
191+
assert isinstance(response, PdfRestFileBasedResponse)
192+
assert response.output_file.type == "application/pdf"
193+
194+
195+
@pytest.mark.asyncio
196+
async def test_async_blank_pdf_defaults(monkeypatch: pytest.MonkeyPatch) -> None:
197+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
198+
output_id = str(PdfRestFileID.generate())
199+
200+
payload_dump = PdfBlankPayload.model_validate(
201+
{
202+
"page_size": "letter",
203+
"page_count": 1,
204+
"page_orientation": "portrait",
205+
}
206+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
207+
208+
seen: dict[str, int] = {"post": 0, "get": 0}
209+
210+
def handler(request: httpx.Request) -> httpx.Response:
211+
if request.method == "POST" and request.url.path == "/blank-pdf":
212+
seen["post"] += 1
213+
payload = json.loads(request.content.decode("utf-8"))
214+
assert payload == payload_dump
215+
return httpx.Response(200, json={"outputId": [output_id]})
216+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
217+
seen["get"] += 1
218+
return httpx.Response(
219+
200,
220+
json=build_file_info_payload(
221+
output_id,
222+
"blank-async-default.pdf",
223+
"application/pdf",
224+
),
225+
)
226+
msg = f"Unexpected request {request.method} {request.url}"
227+
raise AssertionError(msg)
228+
229+
transport = httpx.MockTransport(handler)
230+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
231+
response = await client.blank_pdf()
232+
233+
assert seen == {"post": 1, "get": 1}
234+
assert isinstance(response, PdfRestFileBasedResponse)
235+
assert response.output_file.type == "application/pdf"
236+
237+
153238
@pytest.mark.parametrize(
154239
"page_count",
155240
[
@@ -597,12 +682,6 @@ def test_blank_pdf_validation(monkeypatch: pytest.MonkeyPatch) -> None:
597682
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
598683
transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError))
599684

600-
with (
601-
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
602-
pytest.raises(ValueError, match="page_orientation is required"),
603-
):
604-
client.blank_pdf(page_size="letter", page_count=1)
605-
606685
with (
607686
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
608687
pytest.raises(ValueError, match="Custom page sizes must contain exactly two"),

0 commit comments

Comments
 (0)