import os, time, requests
BASE = "https://eniac.floworks.ai"
AUTH = {"Authorization": f"Bearer {os.environ['ENIAC_CREDENTIAL']}"}
# The five terminal states. Anything else means the run is still going.
TERMINAL = {"done", "stopped", "budget_exhausted", "interrupted", "error"}
def search(query: str, target: int = 50) -> dict:
start = requests.post(
f"{BASE}/v1/searches",
headers=AUTH,
json={"q": query, "target": target, "speed": "fast", "relax": True},
timeout=30,
)
if start.status_code == 402:
# Out of funds. A retry fails the same way until the wallet is topped up.
raise SystemExit(start.json()["detail"]["message"])
start.raise_for_status()
run_id = start.json()["id"]
while True:
run = requests.get(f"{BASE}/v1/searches/{run_id}", headers=AUTH, timeout=30).json()
if run["status"] in TERMINAL:
return run
time.sleep(5)
run = search("US fintech companies hiring ML engineers in the last 3 months")
# A terminal run can carry a partial table, and `result` is null when the run
# never produced one, so neither the status nor the field can be assumed.
rows = (run["result"] or {}).get("table") or []
print(run["status"], run["usdDisplay"], len(rows), "rows")