Skip to content

Commit 94b75f4

Browse files
Add get method for retrieving file metadata in clients
- Added `get` method in both synchronous and asynchronous clients to fetch file metadata by file identifier (`PdfRestFileID` or string input). - Implemented validation for invalid file IDs in both sync and async clients. - Enriched test cases to ensure coverage for valid and invalid scenarios, including both sync and async executions. Assisted-by: Codex
1 parent cf02a2c commit 94b75f4

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

src/pdfrest/client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
PdfRestErrorResponse,
2828
PdfRestFile,
2929
PdfRestFileBasedResponse,
30+
PdfRestFileID,
3031
UpResponse,
3132
)
3233

@@ -206,6 +207,12 @@ def _resolve_file_id(file_ref: PdfRestFile | str) -> str:
206207
return file_ref.id if isinstance(file_ref, PdfRestFile) else str(file_ref)
207208

208209

210+
def _normalize_file_id(file_ref: PdfRestFileID | str) -> PdfRestFileID:
211+
if isinstance(file_ref, PdfRestFileID):
212+
return file_ref
213+
return PdfRestFileID(str(file_ref))
214+
215+
209216
ClientType = TypeVar("ClientType", httpx.Client, httpx.AsyncClient)
210217

211218

@@ -742,6 +749,11 @@ class _FilesClient:
742749
def __init__(self, client: _SyncApiClient) -> None:
743750
self._client = client
744751

752+
def get(self, file_ref: PdfRestFileID | str) -> PdfRestFile:
753+
"""Retrieve file metadata given a file identifier."""
754+
file_id = _normalize_file_id(file_ref)
755+
return self._client.fetch_file_info(str(file_id))
756+
745757
def create(self, files: UploadFiles) -> list[PdfRestFile]:
746758
"""Upload one or more files by content.
747759
@@ -857,6 +869,11 @@ def __init__(
857869
self._client = client
858870
self._concurrency_limit = concurrency_limit
859871

872+
async def get(self, file_ref: PdfRestFileID | str) -> PdfRestFile:
873+
"""Retrieve file metadata given a file identifier."""
874+
file_id = _normalize_file_id(file_ref)
875+
return await self._client.fetch_file_info(str(file_id))
876+
860877
async def create(self, files: UploadFiles) -> list[PdfRestFile]:
861878
"""Upload one or more files by content.
862879

tests/test_files.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pytest_asyncio
1515

1616
from pdfrest import AsyncPdfRestClient, PdfRestClient
17-
from pdfrest.models import PdfRestFile
17+
from pdfrest.models import PdfRestFile, PdfRestFileID
1818

1919
from .resources import get_test_resource_path
2020

@@ -145,6 +145,45 @@ def live_async_file(
145145
)
146146

147147

148+
@pytest.mark.parametrize(
149+
"file_ref",
150+
[
151+
pytest.param(PdfRestFileID.generate(), id="pdfrest-file-id"),
152+
pytest.param(str(uuid.uuid4()), id="raw-str"),
153+
],
154+
)
155+
def test_files_get_fetches_info(file_ref: PdfRestFileID | str) -> None:
156+
file_id = str(file_ref)
157+
info_payload = _build_file_info_payload(file_id, "report.pdf")
158+
159+
def handler(request: httpx.Request) -> httpx.Response:
160+
if request.method == "GET" and request.url.path == f"/resource/{file_id}":
161+
assert request.url.params["format"] == "info"
162+
return httpx.Response(200, json=info_payload)
163+
msg = f"Unexpected request: {request.method} {request.url}"
164+
raise AssertionError(msg)
165+
166+
transport = httpx.MockTransport(handler)
167+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
168+
file_repr = client.files.get(file_ref)
169+
170+
_assert_file_matches_payload(file_repr, info_payload)
171+
172+
173+
def test_files_get_rejects_invalid_id() -> None:
174+
transport = httpx.MockTransport(
175+
lambda request: (_ for _ in ()).throw(
176+
AssertionError("Request should not be sent for invalid IDs.")
177+
)
178+
)
179+
180+
with (
181+
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
182+
pytest.raises(ValueError, match="Invalid PdfRestPrefixedUUID4"),
183+
):
184+
client.files.get("not-a-valid-id")
185+
186+
148187
def test_files_create_uses_upload_and_info() -> None:
149188
uploaded_file_id = str(uuid.uuid4())
150189
info_payload = _build_file_info_payload(uploaded_file_id, "report.pdf")
@@ -698,6 +737,45 @@ def handler(request: httpx.Request) -> httpx.Response:
698737
_assert_file_matches_payload(response[0], info_payload)
699738

700739

740+
@pytest.mark.asyncio
741+
@pytest.mark.parametrize(
742+
"file_ref",
743+
[
744+
pytest.param(PdfRestFileID.generate(), id="pdfrest-file-id"),
745+
pytest.param(str(uuid.uuid4()), id="raw-str"),
746+
],
747+
)
748+
async def test_async_files_get_fetches_info(file_ref: PdfRestFileID | str) -> None:
749+
file_id = str(file_ref)
750+
info_payload = _build_file_info_payload(file_id, "report.pdf")
751+
752+
def handler(request: httpx.Request) -> httpx.Response:
753+
if request.method == "GET" and request.url.path == f"/resource/{file_id}":
754+
assert request.url.params["format"] == "info"
755+
return httpx.Response(200, json=info_payload)
756+
msg = f"Unexpected request: {request.method} {request.url}"
757+
raise AssertionError(msg)
758+
759+
transport = httpx.MockTransport(handler)
760+
async with AsyncPdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
761+
file_repr = await client.files.get(file_ref)
762+
763+
_assert_file_matches_payload(file_repr, info_payload)
764+
765+
766+
@pytest.mark.asyncio
767+
async def test_async_files_get_rejects_invalid_id() -> None:
768+
transport = httpx.MockTransport(
769+
lambda request: (_ for _ in ()).throw(
770+
AssertionError("Request should not be sent for invalid IDs.")
771+
)
772+
)
773+
774+
async with AsyncPdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
775+
with pytest.raises(ValueError, match="Invalid PdfRestPrefixedUUID4"):
776+
await client.files.get("not-a-valid-id")
777+
778+
701779
@pytest.mark.asyncio
702780
async def test_async_files_create_from_paths() -> None:
703781
uploaded_ids = [str(uuid.uuid4()), str(uuid.uuid4())]

0 commit comments

Comments
 (0)