Skip to content

Commit 3c57afc

Browse files
committed
Finish type checking
1 parent 96ca350 commit 3c57afc

File tree

4 files changed

+123
-119
lines changed

4 files changed

+123
-119
lines changed

src/nutrient_dws/builder/base_builder.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from abc import ABC, abstractmethod
33
from typing import Any, Generic, Literal, TypeVar, Union
44

5-
from nutrient_dws.builder.staged_builders import TypedWorkflowResult
5+
from nutrient_dws.builder.staged_builders import TypedWorkflowResult, BufferOutput, ContentOutput, JsonContentOutput
66
from nutrient_dws.http import (
77
AnalyzeBuildRequestData,
88
BuildRequestData,
@@ -24,7 +24,6 @@ async def _send_request(
2424
self,
2525
path: Union[Literal["/build"], Literal["/analyze_build"]],
2626
options: Union[BuildRequestData, AnalyzeBuildRequestData],
27-
response_type: str = "json",
2827
) -> Any:
2928
"""Sends a request to the API."""
3029
config: RequestConfig[Union[BuildRequestData, AnalyzeBuildRequestData]] = {
@@ -34,7 +33,7 @@ async def _send_request(
3433
"headers": None
3534
}
3635

37-
response = await send_request(config, self.client_options, response_type)
36+
response: Any = await send_request(config, self.client_options)
3837
return response["data"]
3938

4039
@abstractmethod

src/nutrient_dws/builder/builder.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -500,18 +500,10 @@ async def execute(
500500

501501
files = await self._prepare_files()
502502

503-
# Determine response type
504-
response_type = "arraybuffer"
505-
if output_config["type"] == "json-content":
506-
response_type = "json"
507-
elif output_config["type"] in ["html", "markdown"]:
508-
response_type = "text"
509-
510503
# Make the request
511504
response = await self._send_request(
512505
"/build",
513506
BuildRequestData(instructions=self.build_instructions, files=files),
514-
response_type,
515507
)
516508

517509
# Step 3: Process response
@@ -574,8 +566,7 @@ async def dry_run(self) -> WorkflowDryRunResult:
574566

575567
response = await self._send_request(
576568
"/analyze_build",
577-
AnalyzeBuildRequestData(instructions=self.build_instructions),
578-
"json",
569+
AnalyzeBuildRequestData(instructions=self.build_instructions)
579570
)
580571

581572
result["success"] = True

src/nutrient_dws/client.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,17 @@ async def get_account_info(self) -> dict[str, Any]:
129129
print(account_info['subscriptionType'])
130130
```
131131
"""
132-
response = await send_request(
132+
response: Any = await send_request(
133133
{
134134
"method": "GET",
135135
"endpoint": "/account/info",
136-
"data": {},
136+
"data": None,
137137
"headers": None,
138138
},
139139
self.options,
140-
"json",
141140
)
142141

143-
return response["data"]
142+
return cast(dict[str, Any], response["data"])
144143

145144
async def create_token(self, params: CreateAuthTokenParameters) -> CreateAuthTokenResponse:
146145
"""Create a new authentication token.
@@ -160,17 +159,17 @@ async def create_token(self, params: CreateAuthTokenParameters) -> CreateAuthTok
160159
print(token['id'])
161160
```
162161
"""
163-
response = await send_request(
162+
response: Any = await send_request(
164163
{
165164
"method": "POST",
166165
"endpoint": "/tokens",
167166
"data": params,
167+
"headers": None,
168168
},
169169
self.options,
170-
"json",
171170
)
172171

173-
return response["data"]
172+
return cast(CreateAuthTokenResponse, response["data"])
174173

175174
async def delete_token(self, token_id: str) -> None:
176175
"""Delete an authentication token.
@@ -187,10 +186,10 @@ async def delete_token(self, token_id: str) -> None:
187186
{
188187
"method": "DELETE",
189188
"endpoint": "/tokens",
190-
"data": {"id": token_id},
189+
"data": cast(Any, {"id": token_id}),
190+
"headers": None,
191191
},
192192
self.options,
193-
"json",
194193
)
195194

196195
def workflow(self, override_timeout: Optional[int] = None) -> WorkflowInitialStage:
@@ -218,7 +217,7 @@ def workflow(self, override_timeout: Optional[int] = None) -> WorkflowInitialSta
218217
return StagedWorkflowBuilder(options)
219218

220219
def _process_typed_workflow_result(
221-
self, result: TypedWorkflowResult[Union[BufferOutput, ContentOutput, JsonContentOutput]]
220+
self, result: TypedWorkflowResult
222221
) -> Union[BufferOutput, ContentOutput, JsonContentOutput]:
223222
"""Helper function that takes a TypedWorkflowResult, throws any errors, and returns the specific output type.
224223
@@ -325,15 +324,14 @@ async def sign(
325324
if normalized_graphic_image:
326325
request_data["graphicImage"] = normalized_graphic_image
327326

328-
response = await send_request(
327+
response: Any = await send_request(
329328
{
330329
"method": "POST",
331330
"endpoint": "/sign",
332-
"data": request_data,
331+
"data": cast(Any, request_data),
333332
"headers": None,
334333
},
335334
self.options,
336-
"arraybuffer",
337335
)
338336

339337
buffer = response["data"]
@@ -372,12 +370,12 @@ async def watermark_text(
372370
f.write(pdf_buffer)
373371
```
374372
"""
375-
watermark_action = BuildActions.watermarkText(text, options or {})
373+
watermark_action = BuildActions.watermarkText(text, cast(Any, options))
376374

377375
builder = self.workflow().add_file_part(file, None, [watermark_action])
378376

379377
result = await builder.output_pdf().execute()
380-
return self._process_typed_workflow_result(result)
378+
return cast(BufferOutput, self._process_typed_workflow_result(result))
381379

382380
async def watermark_image(
383381
self,
@@ -406,12 +404,12 @@ async def watermark_image(
406404
pdf_buffer = result['buffer']
407405
```
408406
"""
409-
watermark_action = BuildActions.watermarkImage(image, options or {})
407+
watermark_action = BuildActions.watermarkImage(image, cast(Any, options))
410408

411409
builder = self.workflow().add_file_part(file, None, [watermark_action])
412410

413411
result = await builder.output_pdf().execute()
414-
return self._process_typed_workflow_result(result)
412+
return cast(BufferOutput, self._process_typed_workflow_result(result))
415413

416414
async def convert(
417415
self,
@@ -496,7 +494,7 @@ async def ocr(
496494
builder = self.workflow().add_file_part(file, None, [ocr_action])
497495

498496
result = await builder.output_pdf().execute()
499-
return self._process_typed_workflow_result(result)
497+
return cast(BufferOutput, self._process_typed_workflow_result(result))
500498

501499
async def extract_text(
502500
self,
@@ -527,7 +525,7 @@ async def extract_text(
527525
"""
528526
normalized_pages = normalize_page_params(pages) if pages else None
529527

530-
part_options = {"pages": normalized_pages} if normalized_pages else None
528+
part_options = cast(Any, {"pages": normalized_pages}) if normalized_pages else None
531529

532530
result = (
533531
await self.workflow()
@@ -536,7 +534,7 @@ async def extract_text(
536534
.execute()
537535
)
538536

539-
return self._process_typed_workflow_result(result)
537+
return cast(JsonContentOutput, self._process_typed_workflow_result(result))
540538

541539
async def extract_table(
542540
self,
@@ -568,7 +566,7 @@ async def extract_table(
568566
"""
569567
normalized_pages = normalize_page_params(pages) if pages else None
570568

571-
part_options = {"pages": normalized_pages} if normalized_pages else None
569+
part_options = cast(Any, {"pages": normalized_pages}) if normalized_pages else None
572570

573571
result = (
574572
await self.workflow()
@@ -577,7 +575,7 @@ async def extract_table(
577575
.execute()
578576
)
579577

580-
return self._process_typed_workflow_result(result)
578+
return cast(JsonContentOutput, self._process_typed_workflow_result(result))
581579

582580
async def extract_key_value_pairs(
583581
self,
@@ -609,7 +607,7 @@ async def extract_key_value_pairs(
609607
"""
610608
normalized_pages = normalize_page_params(pages) if pages else None
611609

612-
part_options = {"pages": normalized_pages} if normalized_pages else None
610+
part_options = cast(Any, {"pages": normalized_pages}) if normalized_pages else None
613611

614612
result = (
615613
await self.workflow()
@@ -618,7 +616,7 @@ async def extract_key_value_pairs(
618616
.execute()
619617
)
620618

621-
return self._process_typed_workflow_result(result)
619+
return cast(JsonContentOutput, self._process_typed_workflow_result(result))
622620

623621
async def password_protect(
624622
self,
@@ -660,9 +658,9 @@ async def password_protect(
660658
if permissions:
661659
pdf_options["userPermissions"] = permissions
662660

663-
result = await self.workflow().add_file_part(file).output_pdf(pdf_options).execute()
661+
result = await self.workflow().add_file_part(file).output_pdf(cast(Any, pdf_options)).execute()
664662

665-
return self._process_typed_workflow_result(result)
663+
return cast(BufferOutput, self._process_typed_workflow_result(result))
666664

667665
async def set_metadata(
668666
self,
@@ -697,10 +695,10 @@ async def set_metadata(
697695
raise ValidationError("Invalid pdf file", {"input": pdf})
698696

699697
result = (
700-
await self.workflow().add_file_part(pdf).output_pdf({"metadata": metadata}).execute()
698+
await self.workflow().add_file_part(pdf).output_pdf(cast(Any, {"metadata": metadata})).execute()
701699
)
702700

703-
return self._process_typed_workflow_result(result)
701+
return cast(BufferOutput, self._process_typed_workflow_result(result))
704702

705703
async def merge(self, files: List[FileInput]) -> BufferOutput:
706704
"""Merge multiple documents into a single document.
@@ -733,7 +731,7 @@ async def merge(self, files: List[FileInput]) -> BufferOutput:
733731
workflow_builder = workflow_builder.add_file_part(file)
734732

735733
result = await workflow_builder.output_pdf().execute()
736-
return self._process_typed_workflow_result(result)
734+
return cast(BufferOutput, self._process_typed_workflow_result(result))
737735

738736
async def flatten(
739737
self,
@@ -774,7 +772,7 @@ async def flatten(
774772
await self.workflow().add_file_part(pdf, None, [flatten_action]).output_pdf().execute()
775773
)
776774

777-
return self._process_typed_workflow_result(result)
775+
return cast(BufferOutput, self._process_typed_workflow_result(result))
778776

779777
async def create_redactions_ai(
780778
self,
@@ -844,17 +842,16 @@ async def create_redactions_ai(
844842
}
845843

846844
if options:
847-
request_data["data"]["options"] = options
845+
request_data["data"]["options"] = options # type: ignore
848846

849-
response = await send_request(
847+
response: Any = await send_request(
850848
{
851849
"method": "POST",
852850
"endpoint": "/ai/redact",
853-
"data": request_data,
851+
"data": cast(Any, request_data),
854852
"headers": None,
855853
},
856854
self.options,
857-
"arraybuffer",
858855
)
859856

860857
buffer = response["data"]

0 commit comments

Comments
 (0)