Skip to content

Commit bf622e1

Browse files
✨ add support for direct url GET inference
1 parent d8bbeef commit bf622e1

5 files changed

Lines changed: 67 additions & 6 deletions

File tree

mindee/v2/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ def get_result(
8484

8585
return self.mindee_api.get_result(response_type, inference_id)
8686

87+
def get_result_from_url(
88+
self, response_type: type[TypeBaseResponse], url: str
89+
) -> TypeBaseResponse:
90+
"""
91+
Get the result of an inference that was previously enqueued by its URL.
92+
93+
:param response_type: Type of the response to return.
94+
:param url: URL of the inference to retrieve.
95+
:return: The result of the inference.
96+
"""
97+
return self.mindee_api.get_result_by_url(response_type, url)
98+
8799
def enqueue_and_get_result(
88100
self,
89101
response_type: type[TypeBaseResponse],

mindee/v2/mindee_http/mindee_api_v2.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ def req_get_job(self, job_id: str) -> requests.Response:
131131
allow_redirects=False,
132132
)
133133

134+
def req_get_inference_by_url(self, url) -> requests.Response:
135+
"""
136+
Sends a request matching a given inference_id. Returns either a Job or a Document.
137+
138+
:param url: URL to use for the request.
139+
:return: Response object from the request.
140+
"""
141+
return requests.get(
142+
url,
143+
headers=self.base_headers,
144+
timeout=self.request_timeout,
145+
allow_redirects=False,
146+
)
147+
134148
def req_get_inference(self, inference_id: str, slug: str) -> requests.Response:
135149
"""
136150
Sends a request matching a given queue_id. Returns either a Job or a Document.
@@ -212,6 +226,20 @@ def get_result(self, response_type, inference_id: str):
212226
handle_error_v2(dict_response)
213227
return response_type(dict_response)
214228

229+
def get_result_by_url(self, response_type, url: str):
230+
"""
231+
Get the result of an inference that was previously enqueued by its URL.
232+
233+
:param response_type: Type of the response to return.
234+
:param url: URL of the inference to retrieve.
235+
:return: The result of the inference.
236+
"""
237+
response = self.req_get_inference_by_url(url)
238+
dict_response = self._response_json(response)
239+
if not is_valid_get_response(response):
240+
handle_error_v2(dict_response)
241+
return response_type(dict_response)
242+
215243
def get_models(self, name: str | None, model_type: str | None):
216244
"""
217245
Get a list of models matching the provided name and type.

tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import gc
2+
3+
import pytest
4+
5+
6+
@pytest.fixture(autouse=True)
7+
def force_gc():
8+
yield
9+
gc.collect()

tests/v1/input/test_url_input_source_integration.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,35 @@ def output_file_path():
2121

2222
@pytest.fixture
2323
def reference_file_path():
24-
return "https://github.com/mindee/client-lib-test-data/blob/main/v1/products/invoice_splitter/invoice_5p.pdf?raw=true"
24+
ref_path = os.getenv("MINDEE_V2_SE_TESTS_BLANK_PDF_URL")
25+
if ref_path is None:
26+
raise ValueError("MINDEE_V2_SE_TESTS_BLANK_PDF_URL not set")
27+
return ref_path
2528

2629

2730
@pytest.mark.integration
2831
def test_load_local_file(client, reference_file_path):
2932
url_source = URLInputSource(reference_file_path)
3033
local_source = url_source.as_local_input_source()
3134
result = client.parse(InvoiceV4, local_source)
32-
assert result.document.n_pages == 5
33-
assert result.document.filename == "invoice_5p.pdf"
35+
assert result.document.n_pages == 1
36+
assert result.document.filename == "blank_1.pdf"
3437

3538

3639
@pytest.mark.integration
3740
def test_custom_file_name(client, reference_file_path):
3841
url_source = URLInputSource(reference_file_path)
3942
local_source = url_source.as_local_input_source("customName.pdf")
4043
result = client.parse(InvoiceV4, local_source)
41-
assert result.document.n_pages == 5
44+
assert result.document.n_pages == 1
4245
assert result.document.filename == "customName.pdf"
4346

4447

4548
@pytest.mark.integration
4649
def test_save_file(client, reference_file_path, output_file_path):
4750
url_source = URLInputSource(reference_file_path)
4851
url_source.save_to_file(output_file_path)
49-
assert os.path.exists(os.path.join(output_file_path, "invoice_5p.pdf"))
52+
assert os.path.exists(os.path.join(output_file_path, "blank_1.pdf"))
5053

5154

5255
@pytest.mark.integration
@@ -59,4 +62,4 @@ def test_save_file_with_filename(client, reference_file_path, output_file_path):
5962
@pytest.fixture(autouse=True)
6063
def cleanup():
6164
yield
62-
cleanup_output_files(["invoice_5p.pdf", "customFileName.pdf"])
65+
cleanup_output_files(["blank_1.pdf", "customFileName.pdf"])

tests/v2/test_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ def test_get_inference(custom_base_url_client):
184184
_assert_findoc_inference(response)
185185

186186

187+
@pytest.mark.v2
188+
def test_get_inference_by_url(custom_base_url_client):
189+
response = custom_base_url_client.get_result(
190+
ExtractionResponse,
191+
"https://api-v2.mindee.net/v2/inference/12345678-1234-1234-1234-123456789ABC",
192+
)
193+
_assert_findoc_inference(response)
194+
195+
187196
@pytest.mark.v2
188197
def test_error_handling(custom_base_url_client):
189198
with pytest.raises(MindeeHTTPErrorV2) as e:

0 commit comments

Comments
 (0)