-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add default non-sdk async sample code
- Loading branch information
1 parent
60568fd
commit 605bd9b
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import json | ||
import requests | ||
from time import sleep | ||
|
||
api_key = "my-api-key" | ||
account = "my-account" | ||
endpoint = "my-endpoint" | ||
version = "my-version" | ||
|
||
url_enqueue = f"https://api.mindee.net/v1/products/{account}/{endpoint}/v{version}/predict_async" | ||
headers = {"Authorization": f"Token {api_key}"} | ||
|
||
with open("/path/to/the/file.ext", "rb") as file_handle: | ||
files = {"document": file_handle} | ||
response_enqueue = requests.post(url_enqueue, files=files, headers=headers) | ||
|
||
json_response_enqueue = response_enqueue.json() | ||
|
||
if not response_enqueue.ok: | ||
raise RuntimeError(json_response_enqueue["api_request"]["error"]) | ||
|
||
job_id = json_response_enqueue["job"]["id"] | ||
|
||
url_parse_queued = f"https://api.mindee.net/v1/products/{account}/{endpoint}/v{version}/documents/queue/{job_id}" | ||
sleep(4) | ||
response_parse = requests.get(url_parse_queued, headers=headers) | ||
tries = 1 | ||
while tries < 10: | ||
if not response_parse.ok: | ||
raise RuntimeError(json_response_enqueue["api_request"]["error"]) | ||
|
||
response_parse = requests.get(url_parse_queued, headers=headers) | ||
json_response_parse = response_parse.json() | ||
if json_response_parse["job"]["status"] == "completed": | ||
break | ||
else: | ||
print(json_response_parse["job"]) | ||
|
||
tries += 1 | ||
sleep(2) | ||
|
||
if json_response_parse["job"]["status"] != "completed": | ||
raise RuntimeError(f"Async parsing timed out after {tries} tries") | ||
|
||
print(json.dumps(json_response_parse["document"], indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters