Skip to content

Commit 145f8b4

Browse files
✨ add support for V2 client
1 parent f57ba8d commit 145f8b4

28 files changed

+843
-112
lines changed

mindee/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mindee import product
22
from mindee.client import Client
3+
from mindee.input.inference_predict_options import InferencePredictOptions
34
from mindee.input.local_response import LocalResponse
45
from mindee.input.page_options import PageOptions
56
from mindee.parsing.common.api_response import ApiResponse

mindee/client.py

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
from pathlib import Path
21
from time import sleep
3-
from typing import BinaryIO, Dict, Optional, Type, Union
2+
from typing import Dict, Optional, Type, Union
43

4+
from mindee.client_mixin import ClientMixin
55
from mindee.error.mindee_error import MindeeClientError, MindeeError
66
from mindee.error.mindee_http_error import handle_error
77
from mindee.input import WorkflowOptions
88
from mindee.input.local_response import LocalResponse
99
from mindee.input.page_options import PageOptions
1010
from mindee.input.predict_options import AsyncPredictOptions, PredictOptions
11-
from mindee.input.sources.base_64_input import Base64Input
12-
from mindee.input.sources.bytes_input import BytesInput
13-
from mindee.input.sources.file_input import FileInput
1411
from mindee.input.sources.local_input_source import LocalInputSource
15-
from mindee.input.sources.path_input import PathInput
1612
from mindee.input.sources.url_input_source import UrlInputSource
1713
from mindee.logger import logger
1814
from mindee.mindee_http.endpoint import CustomEndpoint, Endpoint
@@ -55,7 +51,7 @@ def _clean_account_name(account_name: str) -> str:
5551
return account_name
5652

5753

58-
class Client:
54+
class Client(ClientMixin):
5955
"""
6056
Mindee API Client.
6157
@@ -275,23 +271,6 @@ def execute_workflow(
275271
logger.debug("Sending document to workflow: %s", workflow_id)
276272
return self._send_to_workflow(GeneratedV1, input_source, workflow_id, options)
277273

278-
def _validate_async_params(
279-
self, initial_delay_sec: float, delay_sec: float, max_retries: int
280-
) -> None:
281-
min_delay = 1
282-
min_initial_delay = 1
283-
min_retries = 1
284-
if delay_sec < min_delay:
285-
raise MindeeClientError(
286-
f"Cannot set auto-parsing delay to less than {min_delay} second(s)."
287-
)
288-
if initial_delay_sec < min_initial_delay:
289-
raise MindeeClientError(
290-
f"Cannot set initial parsing delay to less than {min_initial_delay} second(s)."
291-
)
292-
if max_retries < min_retries:
293-
raise MindeeClientError(f"Cannot set retries to less than {min_retries}.")
294-
295274
def enqueue_and_parse( # pylint: disable=too-many-locals
296275
self,
297276
product_class: Type[Inference],
@@ -583,80 +562,3 @@ def create_endpoint(
583562
)
584563
version = "1"
585564
return self._build_endpoint(endpoint_name, account_name, version)
586-
587-
@staticmethod
588-
def source_from_path(
589-
input_path: Union[Path, str], fix_pdf: bool = False
590-
) -> PathInput:
591-
"""
592-
Load a document from an absolute path, as a string.
593-
594-
:param input_path: Path of file to open
595-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
596-
Setting this to `True` can modify the data sent to Mindee.
597-
"""
598-
input_doc = PathInput(input_path)
599-
if fix_pdf:
600-
input_doc.fix_pdf()
601-
return input_doc
602-
603-
@staticmethod
604-
def source_from_file(input_file: BinaryIO, fix_pdf: bool = False) -> FileInput:
605-
"""
606-
Load a document from a normal Python file object/handle.
607-
608-
:param input_file: Input file handle
609-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
610-
Setting this to `True` can modify the data sent to Mindee.
611-
"""
612-
input_doc = FileInput(input_file)
613-
if fix_pdf:
614-
input_doc.fix_pdf()
615-
return input_doc
616-
617-
@staticmethod
618-
def source_from_b64string(
619-
input_string: str, filename: str, fix_pdf: bool = False
620-
) -> Base64Input:
621-
"""
622-
Load a document from a base64 encoded string.
623-
624-
:param input_string: Input to parse as base64 string
625-
:param filename: The name of the file (without the path)
626-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
627-
Setting this to `True` can modify the data sent to Mindee.
628-
"""
629-
input_doc = Base64Input(input_string, filename)
630-
if fix_pdf:
631-
input_doc.fix_pdf()
632-
return input_doc
633-
634-
@staticmethod
635-
def source_from_bytes(
636-
input_bytes: bytes, filename: str, fix_pdf: bool = False
637-
) -> BytesInput:
638-
"""
639-
Load a document from raw bytes.
640-
641-
:param input_bytes: Raw byte input
642-
:param filename: The name of the file (without the path)
643-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
644-
Setting this to `True` can modify the data sent to Mindee.
645-
"""
646-
input_doc = BytesInput(input_bytes, filename)
647-
if fix_pdf:
648-
input_doc.fix_pdf()
649-
return input_doc
650-
651-
@staticmethod
652-
def source_from_url(
653-
url: str,
654-
) -> UrlInputSource:
655-
"""
656-
Load a document from a URL.
657-
658-
:param url: Raw byte input
659-
"""
660-
return UrlInputSource(
661-
url,
662-
)

mindee/client_mixin.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from pathlib import Path
2+
from typing import BinaryIO, Union
3+
4+
from mindee.error import MindeeClientError
5+
from mindee.input import Base64Input, BytesInput, FileInput, PathInput, UrlInputSource
6+
7+
8+
class ClientMixin:
9+
"""Mixin for client Client V1 & V2 common static methods."""
10+
11+
@staticmethod
12+
def source_from_path(
13+
input_path: Union[Path, str], fix_pdf: bool = False
14+
) -> PathInput:
15+
"""
16+
Load a document from an absolute path, as a string.
17+
18+
:param input_path: Path of file to open
19+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
20+
Setting this to `True` can modify the data sent to Mindee.
21+
"""
22+
input_doc = PathInput(input_path)
23+
if fix_pdf:
24+
input_doc.fix_pdf()
25+
return input_doc
26+
27+
@staticmethod
28+
def source_from_file(input_file: BinaryIO, fix_pdf: bool = False) -> FileInput:
29+
"""
30+
Load a document from a normal Python file object/handle.
31+
32+
:param input_file: Input file handle
33+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
34+
Setting this to `True` can modify the data sent to Mindee.
35+
"""
36+
input_doc = FileInput(input_file)
37+
if fix_pdf:
38+
input_doc.fix_pdf()
39+
return input_doc
40+
41+
@staticmethod
42+
def source_from_b64string(
43+
input_string: str, filename: str, fix_pdf: bool = False
44+
) -> Base64Input:
45+
"""
46+
Load a document from a base64 encoded string.
47+
48+
:param input_string: Input to parse as base64 string
49+
:param filename: The name of the file (without the path)
50+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
51+
Setting this to `True` can modify the data sent to Mindee.
52+
"""
53+
input_doc = Base64Input(input_string, filename)
54+
if fix_pdf:
55+
input_doc.fix_pdf()
56+
return input_doc
57+
58+
@staticmethod
59+
def source_from_bytes(
60+
input_bytes: bytes, filename: str, fix_pdf: bool = False
61+
) -> BytesInput:
62+
"""
63+
Load a document from raw bytes.
64+
65+
:param input_bytes: Raw byte input
66+
:param filename: The name of the file (without the path)
67+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
68+
Setting this to `True` can modify the data sent to Mindee.
69+
"""
70+
input_doc = BytesInput(input_bytes, filename)
71+
if fix_pdf:
72+
input_doc.fix_pdf()
73+
return input_doc
74+
75+
@staticmethod
76+
def source_from_url(
77+
url: str,
78+
) -> UrlInputSource:
79+
"""
80+
Load a document from a URL.
81+
82+
:param url: Raw byte input
83+
"""
84+
return UrlInputSource(
85+
url,
86+
)
87+
88+
@staticmethod
89+
def _validate_async_params(
90+
initial_delay_sec: float, delay_sec: float, max_retries: int
91+
) -> None:
92+
min_delay = 1
93+
min_initial_delay = 1
94+
min_retries = 1
95+
if delay_sec < min_delay:
96+
raise MindeeClientError(
97+
f"Cannot set auto-parsing delay to less than {min_delay} second(s)."
98+
)
99+
if initial_delay_sec < min_initial_delay:
100+
raise MindeeClientError(
101+
f"Cannot set initial parsing delay to less than {min_initial_delay} second(s)."
102+
)
103+
if max_retries < min_retries:
104+
raise MindeeClientError(f"Cannot set retries to less than {min_retries}.")

0 commit comments

Comments
 (0)