Skip to content

Commit d003be6

Browse files
committed
update GitHub actions, better typing
1 parent 6c1581e commit d003be6

File tree

9 files changed

+183
-119
lines changed

9 files changed

+183
-119
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: CI
22

33
on:
44
push:
5-
branches: [ main, develop ]
5+
branches: [ main ]
66
pull_request:
77
branches: [ main ]
88

.github/workflows/security.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Security Checks
22

33
on:
44
push:
5-
branches: [ main, develop ]
5+
branches: [ main ]
66
pull_request:
77
branches: [ main ]
88
schedule:

examples/src/workflow.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -155,35 +155,6 @@ async def complex_workflow():
155155
raise error
156156

157157

158-
# Example 5: Using sample.pdf directly
159-
async def sample_pdf_workflow():
160-
print('\nExample 5: Using sample.pdf directly')
161-
162-
try:
163-
pdf_path = assets_dir / 'sample.pdf'
164-
165-
result = await client.workflow() \
166-
.add_file_part(pdf_path) \
167-
.apply_action(BuildActions.watermark_text('SAMPLE PDF', {
168-
'opacity': 0.4,
169-
'fontSize': 42,
170-
'fontColor': '#008000'
171-
})) \
172-
.output_pdf() \
173-
.execute()
174-
175-
# Save the result to the output directory
176-
output_path = output_dir / 'workflow-sample-pdf-processed.pdf'
177-
with open(output_path, 'wb') as f:
178-
f.write(result['output']['buffer'])
179-
180-
print(f'Sample PDF workflow successful. Output saved to: {output_path}')
181-
return output_path
182-
except Exception as error:
183-
print(f'Sample PDF workflow failed: {error}')
184-
raise error
185-
186-
187158
# Run all examples
188159
async def run_examples():
189160
try:
@@ -194,7 +165,6 @@ async def run_examples():
194165
await merge_with_watermark_workflow()
195166
await extract_text_workflow(converted_pdf_path)
196167
await complex_workflow()
197-
await sample_pdf_workflow()
198168

199169
print('\nAll workflow examples completed successfully!')
200170
except Exception as error:

src/nutrient_dws/builder/base_builder.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
"""Base builder class that all builders extend from."""
22

33
from abc import ABC, abstractmethod
4-
from typing import Any, Literal
4+
from typing import Literal, Union, overload
55

66
from nutrient_dws.builder.staged_builders import (
77
TypedWorkflowResult,
88
)
9+
from nutrient_dws.errors import ValidationError
910
from nutrient_dws.http import (
1011
AnalyzeBuildRequestData,
1112
BuildRequestData,
1213
NutrientClientOptions,
1314
RequestConfig,
15+
is_post_analyse_build_request_config,
16+
is_post_build_request_config,
1417
send_request,
1518
)
19+
from nutrient_dws.types.analyze_response import AnalyzeBuildResponse
20+
from nutrient_dws.types.build_response_json import BuildResponseJsonContents
1621

1722

1823
class BaseBuilder(ABC):
@@ -23,21 +28,35 @@ class BaseBuilder(ABC):
2328
def __init__(self, client_options: NutrientClientOptions) -> None:
2429
self.client_options = client_options
2530

31+
@overload
32+
async def _send_request(
33+
self, path: Literal["/build"], options: BuildRequestData
34+
) -> Union[BuildResponseJsonContents, bytes, str]: ...
35+
36+
@overload
37+
async def _send_request(
38+
self, path: Literal["/analyze_build"], options: AnalyzeBuildRequestData
39+
) -> AnalyzeBuildResponse: ...
40+
2641
async def _send_request(
2742
self,
28-
path: Literal["/build"] | Literal["/analyze_build"],
43+
path: Literal["/build", "/analyze_build"],
2944
options: BuildRequestData | AnalyzeBuildRequestData,
30-
) -> Any:
45+
) -> Union[BuildResponseJsonContents, bytes, str, AnalyzeBuildResponse]:
3146
"""Sends a request to the API."""
32-
config: RequestConfig[BuildRequestData | AnalyzeBuildRequestData] = {
33-
"endpoint": path,
34-
"method": "POST",
35-
"data": options,
36-
"headers": None,
37-
}
38-
39-
response: Any = await send_request(config, self.client_options)
40-
return response["data"]
47+
config = RequestConfig(endpoint=path, method="POST", data=options, headers=None)
48+
49+
if is_post_build_request_config(config):
50+
response = await send_request(config, self.client_options)
51+
return response["data"]
52+
53+
if is_post_analyse_build_request_config(config):
54+
analyze_response = await send_request(config, self.client_options)
55+
return analyze_response["data"]
56+
57+
raise ValidationError(
58+
"Invalid _send_request args", {"path": path, "options": options}
59+
)
4160

4261
@abstractmethod
4362
async def execute(self) -> TypedWorkflowResult:

src/nutrient_dws/builder/builder.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
PDFOutputOptions,
4747
PDFUAOutputOptions,
4848
)
49+
from nutrient_dws.types.build_response_json import BuildResponseJsonContents
4950
from nutrient_dws.types.input_parts import (
5051
DocumentPart,
5152
DocumentPartOptions,
@@ -537,20 +538,22 @@ async def execute(
537538

538539
if output_config["type"] == "json-content":
539540
result["success"] = True
540-
result["output"] = JsonContentOutput(data=response)
541+
result["output"] = JsonContentOutput(
542+
data=cast("BuildResponseJsonContents", response)
543+
)
541544
elif output_config["type"] in ["html", "markdown"]:
542545
mime_info = BuildOutputs.getMimeTypeForOutput(output_config)
543546
result["success"] = True
544547
result["output"] = ContentOutput(
545-
content=response,
548+
content=cast("str", response),
546549
mimeType=mime_info["mimeType"],
547550
filename=mime_info.get("filename"),
548551
)
549552
else:
550553
mime_info = BuildOutputs.getMimeTypeForOutput(output_config)
551554
result["success"] = True
552555
result["output"] = BufferOutput(
553-
buffer=response,
556+
buffer=cast("bytes", response),
554557
mimeType=mime_info["mimeType"],
555558
filename=mime_info.get("filename"),
556559
)

src/nutrient_dws/builder/staged_builders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class BufferOutput(TypedDict):
5353

5454

5555
class ContentOutput(TypedDict):
56-
content: bytes
56+
content: str
5757
mimeType: str
5858
filename: str | None
5959

src/nutrient_dws/client.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from nutrient_dws.errors import NutrientError, ValidationError
1919
from nutrient_dws.http import (
2020
NutrientClientOptions,
21+
RedactRequestData,
22+
RequestConfig,
2123
SignRequestData,
2224
SignRequestOptions,
2325
send_request,
@@ -54,7 +56,7 @@
5456
CreateAuthTokenResponse,
5557
)
5658
from nutrient_dws.types.misc import OcrLanguage, PageRange, Pages
57-
from nutrient_dws.types.redact_data import RedactData, RedactOptions
59+
from nutrient_dws.types.redact_data import RedactOptions
5860
from nutrient_dws.types.sign_request import CreateDigitalSignature
5961

6062
if TYPE_CHECKING:
@@ -1054,13 +1056,15 @@ async def create_redactions_ai(
10541056
if options:
10551057
request_data["data"]["options"] = options # type: ignore
10561058

1059+
config = RequestConfig(
1060+
method="POST",
1061+
data=cast("RedactRequestData", request_data),
1062+
endpoint="/ai/redact",
1063+
headers=None,
1064+
)
1065+
10571066
response: Any = await send_request(
1058-
{
1059-
"method": "POST",
1060-
"endpoint": "/ai/redact",
1061-
"data": cast("RedactData", request_data),
1062-
"headers": None,
1063-
},
1067+
config,
10641068
self.options,
10651069
)
10661070

0 commit comments

Comments
 (0)