From a076c63527de1e955b49e8478bffbe0aa9894b01 Mon Sep 17 00:00:00 2001 From: Louis Baudoux Date: Mon, 3 Mar 2025 14:46:17 +0000 Subject: [PATCH] [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: 56fa6da441b81efb7cbd33d31b7adfd78e3f1f94 Part-of: odoo/documentation#12284 Signed-off-by: Louis Baudoux (lba) --- .../reference/extract_api/implementation.py | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/content/developer/reference/extract_api/implementation.py b/content/developer/reference/extract_api/implementation.py index 3392f4ee5..243590a03 100644 --- a/content/developer/reference/extract_api/implementation.py +++ b/content/developer/reference/extract_api/implementation.py @@ -2,27 +2,25 @@ import base64 import json import sys import time +import uuid -import requests +try: + import requests +except ImportError: + print("The 'requests' library is required to run this script. More information at https://pypi.org/project/requests.") + exit() account_token = "integration_token" # Use your token domain_name = "https://extract.api.odoo.com" path_to_pdf = "/path/to/your/pdf" -doc_type = "invoice" # invoice, expense or applicant -# Do not change -API_VERSION = { - 'invoice': 122, - 'expense': 132, - 'applicant': 102, -} def extract_jsonrpc_call(path: str, params: dict): payload = { 'jsonrpc': '2.0', 'method': 'call', 'params': params, - 'id': 0, # This should be unique for each call + 'id': uuid.uuid4().hex, # This should be unique for each call } response = requests.post(domain_name + path, json=payload, timeout=10) response.raise_for_status() @@ -35,20 +33,20 @@ def send_document_to_extract(doc_path: str): encoded_doc = base64.b64encode(f.read()).decode() params = { 'account_token': account_token, - 'version': API_VERSION[doc_type], + 'version': 123, 'documents': [encoded_doc], } - response = extract_jsonrpc_call(f"/api/extract/{doc_type}/2/parse", params) + response = extract_jsonrpc_call(f"/api/extract/invoice/2/parse", params) return response def get_result_from_extract(document_token: str): params = { - 'version': API_VERSION[doc_type], + 'version': 123, 'document_token': document_token, 'account_token': account_token, } - endpoint = f"/api/extract/{doc_type}/2/get_result" + endpoint = f"/api/extract/invoice/2/get_result" response = extract_jsonrpc_call(endpoint, params) while response['result']['status'] == 'processing': print("Still processing... Retrying in 5 seconds") @@ -83,8 +81,14 @@ if __name__ == '__main__': document_results = response['result']['results'][0] - print("\nTotal:", document_results['total']['selected_value']['content']) - print("Subtotal:", document_results['subtotal']['selected_value']['content']) - print("Invoice id:", document_results['invoice_id']['selected_value']['content']) - print("Date:", document_results['date']['selected_value']['content']) - print("...\n") + + def _get_selected_value(field): + return document_results.get(field, {}).get('selected_value', {}).get('content', '') + + + print("\nTotal:", _get_selected_value('total')) + print("Subtotal:", _get_selected_value('subtotal')) + print("Reference:", _get_selected_value('invoice_id')) + print("Date:", _get_selected_value('date')) + print("Due date:", _get_selected_value('due_date')) + print("Currency:", _get_selected_value('currency'))