Skip to content

Commit 715f177

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

4 files changed

Lines changed: 58 additions & 0 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/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)