Skip to content

Commit

Permalink
✨ add default non-sdk async sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianMindee committed Oct 18, 2023
1 parent 60568fd commit 605bd9b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/extras/code_samples/default_async.txt
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))
7 changes: 7 additions & 0 deletions tests/test_code_samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ do
sed -i "s/my-version/1/" $OUTPUT_FILE
fi

if echo "${f}" | grep -q "default_async.txt"
then
sed -i "s/my-account/mindee/" $OUTPUT_FILE
sed -i "s/my-endpoint/invoice_splitter/" $OUTPUT_FILE
sed -i "s/my-version/1/" $OUTPUT_FILE
fi

python $OUTPUT_FILE
done

0 comments on commit 605bd9b

Please sign in to comment.