Skip to content

Commit bed00f8

Browse files
✨ add support for page count, mimetype + fixes (#346)
1 parent ee520c9 commit bed00f8

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

mindee/parsing/v2/inference.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,9 @@ def __init__(self, raw_response: StringDict):
2525
self.id = raw_response["id"] if "id" in raw_response else None
2626

2727
def __str__(self) -> str:
28-
alias = f" {self.file.alias}" if self.file.alias else ""
2928
return (
30-
f"Inference\n#########"
31-
f"\nModel\n====="
32-
f"\n:ID: {self.model.id}"
33-
f"\n\nFile\n===="
34-
f"\n:Name: {self.file.name}"
35-
f"\n:Alias:{alias}"
36-
f"{self.result}"
37-
"\n"
29+
f"Inference\n#########\n"
30+
f"{self.model}\n\n"
31+
f"{self.file}"
32+
f"{self.result}\n"
3833
)

mindee/parsing/v2/inference_file.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@ class InferenceFile:
66

77
name: str
88
"""Name of the file."""
9-
alais: str
9+
alias: str
1010
"""Alias of the file."""
11+
page_count: str
12+
"""Number of pages in the file."""
13+
mime_type: str
14+
"""Mime type of the file."""
1115

1216
def __init__(self, raw_response: StringDict) -> None:
1317
self.name = raw_response["name"]
1418
self.alias = raw_response["alias"]
19+
self.page_count = raw_response["page_count"]
20+
self.mime_type = raw_response["mime_type"]
21+
22+
def __str__(self) -> str:
23+
return (
24+
f"File\n===="
25+
f"\n:Name: {self.name}"
26+
f"\n:Alias:{self.alias if self.alias else ''}"
27+
f"\n:Page Count: {self.page_count}"
28+
f"\n:MIME Type: {self.mime_type}"
29+
)

mindee/parsing/v2/inference_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ class InferenceModel:
99

1010
def __init__(self, raw_response: StringDict) -> None:
1111
self.id = raw_response["id"]
12+
13+
def __str__(self) -> str:
14+
return f"Model\n=====" f"\n:ID: {self.id}"

mindee/parsing/v2/job.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from mindee.parsing.common.string_dict import StringDict
55
from mindee.parsing.v2.error_response import ErrorResponse
6+
from mindee.parsing.v2.job_webhook import JobWebhook
67

78

89
class Job:
@@ -26,7 +27,7 @@ class Job:
2627
"""URL to poll for the job status."""
2728
result_url: Optional[str]
2829
"""URL to poll for the job result, redirects to the result if available."""
29-
webhooks: List[str]
30+
webhooks: List[JobWebhook]
3031
"""ID of webhooks associated with the job."""
3132

3233
def __init__(self, raw_response: StringDict) -> None:
@@ -43,4 +44,6 @@ def __init__(self, raw_response: StringDict) -> None:
4344
self.filename = raw_response["filename"]
4445
self.result_url = raw_response["result_url"]
4546
self.alias = raw_response["alias"]
46-
self.webhooks = raw_response["webhooks"]
47+
self.webhooks = []
48+
for webhook in raw_response["webhooks"]:
49+
self.webhooks.append(JobWebhook(webhook))

mindee/parsing/v2/job_webhook.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from datetime import datetime
2+
from typing import Optional
3+
4+
from mindee.parsing.common.string_dict import StringDict
5+
from mindee.parsing.v2.error_response import ErrorResponse
6+
7+
8+
class JobWebhook:
9+
"""JobWebhook information."""
10+
11+
id: str
12+
"""JobWebhook ID."""
13+
created_at: Optional[datetime]
14+
"""Created at date."""
15+
status: str
16+
"""Status of the webhook."""
17+
error: Optional[ErrorResponse]
18+
"""Error response, if any."""
19+
20+
def __init__(self, server_response: StringDict) -> None:
21+
self.id = server_response["id"]
22+
self.created_at = self.parse_date(server_response.get("created_at"))
23+
self.status = server_response["status"]
24+
self.error = (
25+
ErrorResponse(server_response["error"])
26+
if server_response.get("error") is not None
27+
else None
28+
)
29+
30+
@staticmethod
31+
def parse_date(date_string: Optional[str]) -> Optional[datetime]:
32+
"""Parse the date, if present."""
33+
if not date_string:
34+
return None
35+
date_string = date_string.replace("Z", "+00:00")
36+
return datetime.fromisoformat(date_string)

tests/v2/test_inference_response.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,7 @@ def test_full_inference_response():
181181

182182
assert isinstance(inference_result.inference.file, InferenceFile)
183183
assert inference_result.inference.file.name == "complete.jpg"
184+
assert inference_result.inference.file.page_count == 1
185+
assert inference_result.inference.file.mime_type == "image/jpeg"
184186
assert not inference_result.inference.file.alias
185187
assert not inference_result.inference.result.options

0 commit comments

Comments
 (0)