Skip to content

Commit 75af16b

Browse files
client.py: Streamline payload dicts
Assisted-by: Codex
1 parent 82f948a commit 75af16b

1 file changed

Lines changed: 104 additions & 124 deletions

File tree

src/pdfrest/client.py

Lines changed: 104 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,25 +3324,19 @@ def convert_office_to_pdf(
33243324
) -> PdfRestFileBasedResponse:
33253325
"""Convert a Microsoft Office file (Word, Excel, PowerPoint) to PDF."""
33263326

3327-
payload: dict[str, Any] = {"files": file}
3328-
if output is not None:
3329-
payload["output"] = output
3330-
if compression is not None:
3331-
payload["compression"] = compression
3332-
if downsample is not None:
3333-
payload["downsample"] = downsample
3334-
if tagged_pdf is not None:
3335-
payload["tagged_pdf"] = tagged_pdf
3336-
if locale is not None:
3337-
payload["locale"] = locale
3338-
if page_size is not None:
3339-
payload["page_size"] = page_size
3340-
if page_margin is not None:
3341-
payload["page_margin"] = page_margin
3342-
if page_orientation is not None:
3343-
payload["page_orientation"] = page_orientation
3344-
if web_layout is not None:
3345-
payload["web_layout"] = web_layout
3327+
payload: dict[str, Any] = {
3328+
"files": file,
3329+
"output": output,
3330+
"compression": compression,
3331+
"downsample": downsample,
3332+
"tagged_pdf": tagged_pdf,
3333+
"locale": locale,
3334+
"page_size": page_size,
3335+
"page_margin": page_margin,
3336+
"page_orientation": page_orientation,
3337+
"web_layout": web_layout,
3338+
}
3339+
payload = {key: value for key, value in payload.items() if value is not None}
33463340

33473341
return self._post_file_operation(
33483342
endpoint="/pdf",
@@ -3368,13 +3362,13 @@ def convert_postscript_to_pdf(
33683362
) -> PdfRestFileBasedResponse:
33693363
"""Convert a PostScript or EPS file to PDF."""
33703364

3371-
payload: dict[str, Any] = {"files": file}
3372-
if output is not None:
3373-
payload["output"] = output
3374-
if compression is not None:
3375-
payload["compression"] = compression
3376-
if downsample is not None:
3377-
payload["downsample"] = downsample
3365+
payload: dict[str, Any] = {
3366+
"files": file,
3367+
"output": output,
3368+
"compression": compression,
3369+
"downsample": downsample,
3370+
}
3371+
payload = {key: value for key, value in payload.items() if value is not None}
33783372

33793373
return self._post_file_operation(
33803374
endpoint="/pdf",
@@ -3398,9 +3392,11 @@ def convert_email_to_pdf(
33983392
) -> PdfRestFileBasedResponse:
33993393
"""Convert an RFC822 email file to PDF."""
34003394

3401-
payload: dict[str, Any] = {"files": file}
3402-
if output is not None:
3403-
payload["output"] = output
3395+
payload: dict[str, Any] = {
3396+
"files": file,
3397+
"output": output,
3398+
}
3399+
payload = {key: value for key, value in payload.items() if value is not None}
34043400

34053401
return self._post_file_operation(
34063402
endpoint="/pdf",
@@ -3424,9 +3420,11 @@ def convert_image_to_pdf(
34243420
) -> PdfRestFileBasedResponse:
34253421
"""Convert a supported image file to PDF."""
34263422

3427-
payload: dict[str, Any] = {"files": file}
3428-
if output is not None:
3429-
payload["output"] = output
3423+
payload: dict[str, Any] = {
3424+
"files": file,
3425+
"output": output,
3426+
}
3427+
payload = {key: value for key, value in payload.items() if value is not None}
34303428

34313429
return self._post_file_operation(
34323430
endpoint="/pdf",
@@ -3456,21 +3454,17 @@ def convert_html_to_pdf(
34563454
) -> PdfRestFileBasedResponse:
34573455
"""Convert an uploaded HTML file to PDF."""
34583456

3459-
payload: dict[str, Any] = {"files": file}
3460-
if output is not None:
3461-
payload["output"] = output
3462-
if compression is not None:
3463-
payload["compression"] = compression
3464-
if downsample is not None:
3465-
payload["downsample"] = downsample
3466-
if page_size is not None:
3467-
payload["page_size"] = page_size
3468-
if page_margin is not None:
3469-
payload["page_margin"] = page_margin
3470-
if page_orientation is not None:
3471-
payload["page_orientation"] = page_orientation
3472-
if web_layout is not None:
3473-
payload["web_layout"] = web_layout
3457+
payload: dict[str, Any] = {
3458+
"files": file,
3459+
"output": output,
3460+
"compression": compression,
3461+
"downsample": downsample,
3462+
"page_size": page_size,
3463+
"page_margin": page_margin,
3464+
"page_orientation": page_orientation,
3465+
"web_layout": web_layout,
3466+
}
3467+
payload = {key: value for key, value in payload.items() if value is not None}
34743468

34753469
return self._post_file_operation(
34763470
endpoint="/pdf",
@@ -3500,21 +3494,17 @@ def convert_urls_to_pdf(
35003494
) -> PdfRestFileBasedResponse:
35013495
"""Convert HTML content from one or more URLs to PDF."""
35023496

3503-
payload: dict[str, Any] = {"url": urls}
3504-
if output is not None:
3505-
payload["output"] = output
3506-
if compression is not None:
3507-
payload["compression"] = compression
3508-
if downsample is not None:
3509-
payload["downsample"] = downsample
3510-
if page_size is not None:
3511-
payload["page_size"] = page_size
3512-
if page_margin is not None:
3513-
payload["page_margin"] = page_margin
3514-
if page_orientation is not None:
3515-
payload["page_orientation"] = page_orientation
3516-
if web_layout is not None:
3517-
payload["web_layout"] = web_layout
3497+
payload: dict[str, Any] = {
3498+
"url": urls,
3499+
"output": output,
3500+
"compression": compression,
3501+
"downsample": downsample,
3502+
"page_size": page_size,
3503+
"page_margin": page_margin,
3504+
"page_orientation": page_orientation,
3505+
"web_layout": web_layout,
3506+
}
3507+
payload = {key: value for key, value in payload.items() if value is not None}
35183508

35193509
return self._post_file_operation(
35203510
endpoint="/pdf",
@@ -5037,25 +5027,19 @@ async def convert_office_to_pdf(
50375027
) -> PdfRestFileBasedResponse:
50385028
"""Asynchronously convert a Microsoft Office file to PDF."""
50395029

5040-
payload: dict[str, Any] = {"files": file}
5041-
if output is not None:
5042-
payload["output"] = output
5043-
if compression is not None:
5044-
payload["compression"] = compression
5045-
if downsample is not None:
5046-
payload["downsample"] = downsample
5047-
if tagged_pdf is not None:
5048-
payload["tagged_pdf"] = tagged_pdf
5049-
if locale is not None:
5050-
payload["locale"] = locale
5051-
if page_size is not None:
5052-
payload["page_size"] = page_size
5053-
if page_margin is not None:
5054-
payload["page_margin"] = page_margin
5055-
if page_orientation is not None:
5056-
payload["page_orientation"] = page_orientation
5057-
if web_layout is not None:
5058-
payload["web_layout"] = web_layout
5030+
payload: dict[str, Any] = {
5031+
"files": file,
5032+
"output": output,
5033+
"compression": compression,
5034+
"downsample": downsample,
5035+
"tagged_pdf": tagged_pdf,
5036+
"locale": locale,
5037+
"page_size": page_size,
5038+
"page_margin": page_margin,
5039+
"page_orientation": page_orientation,
5040+
"web_layout": web_layout,
5041+
}
5042+
payload = {key: value for key, value in payload.items() if value is not None}
50595043

50605044
return await self._post_file_operation(
50615045
endpoint="/pdf",
@@ -5081,13 +5065,13 @@ async def convert_postscript_to_pdf(
50815065
) -> PdfRestFileBasedResponse:
50825066
"""Asynchronously convert a PostScript or EPS file to PDF."""
50835067

5084-
payload: dict[str, Any] = {"files": file}
5085-
if output is not None:
5086-
payload["output"] = output
5087-
if compression is not None:
5088-
payload["compression"] = compression
5089-
if downsample is not None:
5090-
payload["downsample"] = downsample
5068+
payload: dict[str, Any] = {
5069+
"files": file,
5070+
"output": output,
5071+
"compression": compression,
5072+
"downsample": downsample,
5073+
}
5074+
payload = {key: value for key, value in payload.items() if value is not None}
50915075

50925076
return await self._post_file_operation(
50935077
endpoint="/pdf",
@@ -5111,9 +5095,11 @@ async def convert_email_to_pdf(
51115095
) -> PdfRestFileBasedResponse:
51125096
"""Asynchronously convert an RFC822 email file to PDF."""
51135097

5114-
payload: dict[str, Any] = {"files": file}
5115-
if output is not None:
5116-
payload["output"] = output
5098+
payload: dict[str, Any] = {
5099+
"files": file,
5100+
"output": output,
5101+
}
5102+
payload = {key: value for key, value in payload.items() if value is not None}
51175103

51185104
return await self._post_file_operation(
51195105
endpoint="/pdf",
@@ -5137,9 +5123,11 @@ async def convert_image_to_pdf(
51375123
) -> PdfRestFileBasedResponse:
51385124
"""Asynchronously convert a supported image file to PDF."""
51395125

5140-
payload: dict[str, Any] = {"files": file}
5141-
if output is not None:
5142-
payload["output"] = output
5126+
payload: dict[str, Any] = {
5127+
"files": file,
5128+
"output": output,
5129+
}
5130+
payload = {key: value for key, value in payload.items() if value is not None}
51435131

51445132
return await self._post_file_operation(
51455133
endpoint="/pdf",
@@ -5169,21 +5157,17 @@ async def convert_html_to_pdf(
51695157
) -> PdfRestFileBasedResponse:
51705158
"""Asynchronously convert an uploaded HTML file to PDF."""
51715159

5172-
payload: dict[str, Any] = {"files": file}
5173-
if output is not None:
5174-
payload["output"] = output
5175-
if compression is not None:
5176-
payload["compression"] = compression
5177-
if downsample is not None:
5178-
payload["downsample"] = downsample
5179-
if page_size is not None:
5180-
payload["page_size"] = page_size
5181-
if page_margin is not None:
5182-
payload["page_margin"] = page_margin
5183-
if page_orientation is not None:
5184-
payload["page_orientation"] = page_orientation
5185-
if web_layout is not None:
5186-
payload["web_layout"] = web_layout
5160+
payload: dict[str, Any] = {
5161+
"files": file,
5162+
"output": output,
5163+
"compression": compression,
5164+
"downsample": downsample,
5165+
"page_size": page_size,
5166+
"page_margin": page_margin,
5167+
"page_orientation": page_orientation,
5168+
"web_layout": web_layout,
5169+
}
5170+
payload = {key: value for key, value in payload.items() if value is not None}
51875171

51885172
return await self._post_file_operation(
51895173
endpoint="/pdf",
@@ -5213,21 +5197,17 @@ async def convert_urls_to_pdf(
52135197
) -> PdfRestFileBasedResponse:
52145198
"""Asynchronously convert HTML content from one or more URLs to PDF."""
52155199

5216-
payload: dict[str, Any] = {"url": urls}
5217-
if output is not None:
5218-
payload["output"] = output
5219-
if compression is not None:
5220-
payload["compression"] = compression
5221-
if downsample is not None:
5222-
payload["downsample"] = downsample
5223-
if page_size is not None:
5224-
payload["page_size"] = page_size
5225-
if page_margin is not None:
5226-
payload["page_margin"] = page_margin
5227-
if page_orientation is not None:
5228-
payload["page_orientation"] = page_orientation
5229-
if web_layout is not None:
5230-
payload["web_layout"] = web_layout
5200+
payload: dict[str, Any] = {
5201+
"url": urls,
5202+
"output": output,
5203+
"compression": compression,
5204+
"downsample": downsample,
5205+
"page_size": page_size,
5206+
"page_margin": page_margin,
5207+
"page_orientation": page_orientation,
5208+
"web_layout": web_layout,
5209+
}
5210+
payload = {key: value for key, value in payload.items() if value is not None}
52315211

52325212
return await self._post_file_operation(
52335213
endpoint="/pdf",

0 commit comments

Comments
 (0)