Skip to content

Commit 7bacd32

Browse files
committed
[IMP] extract_api: improve implementation example
- Since the example only works for invoices, references to the other document types supported by the OCR have been removed. - Handle the case where library `requests` isn't available. - Show additional fields detected by the OCR. - Properly set the ID of the JSON-RPC request. - Commit 8c93ff7 should have adapted the implementation example with the latest API version. X-original-commit: 56fa6da Part-of: #12278 Signed-off-by: Louis Baudoux (lba) <[email protected]>
1 parent 9f421b7 commit 7bacd32

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

content/developer/reference/extract_api/implementation.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22
import json
33
import sys
44
import time
5+
import uuid
56

6-
import requests
7+
try:
8+
import requests
9+
except ImportError:
10+
print("The 'requests' library is required to run this script. More information at https://pypi.org/project/requests.")
11+
exit()
712

813
account_token = "integration_token" # Use your token
914
domain_name = "https://extract.api.odoo.com"
1015
path_to_pdf = "/path/to/your/pdf"
11-
doc_type = "invoice" # invoice, expense or applicant
1216

13-
# Do not change
14-
API_VERSION = {
15-
'invoice': 122,
16-
'expense': 132,
17-
'applicant': 102,
18-
}
1917

2018
def extract_jsonrpc_call(path: str, params: dict):
2119
payload = {
2220
'jsonrpc': '2.0',
2321
'method': 'call',
2422
'params': params,
25-
'id': 0, # This should be unique for each call
23+
'id': uuid.uuid4().hex, # This should be unique for each call
2624
}
2725
response = requests.post(domain_name + path, json=payload, timeout=10)
2826
response.raise_for_status()
@@ -35,20 +33,20 @@ def send_document_to_extract(doc_path: str):
3533
encoded_doc = base64.b64encode(f.read()).decode()
3634
params = {
3735
'account_token': account_token,
38-
'version': API_VERSION[doc_type],
36+
'version': 123,
3937
'documents': [encoded_doc],
4038
}
41-
response = extract_jsonrpc_call(f"/api/extract/{doc_type}/2/parse", params)
39+
response = extract_jsonrpc_call(f"/api/extract/invoice/2/parse", params)
4240
return response
4341

4442

4543
def get_result_from_extract(document_token: str):
4644
params = {
47-
'version': API_VERSION[doc_type],
45+
'version': 123,
4846
'document_token': document_token,
4947
'account_token': account_token,
5048
}
51-
endpoint = f"/api/extract/{doc_type}/2/get_result"
49+
endpoint = f"/api/extract/invoice/2/get_result"
5250
response = extract_jsonrpc_call(endpoint, params)
5351
while response['result']['status'] == 'processing':
5452
print("Still processing... Retrying in 5 seconds")
@@ -83,8 +81,14 @@ def get_result_from_extract(document_token: str):
8381

8482
document_results = response['result']['results'][0]
8583

86-
print("\nTotal:", document_results['total']['selected_value']['content'])
87-
print("Subtotal:", document_results['subtotal']['selected_value']['content'])
88-
print("Invoice id:", document_results['invoice_id']['selected_value']['content'])
89-
print("Date:", document_results['date']['selected_value']['content'])
90-
print("...\n")
84+
85+
def _get_selected_value(field):
86+
return document_results.get(field, {}).get('selected_value', {}).get('content', '')
87+
88+
89+
print("\nTotal:", _get_selected_value('total'))
90+
print("Subtotal:", _get_selected_value('subtotal'))
91+
print("Reference:", _get_selected_value('invoice_id'))
92+
print("Date:", _get_selected_value('date'))
93+
print("Due date:", _get_selected_value('due_date'))
94+
print("Currency:", _get_selected_value('currency'))

0 commit comments

Comments
 (0)