| 51 | raise RuntimeError(f"status failed ({response.status_code}): {response.text}") |
| 52 | status = response.json() |
| 53 | task_status = status.get("status") |
| 54 | logger.info(f"task_id={task_id}, status={task_status}") |
| 55 | if task_status == "completed": |
| 56 | return status |
| 57 | if task_status in ("failed", "cancelled"): |
| 58 | raise RuntimeError(f"task ended with status={task_status}, detail={status}") |
| 59 | time.sleep(poll_interval) |
| 60 | raise TimeoutError(f"task {task_id} timeout after {timeout_seconds}s") |
| 61 | |
| 62 | |
| 63 | def download_result(base_url: str, task_id: str, output: str) -> Path: |
| 64 | result_url = f"{base_url.rstrip('/')}/v1/tasks/{task_id}/result" |
| 65 | response = requests.get(result_url, timeout=120) |
| 66 | if response.status_code != 200: |
| 67 | raise RuntimeError(f"download failed ({response.status_code}): {response.text}") |
| 68 | output_path = Path(output) |
| 69 | output_path.parent.mkdir(parents=True, exist_ok=True) |
| 70 | output_path.write_bytes(response.content) |
| 71 | return output_path |