| 1 | """ |
| 2 | Further AI API — End-to-end quickstart |
| 3 | """ |
| 4 | import time |
| 5 | import requests |
| 6 | |
| 7 | # ── Configuration ──────────────────────────────────────────── |
| 8 | CLIENT_ID = "YOUR_CLIENT_ID" |
| 9 | CLIENT_SECRET = "YOUR_CLIENT_SECRET" |
| 10 | WORKFLOW_ID = "YOUR_WORKFLOW_ID" |
| 11 | FILE_PATH = "policy.pdf" |
| 12 | BASE_URL = "https://api.further.ai" |
| 13 | POLL_INTERVAL = 5 # seconds |
| 14 | MAX_WAIT = 600 # seconds |
| 15 | |
| 16 | # ── 1. Authenticate ───────────────────────────────────────── |
| 17 | print("Generating access token...") |
| 18 | token_resp = requests.post( |
| 19 | f"{BASE_URL}/api/v1/oauth2/access_token", |
| 20 | json={"client_id": CLIENT_ID, "client_secret": CLIENT_SECRET} |
| 21 | ) |
| 22 | token_resp.raise_for_status() |
| 23 | access_token = token_resp.json()["access_token"] |
| 24 | print(f" Token obtained (expires in 60 min)") |
| 25 | |
| 26 | headers = {"Authorization": f"Bearer {access_token}"} |
| 27 | |
| 28 | # ── 2. Trigger Execution ──────────────────────────────────── |
| 29 | print(f"Triggering execution for workflow {WORKFLOW_ID}...") |
| 30 | with open(FILE_PATH, "rb") as f: |
| 31 | exec_resp = requests.post( |
| 32 | f"{BASE_URL}/api/v1/workflow-builder/workflows/{WORKFLOW_ID}/executions", |
| 33 | headers=headers, |
| 34 | files=[("files", (FILE_PATH, f, "application/pdf"))], |
| 35 | ) |
| 36 | exec_resp.raise_for_status() |
| 37 | execution_id = exec_resp.json()["workflow_execution_id"] |
| 38 | print(f" Execution ID: {execution_id}") |
| 39 | |
| 40 | # ── 3. Poll for Results ───────────────────────────────────── |
| 41 | print("Polling for results...") |
| 42 | status_url = f"{BASE_URL}/api/v1/workflow-builder/workflows/{WORKFLOW_ID}/executions/{execution_id}" |
| 43 | elapsed = 0 |
| 44 | |
| 45 | while elapsed < MAX_WAIT: |
| 46 | poll_resp = requests.get(status_url, headers=headers) |
| 47 | poll_resp.raise_for_status() |
| 48 | result = poll_resp.json() |
| 49 | status = result["status"] |
| 50 | print(f" [{elapsed}s] Status: {status}") |
| 51 | |
| 52 | if status in ("completed", "failed"): |
| 53 | break |
| 54 | |
| 55 | time.sleep(POLL_INTERVAL) |
| 56 | elapsed += POLL_INTERVAL |
| 57 | |
| 58 | # ── 4. Process Results ────────────────────────────────────── |
| 59 | if status == "completed": |
| 60 | print("\nExecution completed successfully!\n") |
| 61 | for step in result.get("steps", []): |
| 62 | print(f"Step: {step['step_name']}") |
| 63 | print(f" Type: {step.get('step_type', 'N/A')}") |
| 64 | print(f" Data: {step.get('data', {})}") |
| 65 | print() |
| 66 | elif status == "failed": |
| 67 | print(f"\nExecution failed: {result.get('error', 'Unknown error')}") |
| 68 | else: |
| 69 | print(f"\nTimed out after {MAX_WAIT}s. Last status: {status}") |