Polling task retrieval
This example demonstrates how to use the Python requests library to integrate the Koncile API. The script illustrates the following steps:
- File Upload: The file is sent to the API via a dedicated endpoint, along with the necessary parameters such as the extraction template and the folder ID. 
- Retrieval of the Task ID: Once the file is processed, the task ID generated by the API is used to track its status. 
- 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, folder-id, template-id, path-to-file) with your own data.
import requests
import time
api_key = "your-api-key"
api_url = "https://api.koncile.ai"
upload_url = f"{api_url}/v1/upload_file/"
fetch_url = f"{api_url}/v1/fetch_tasks_results/"
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={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"])Last updated
Was this helpful?

