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
2 changes: 1 addition & 1 deletion mindee/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
def main() -> None:
"""Run the Command Line Interface."""
parser = MindeeParser()
parser.call_endpoint()
parser.call_parse()
100 changes: 7 additions & 93 deletions mindee/commands/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
from mindee.input.sources import LocalInputSource, UrlInputSource
from mindee.parsing.common.async_predict_response import AsyncPredictResponse
from mindee.parsing.common.document import Document, serialize_for_json
from mindee.parsing.common.feedback_response import FeedbackResponse
from mindee.parsing.common.inference import Inference
from mindee.parsing.common.predict_response import PredictResponse
from mindee.parsing.common.string_dict import StringDict


class MindeeArgumentParser(ArgumentParser):
Expand Down Expand Up @@ -75,19 +73,6 @@ def add_sending_options(self) -> None:
)
self.add_argument(dest="path", help="Full path to the file")

def add_feedback_options(self) -> None:
"""Adds the option to give feedback manually."""
self.add_argument(
dest="document_id",
help="Mindee UUID of the document.",
type=str,
)
self.add_argument(
dest="feedback",
type=json.loads,
help='Feedback JSON string to send, ex \'{"key": "value"}\'.',
)

def add_custom_options(self) -> None:
"""Adds options to custom-type documents."""
self.add_argument(
Expand Down Expand Up @@ -128,8 +113,6 @@ class MindeeParser:
"""Document to be parsed."""
product_class: Type[Inference]
"""Product to parse."""
feedback: Optional[StringDict]
"""Dict representation of a feedback."""

def __init__(
self,
Expand All @@ -147,38 +130,11 @@ def __init__(
else:
api_key = self.parsed_args.api_key if "api_key" in self.parsed_args else ""
self.client = Client(api_key=api_key)
self._set_input()
self.input_doc = self._get_input_doc()
self.document_info = (
document_info if document_info else PRODUCTS[self.parsed_args.product_name]
)

def call_endpoint(self) -> None:
"""Calls the proper type of endpoint according to given command."""
if self.parsed_args.parse_type == "parse":
self.call_parse()
else:
self.call_feedback()

def call_feedback(self) -> None:
"""Sends feedback to an API."""
custom_endpoint: Optional[Endpoint] = None
if self.parsed_args.product_name in ("custom", "generated"):
custom_endpoint = self.client.create_endpoint(
self.parsed_args.endpoint_name,
self.parsed_args.account_name,
self.parsed_args.api_version,
)
if self.feedback is None:
raise MindeeClientError("Invalid feedback provided.")

response: FeedbackResponse = self.client.send_feedback(
self.document_info.doc_class,
self.parsed_args.document_id,
{"feedback": self.feedback},
custom_endpoint,
)
print(json.dumps(response.feedback, indent=2))

def call_parse(self) -> None:
"""Calls an endpoint with the appropriate method, and displays the results."""
response: Union[PredictResponse, AsyncPredictResponse]
Expand Down Expand Up @@ -277,19 +233,13 @@ def _set_args(self) -> Namespace:
for name, info in PRODUCTS.items():
parse_subparser = parse_product_subparsers.add_parser(name, help=info.help)

call_parser = parse_subparser.add_subparsers(
dest="parse_type", required=True
)
parse_subp = call_parser.add_parser("parse")
feedback_subp = call_parser.add_parser("feedback")

parse_subp.add_main_options()
parse_subp.add_sending_options()
parse_subp.add_display_options()
parse_subparser.add_main_options()
parse_subparser.add_sending_options()
parse_subparser.add_display_options()
if name in ("custom", "generated"):
parse_subp.add_custom_options()
parse_subparser.add_custom_options()
else:
parse_subp.add_argument(
parse_subparser.add_argument(
"-t",
"--full-text",
dest="include_words",
Expand All @@ -298,7 +248,7 @@ def _set_args(self) -> Namespace:
)

if info.is_async and info.is_sync:
parse_subp.add_argument(
parse_subparser.add_argument(
"-A",
"--asynchronous",
dest="async_parse",
Expand All @@ -308,9 +258,6 @@ def _set_args(self) -> Namespace:
default=False,
)

feedback_subp.add_main_options()
feedback_subp.add_feedback_options()

parsed_args = self.parser.parse_args()
return parsed_args

Expand All @@ -332,36 +279,3 @@ def _get_input_doc(self) -> Union[LocalInputSource, UrlInputSource]:
elif self.parsed_args.input_type == "url":
return self.client.source_from_url(self.parsed_args.path)
return self.client.source_from_path(self.parsed_args.path)

def _get_feedback_doc(self) -> StringDict:
"""Loads a feedback."""
json_doc: StringDict = {}
if self.parsed_args.input_type == "file":
with open(self.parsed_args.path, "rb", buffering=30) as f_f:
json_doc = json.loads(f_f.read())
elif self.parsed_args.input_type == "base64":
with open(self.parsed_args.path, "rt", encoding="ascii") as f_b64:
json_doc = json.loads(f_b64.read())
elif self.parsed_args.input_type == "bytes":
with open(self.parsed_args.path, "rb") as f_b:
json_doc = json.loads(f_b.read())
else:
if (
not self.parsed_args.feedback
or "feedback" not in self.parsed_args.feedback
):
raise MindeeClientError("Invalid feedback.")
if not json_doc or "feedback" not in json_doc:
raise MindeeClientError("Invalid feedback.")
return json_doc

def _set_input(self) -> None:
"""Loads an input document, or a feedback document."""
self.feedback = None
if self.parsed_args.parse_type == "feedback":
if not self.parsed_args.feedback:
self.feedback = self._get_feedback_doc()
else:
self.feedback = self.parsed_args.feedback
else:
self.input_doc = self._get_input_doc()
14 changes: 9 additions & 5 deletions mindee/mindee_http/mindee_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ def __init__(
self,
api_key: Optional[str],
):
self.api_key = api_key
if not self.api_key or len(self.api_key) == 0:
self.api_key = (
api_key
if api_key
else os.environ.get(API_KEY_V2_ENV_NAME, API_KEY_V2_DEFAULT)
)
self.request_timeout = TIMEOUT_DEFAULT
self.set_base_url(BASE_URL_DEFAULT)
self.set_from_env()
if not self.api_key:
raise MindeeApiV2Error(
(
f"Missing API key,"
Expand All @@ -42,9 +49,6 @@ def __init__(
f"'{API_KEY_V2_ENV_NAME}' environment variable."
)
)
self.request_timeout = TIMEOUT_DEFAULT
self.set_base_url(BASE_URL_DEFAULT)
self.set_from_env()
self.url_root = f"{self.base_url.rstrip('/')}"

@property
Expand Down
44 changes: 16 additions & 28 deletions tests/v1/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from mindee.commands.cli_parser import MindeeParser
from mindee.error.mindee_http_error import MindeeHTTPClientError, MindeeHTTPError
from mindee.error.mindee_http_error import MindeeHTTPError
from tests.utils import FILE_TYPES_DIR, V1_PRODUCT_DATA_DIR, clear_envvars


Expand Down Expand Up @@ -124,100 +124,88 @@ def ots_doc_feedback(monkeypatch):
def test_cli_custom_doc(custom_doc):
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=custom_doc)
parser.call_endpoint()
parser.call_parse()


def test_cli_generated_doc_sync(generated_doc_sync):
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=generated_doc_sync)
parser.call_endpoint()
parser.call_parse()


def test_cli_generated_doc_async(generated_doc_async):
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=generated_doc_async)
parser.call_endpoint()
parser.call_parse()


def test_cli_invoice(ots_doc):
ots_doc.product_name = "invoice"
ots_doc.api_key = ""
with pytest.raises(RuntimeError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()
ots_doc.api_key = "dummy"
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()


def test_cli_receipt(ots_doc):
ots_doc.product_name = "receipt"
ots_doc.api_key = ""
with pytest.raises(RuntimeError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()
ots_doc.api_key = "dummy"
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()


def test_cli_financial_doc(ots_doc):
ots_doc.product_name = "financial-document"
ots_doc.api_key = ""
with pytest.raises(RuntimeError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()
ots_doc.api_key = "dummy"
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()


def test_cli_passport(ots_doc):
ots_doc.product_name = "passport"
ots_doc.api_key = ""
with pytest.raises(RuntimeError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()
ots_doc.api_key = "dummy"
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()


def test_cli_us_bank_check(ots_doc):
ots_doc.product_name = "us-bank-check"
ots_doc.api_key = ""
with pytest.raises(RuntimeError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()
ots_doc.api_key = "dummy"
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=ots_doc)
parser.call_endpoint()
parser.call_parse()


def test_cli_invoice_splitter_enqueue(ots_doc_enqueue_and_parse):
ots_doc_enqueue_and_parse.product_name = "invoice-splitter"
ots_doc_enqueue_and_parse.api_key = ""
with pytest.raises(RuntimeError):
parser = MindeeParser(parsed_args=ots_doc_enqueue_and_parse)
parser.call_endpoint()
parser.call_parse()
ots_doc_enqueue_and_parse.api_key = "dummy"
with pytest.raises(MindeeHTTPError):
parser = MindeeParser(parsed_args=ots_doc_enqueue_and_parse)
parser.call_endpoint()


def test_cli_feedback(ots_doc_feedback):
ots_doc_feedback.document_id = "dummy-document-id"
ots_doc_feedback.api_key = ""
with pytest.raises(RuntimeError):
parser = MindeeParser(parsed_args=ots_doc_feedback)
parser.call_endpoint()
ots_doc_feedback.api_key = "dummy"
with pytest.raises(MindeeHTTPClientError):
parser = MindeeParser(parsed_args=ots_doc_feedback)
parser.call_endpoint()
parser.call_parse()
2 changes: 1 addition & 1 deletion tests/v2/input/test_local_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def file_path() -> Path:

def _assert_local_response(local_response):
fake_hmac_signing = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"
signature = "a1bc9012fa63539d602f163d8980604a0cf2b2ae88e56009cfa1db33382736cf"
signature = "b9c2dfc67c2ba457603dd9880d45f089ae79b95bf6389b4a4387ef255c924fe7"

assert local_response._file is not None
assert not local_response.is_valid_hmac_signature(
Expand Down
Loading