Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions mindee/client_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from mindee.error.mindee_error import MindeeError
from mindee.error.mindee_http_error_v2 import handle_error_v2
from mindee.input.inference_parameters import InferenceParameters
from mindee.input.local_response import LocalResponse
from mindee.input.polling_options import PollingOptions
from mindee.input.sources.local_input_source import LocalInputSource
from mindee.logger import logger
Expand Down Expand Up @@ -137,16 +136,3 @@ def enqueue_and_get_inference(
sleep(params.polling_options.delay_sec)

raise MindeeError(f"Couldn't retrieve document after {try_counter + 1} tries.")

@staticmethod
def load_inference(local_response: LocalResponse) -> InferenceResponse:
"""
Load a prediction from the V2 API.

:param local_response: Local response to load.
:return: A valid prediction.
"""
try:
return InferenceResponse(local_response.as_dict)
except KeyError as exc:
raise MindeeError("No prediction found in local response.") from exc
16 changes: 15 additions & 1 deletion mindee/input/local_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import json
import os
from pathlib import Path
from typing import Any, BinaryIO, Dict, Union
from typing import Any, BinaryIO, Dict, Type, TypeVar, Union

from mindee.error.mindee_error import MindeeError
from mindee.parsing.v2.common_response import CommonResponse


class LocalResponse:
Expand Down Expand Up @@ -102,3 +103,16 @@ def is_valid_hmac_signature(
:return: True if the HMAC signature is valid.
"""
return signature == self.get_hmac_signature(secret_key)

ResponseT = TypeVar("ResponseT", bound=CommonResponse)

def deserialize_response(self, response_class: Type[ResponseT]) -> ResponseT:
"""
Load a local inference.

Typically used when wanting to load a V2 webhook callback.
"""
try:
return response_class(self.as_dict)
except KeyError as exc:
raise MindeeError("Invalid class specified for deserialization.") from exc
8 changes: 5 additions & 3 deletions tests/test_client_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from mindee import ClientV2, InferenceParameters, InferenceResponse, LocalResponse
from mindee.error.mindee_error import MindeeApiV2Error
from mindee.error.mindee_error import MindeeApiV2Error, MindeeError
from mindee.error.mindee_http_error_v2 import MindeeHTTPErrorV2
from mindee.input import LocalInputSource, PathInput
from mindee.mindee_http.base_settings import USER_AGENT
Expand Down Expand Up @@ -140,12 +140,14 @@ def _assert_findoc_inference(response: InferenceResponse):


@pytest.mark.v2
def test_loads_from_prediction(env_client):
def test_loads_from_prediction():
input_inference = LocalResponse(
V2_DATA_DIR / "products" / "financial_document" / "complete.json"
)
response = env_client.load_inference(input_inference)
response = input_inference.deserialize_response(InferenceResponse)
_assert_findoc_inference(response)
with pytest.raises(MindeeError):
input_inference.deserialize_response(JobResponse)


@pytest.mark.v2
Expand Down