Skip to content

Commit 9ffda88

Browse files
client: scope input-id fallback to blank-pdf only
- Restrict response input-id fallback logic to the /blank-pdf endpoint in both sync and async file-operation paths. - Add sync and async regression tests for /compressed-pdf confirming that missing inputId no longer falls back to outputId and still raises validation errors for non-blank endpoints. Assisted-by: Codex
1 parent a1e34a1 commit 9ffda88

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

src/pdfrest/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,9 @@ def _post_file_operation(
10321032
for file_id in output_ids
10331033
]
10341034

1035-
input_ids = raw_response.input_id or (raw_response.ids or [])
1035+
input_ids = raw_response.input_id
1036+
if not input_ids and endpoint == "/blank-pdf":
1037+
input_ids = raw_response.ids or []
10361038
response_payload: dict[str, Any] = {
10371039
"input_id": [str(file_id) for file_id in input_ids],
10381040
"output_file": [
@@ -1308,7 +1310,9 @@ async def throttled_fetch_file_info(file_id: str) -> PdfRestFile:
13081310
)
13091311
)
13101312

1311-
input_ids = raw_response.input_id or (raw_response.ids or [])
1313+
input_ids = raw_response.input_id
1314+
if not input_ids and endpoint == "/blank-pdf":
1315+
input_ids = raw_response.ids or []
13121316
response_payload: dict[str, Any] = {
13131317
"input_id": [str(file_id) for file_id in input_ids],
13141318
"output_file": [

tests/test_compress_pdf.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,42 @@ def handler(request: httpx.Request) -> httpx.Response:
7171
assert str(response.input_id) == str(input_file.id)
7272

7373

74+
def test_compress_pdf_requires_input_id_in_response(
75+
monkeypatch: pytest.MonkeyPatch,
76+
) -> None:
77+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
78+
input_file = make_pdf_file(PdfRestFileID.generate(1))
79+
output_id = str(PdfRestFileID.generate())
80+
81+
seen: dict[str, int] = {"post": 0, "get": 0}
82+
83+
def handler(request: httpx.Request) -> httpx.Response:
84+
if request.method == "POST" and request.url.path == "/compressed-pdf":
85+
seen["post"] += 1
86+
return httpx.Response(200, json={"outputId": [output_id]})
87+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
88+
seen["get"] += 1
89+
return httpx.Response(
90+
200,
91+
json=build_file_info_payload(
92+
output_id,
93+
"compressed.pdf",
94+
"application/pdf",
95+
),
96+
)
97+
msg = f"Unexpected request {request.method} {request.url}"
98+
raise AssertionError(msg)
99+
100+
transport = httpx.MockTransport(handler)
101+
with (
102+
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
103+
pytest.raises(ValidationError, match="at least 1 item"),
104+
):
105+
client.compress_pdf(input_file, compression_level="low")
106+
107+
assert seen == {"post": 1, "get": 1}
108+
109+
74110
def test_compress_pdf_custom_profile(monkeypatch: pytest.MonkeyPatch) -> None:
75111
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
76112
input_file = make_pdf_file(PdfRestFileID.generate(1))
@@ -300,6 +336,41 @@ def handler(request: httpx.Request) -> httpx.Response:
300336
assert response.output_file.name == "async.pdf"
301337

302338

339+
@pytest.mark.asyncio
340+
async def test_async_compress_pdf_requires_input_id_in_response(
341+
monkeypatch: pytest.MonkeyPatch,
342+
) -> None:
343+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
344+
input_file = make_pdf_file(PdfRestFileID.generate(2))
345+
output_id = str(PdfRestFileID.generate())
346+
347+
seen: dict[str, int] = {"post": 0, "get": 0}
348+
349+
def handler(request: httpx.Request) -> httpx.Response:
350+
if request.method == "POST" and request.url.path == "/compressed-pdf":
351+
seen["post"] += 1
352+
return httpx.Response(200, json={"outputId": [output_id]})
353+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
354+
seen["get"] += 1
355+
return httpx.Response(
356+
200,
357+
json=build_file_info_payload(
358+
output_id,
359+
"compressed.pdf",
360+
"application/pdf",
361+
),
362+
)
363+
msg = f"Unexpected request {request.method} {request.url}"
364+
raise AssertionError(msg)
365+
366+
transport = httpx.MockTransport(handler)
367+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
368+
with pytest.raises(ValidationError, match="at least 1 item"):
369+
await client.compress_pdf(input_file, compression_level="low")
370+
371+
assert seen == {"post": 1, "get": 1}
372+
373+
303374
@pytest.mark.asyncio
304375
async def test_async_compress_pdf_custom_profile(
305376
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)