Skip to content

Commit ebaad21

Browse files
models: Resolve payload-model test serialization drift and import cycle
- import `PdfRestFile` from `models.public` inside `_internal.py` to avoid circular imports - update encrypt/restrict unit tests to build expected request payloads through `PdfEncryptPayload`/`PdfDecryptPayload`/`PdfRestrictPayload`/`PdfUnrestrictPayload` - replace hand-built payload dicts with `model_validate(...).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)` Assisted-by: Codex
1 parent eff7194 commit ebaad21

3 files changed

Lines changed: 52 additions & 50 deletions

File tree

src/pdfrest/models/_internal.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
SummaryOutputType,
3434
TranslateOutputFormat,
3535
)
36-
from . import PdfRestFile
37-
from .public import PdfRestFileID
36+
from .public import PdfRestFile, PdfRestFileID
3837

3938

4039
def _ensure_list(value: Any) -> Any:

tests/test_encrypt_pdf.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
from pydantic import ValidationError
99

1010
from pdfrest import AsyncPdfRestClient, PdfRestClient
11-
from pdfrest.models import PdfRestFile, PdfRestFileBasedResponse, PdfRestFileID
11+
from pdfrest.models import (
12+
PdfRestFile,
13+
PdfRestFileBasedResponse,
14+
PdfRestFileID,
15+
)
16+
from pdfrest.models._internal import PdfDecryptPayload, PdfEncryptPayload
1217

1318
from .graphics_test_helpers import (
1419
ASYNC_API_KEY,
@@ -39,18 +44,16 @@ def build_encrypt_payload(
3944
current_open_password: str | None = None,
4045
current_permissions_password: str | None = None,
4146
output: str | None = None,
42-
) -> dict[str, str]:
43-
payload: dict[str, str] = {
44-
"id": str(input_file.id),
45-
"new_open_password": new_open_password,
46-
}
47-
if current_open_password is not None:
48-
payload["current_open_password"] = current_open_password
49-
if current_permissions_password is not None:
50-
payload["current_permissions_password"] = current_permissions_password
51-
if output is not None:
52-
payload["output"] = output
53-
return payload
47+
) -> dict[str, object]:
48+
return PdfEncryptPayload.model_validate(
49+
{
50+
"files": [input_file],
51+
"new_open_password": new_open_password,
52+
"current_open_password": current_open_password,
53+
"current_permissions_password": current_permissions_password,
54+
"output": output,
55+
}
56+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
5457

5558

5659
def build_decrypt_payload(
@@ -59,16 +62,15 @@ def build_decrypt_payload(
5962
current_open_password: str,
6063
current_permissions_password: str | None = None,
6164
output: str | None = None,
62-
) -> dict[str, str]:
63-
payload: dict[str, str] = {
64-
"id": str(input_file.id),
65-
"current_open_password": current_open_password,
66-
}
67-
if current_permissions_password is not None:
68-
payload["current_permissions_password"] = current_permissions_password
69-
if output is not None:
70-
payload["output"] = output
71-
return payload
65+
) -> dict[str, object]:
66+
return PdfDecryptPayload.model_validate(
67+
{
68+
"files": [input_file],
69+
"current_open_password": current_open_password,
70+
"current_permissions_password": current_permissions_password,
71+
"output": output,
72+
}
73+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
7274

7375

7476
@pytest.mark.parametrize(

tests/test_permissions_password.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
from pydantic import ValidationError
99

1010
from pdfrest import AsyncPdfRestClient, PdfRestClient
11-
from pdfrest.models import PdfRestFile, PdfRestFileBasedResponse, PdfRestFileID
11+
from pdfrest.models import (
12+
PdfRestFile,
13+
PdfRestFileBasedResponse,
14+
PdfRestFileID,
15+
)
16+
from pdfrest.models._internal import PdfRestrictPayload, PdfUnrestrictPayload
1217
from pdfrest.types import PdfRestriction
1318

1419
from .graphics_test_helpers import (
@@ -41,20 +46,17 @@ def build_restrict_payload(
4146
current_open_password: str | None = None,
4247
restrictions: list[PdfRestriction] | None = None,
4348
output: str | None = None,
44-
) -> dict[str, str | list[PdfRestriction]]:
45-
payload: dict[str, str | list[PdfRestriction]] = {
46-
"id": str(input_file.id),
47-
"new_permissions_password": new_permissions_password,
48-
}
49-
if current_permissions_password is not None:
50-
payload["current_permissions_password"] = current_permissions_password
51-
if current_open_password is not None:
52-
payload["current_open_password"] = current_open_password
53-
if restrictions is not None:
54-
payload["restrictions"] = restrictions
55-
if output is not None:
56-
payload["output"] = output
57-
return payload
49+
) -> dict[str, object]:
50+
return PdfRestrictPayload.model_validate(
51+
{
52+
"files": [input_file],
53+
"new_permissions_password": new_permissions_password,
54+
"current_permissions_password": current_permissions_password,
55+
"current_open_password": current_open_password,
56+
"restrictions": restrictions,
57+
"output": output,
58+
}
59+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
5860

5961

6062
def build_unrestrict_payload(
@@ -63,16 +65,15 @@ def build_unrestrict_payload(
6365
current_permissions_password: str,
6466
current_open_password: str | None = None,
6567
output: str | None = None,
66-
) -> dict[str, str]:
67-
payload: dict[str, str] = {
68-
"id": str(input_file.id),
69-
"current_permissions_password": current_permissions_password,
70-
}
71-
if current_open_password is not None:
72-
payload["current_open_password"] = current_open_password
73-
if output is not None:
74-
payload["output"] = output
75-
return payload
68+
) -> dict[str, object]:
69+
return PdfUnrestrictPayload.model_validate(
70+
{
71+
"files": [input_file],
72+
"current_permissions_password": current_permissions_password,
73+
"current_open_password": current_open_password,
74+
"output": output,
75+
}
76+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
7677

7778

7879
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)