"""Submit an invoice and wait for it — the polling path, for when webhooks are not set up.""" import os import time import httpx API = "https://api.fintray.ro/v1" HEADERS = {"Authorization": f"Bearer {os.environ['FINTRAY_KEY']}"} def extract(path: str, buyer_vat: str, external_id: str) -> dict: """`buyer_vat` is your customer's VAT number — the company the invoice was sent to.""" with open(path, "rb") as f: data = {"buyer_vat": buyer_vat, "external_id": external_id} r = httpx.post(f"{API}/documents", headers={**HEADERS, "Idempotency-Key": external_id}, files={"file": (os.path.basename(path), f)}, data=data, timeout=60) r.raise_for_status() doc = r.json() while doc["status"] in ("queued", "processing"): time.sleep(2) doc = httpx.get(f"{API}/documents/{doc['id']}", headers=HEADERS, timeout=30).json() if doc["status"] == "failed": raise RuntimeError(doc["error"]) return doc # doc["document"], doc["fields"], doc["warnings"]