Skip to content

Commit 1f02361

Browse files
authored
✨ add generic response loader (#341)
1 parent da49749 commit 1f02361

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

mindee/client_v2.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from mindee.error.mindee_error import MindeeError
66
from mindee.error.mindee_http_error_v2 import handle_error_v2
77
from mindee.input.inference_parameters import InferenceParameters
8-
from mindee.input.local_response import LocalResponse
98
from mindee.input.polling_options import PollingOptions
109
from mindee.input.sources.local_input_source import LocalInputSource
1110
from mindee.logger import logger
@@ -137,16 +136,3 @@ def enqueue_and_get_inference(
137136
sleep(params.polling_options.delay_sec)
138137

139138
raise MindeeError(f"Couldn't retrieve document after {try_counter + 1} tries.")
140-
141-
@staticmethod
142-
def load_inference(local_response: LocalResponse) -> InferenceResponse:
143-
"""
144-
Load a prediction from the V2 API.
145-
146-
:param local_response: Local response to load.
147-
:return: A valid prediction.
148-
"""
149-
try:
150-
return InferenceResponse(local_response.as_dict)
151-
except KeyError as exc:
152-
raise MindeeError("No prediction found in local response.") from exc

mindee/input/local_response.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import json
55
import os
66
from pathlib import Path
7-
from typing import Any, BinaryIO, Dict, Union
7+
from typing import Any, BinaryIO, Dict, Type, TypeVar, Union
88

99
from mindee.error.mindee_error import MindeeError
10+
from mindee.parsing.v2.common_response import CommonResponse
1011

1112

1213
class LocalResponse:
@@ -102,3 +103,16 @@ def is_valid_hmac_signature(
102103
:return: True if the HMAC signature is valid.
103104
"""
104105
return signature == self.get_hmac_signature(secret_key)
106+
107+
ResponseT = TypeVar("ResponseT", bound=CommonResponse)
108+
109+
def deserialize_response(self, response_class: Type[ResponseT]) -> ResponseT:
110+
"""
111+
Load a local inference.
112+
113+
Typically used when wanting to load a V2 webhook callback.
114+
"""
115+
try:
116+
return response_class(self.as_dict)
117+
except KeyError as exc:
118+
raise MindeeError("Invalid class specified for deserialization.") from exc

tests/test_client_v2.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from mindee import ClientV2, InferenceParameters, InferenceResponse, LocalResponse
6-
from mindee.error.mindee_error import MindeeApiV2Error
6+
from mindee.error.mindee_error import MindeeApiV2Error, MindeeError
77
from mindee.error.mindee_http_error_v2 import MindeeHTTPErrorV2
88
from mindee.input import LocalInputSource, PathInput
99
from mindee.mindee_http.base_settings import USER_AGENT
@@ -140,12 +140,14 @@ def _assert_findoc_inference(response: InferenceResponse):
140140

141141

142142
@pytest.mark.v2
143-
def test_loads_from_prediction(env_client):
143+
def test_loads_from_prediction():
144144
input_inference = LocalResponse(
145145
V2_DATA_DIR / "products" / "financial_document" / "complete.json"
146146
)
147-
response = env_client.load_inference(input_inference)
147+
response = input_inference.deserialize_response(InferenceResponse)
148148
_assert_findoc_inference(response)
149+
with pytest.raises(MindeeError):
150+
input_inference.deserialize_response(JobResponse)
149151

150152

151153
@pytest.mark.v2

0 commit comments

Comments
 (0)