Exemple python code

Exemple of a python code

This example demonstrates how to use the Python requests library to integrate the Koncile API. The script illustrates the following steps:

  1. File Upload: The file is sent to the API via a dedicated endpoint, along with the necessary parameters such as the file type and the use case ID.
  2. Retrieval of the Task ID: Once the file is processed, the task ID generated by the API is used to track its status.
  3. Task Processing Monitoring: A polling function periodically checks whether the task is completed before extracting the returned information.

This script can serve as a starting point for integrating Koncile into your automated systems. Be sure to replace placeholder values (e.g., your-api-key, use-case-id, file-type-id, path-to-file) with your own data.

import requests
import time

api_key = "your-api-key"

upload_url = "http://localhost:8000/buffer/upload_file/"
fetch_url = "http://localhost:8000/tasks/"
params = {"config_id": 'use-case-id', "class_id": 'file-type-id'}
file_path = './path-to-file'
headers = {
    "accept": "application/json",
    "Authorization": f"Bearer {api_key}"
}
files = [
    ('files', open(file_path, 'rb')),
]
response = requests.post(upload_url, params=params, headers=headers, files=files)

if response.status_code != 200:
    print(response.status_code)
    print(response.json())
    exit(1)

task_id = response.json()["task_ids"][0]

def poll_until_done(task_id, headers):
    while True:
        response = requests.get(f"{fetch_url}{task_id}", headers=headers)
        status = response.json()["status"]
        if status != "IN PROGRESS":
            return response
        print("Task in progress")
        time.sleep(2)

response = poll_until_done(task_id, headers)
print(response.json()["status"])
print(response.json()["status_message"])

print(response.json()["General_fields"])
print(response.json()["Line_fields"])

Raphaël Balbous

Technical Lead

Related questions

No items found.