Skip to content

Commit 9a1607a

Browse files
committed
📝 add asynchronous code sample for material certificate v1
1 parent 8b47d9a commit 9a1607a

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from time import sleep
2+
3+
from mindee import Client
4+
from mindee.documents import TypeMaterialCertificateV1
5+
6+
7+
# Init a new client
8+
mindee_client = Client(api_key="my-api-key")
9+
10+
# Load a file from disk
11+
input_doc = mindee_client.doc_from_path("/path/to/the/file.ext")
12+
13+
# Limit the amount of API calls to retrieve your document
14+
MAX_RETRIES = 10
15+
16+
# How many seconds to wait in-between tries
17+
INTERVAL_SECS = 6
18+
19+
20+
# Recursive function that tries to retrieve the completed document.
21+
# If the document is not "completed", try again
22+
def get_doc_from_async_queue(queue_id, times_tried=0):
23+
24+
# Have we exceeded our retry count?
25+
if times_tried >= MAX_RETRIES:
26+
raise Exception(f"Maximum retries reached {times_tried}")
27+
28+
# Wait for a few seconds before fetching
29+
sleep(INTERVAL_SECS)
30+
31+
# Fetch and parse the result, using the same type
32+
parsed_result = input_doc.parse_queued(TypeMaterialCertificateV1, queue_id)
33+
34+
# Check whether the result is ready
35+
if parsed_result.job.status == "completed":
36+
return parsed_result
37+
38+
# Otherwise, try again...
39+
else:
40+
get_doc_from_async_queue(queue_id, times_tried+1)
41+
42+
43+
# Add the file to the prediction queue
44+
queue_result = input_doc.enqueue(TypeMaterialCertificateV1)
45+
46+
# Get the queue's job ID
47+
job_id = queue_result.job.id
48+
49+
# Start the recursion, passing the job id
50+
parsed_result = get_doc_from_async_queue(queue_result.job.id)
51+
52+
# Print a brief summary of the parsed data
53+
print(parsed_result.document.document)

mindee/documents/material_certificate/material_certificate_v1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@ def __str__(self) -> str:
6969

7070

7171
TypeMaterialCertificateV1 = TypeVar(
72-
"TypeMaterialCertificateV1", bound=MaterialCertificateV1
72+
"TypeMaterialCertificateV1",
73+
bound=MaterialCertificateV1,
7374
)

tests/documents/test_material_certificate_v1.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515

1616
@pytest.fixture
1717
def material_certificate_v1_doc() -> MaterialCertificateV1:
18-
json_data = json.load(open(FILE_PATH_MATERIAL_CERTIFICATE_V1_COMPLETE))
18+
json_data = json.load(
19+
open(FILE_PATH_MATERIAL_CERTIFICATE_V1_COMPLETE, encoding="utf-8")
20+
)
1921
return MaterialCertificateV1(json_data["document"]["inference"], page_n=None)
2022

2123

2224
@pytest.fixture
2325
def material_certificate_v1_doc_empty() -> MaterialCertificateV1:
24-
json_data = json.load(open(FILE_PATH_MATERIAL_CERTIFICATE_V1_EMPTY))
26+
json_data = json.load(
27+
open(FILE_PATH_MATERIAL_CERTIFICATE_V1_EMPTY, encoding="utf-8")
28+
)
2529
return MaterialCertificateV1(json_data["document"]["inference"], page_n=None)
2630

2731

0 commit comments

Comments
 (0)