| 73 | |
| 74 | |
| 75 | class HTTPClient: |
| 76 | def __init__( |
| 77 | self, |
| 78 | token: str, |
| 79 | base_url: str, |
| 80 | timeout: float, |
| 81 | client_platform: Optional[str] = None, |
| 82 | ): |
| 83 | self._token = token |
| 84 | self._base_url = base_url.rstrip("/") |
| 85 | self._jobs_url = f"{self._base_url}{API_PATH}" |
| 86 | self._timeout = timeout |
| 87 | self._session = requests.Session() |
| 88 | self._session.headers["Authorization"] = f"Bearer {token}" |
| 89 | if client_platform: |
| 90 | self._session.headers["Client-Platform"] = client_platform |
| 91 | |
| 92 | @property |
| 93 | def timeout(self) -> float: |
| 94 | return self._timeout |
| 95 | |
| 96 | def submit_url( |
| 97 | self, |
| 98 | model: str, |
| 99 | file_url: str, |
| 100 | optional_payload: dict, |
| 101 | page_ranges: Optional[str] = None, |
| 102 | batch_id: Optional[str] = None, |
| 103 | ) -> str: |
| 104 | body = { |
| 105 | "fileUrl": file_url, |
| 106 | "model": model, |
| 107 | "optionalPayload": optional_payload, |
| 108 | } |
| 109 | if page_ranges is not None: |
| 110 | body["pageRanges"] = page_ranges |
| 111 | if batch_id is not None: |
| 112 | body["batchId"] = batch_id |
| 113 | try: |
| 114 | resp = self._session.post( |
| 115 | self._jobs_url, |
| 116 | json=body, |
| 117 | timeout=self._timeout, |
| 118 | ) |
| 119 | except requests.Timeout as e: |
| 120 | raise RequestTimeoutError(f"Request timed out: {e}") from e |
| 121 | except requests.ConnectionError as e: |
| 122 | raise NetworkError(f"Connection failed: {e}") from e |
| 123 | _raise_for_response(resp) |
| 124 | return _job_id_from_response(resp) |
| 125 | |
| 126 | def submit_file( |
| 127 | self, |
| 128 | model: str, |
| 129 | file_path: str, |
| 130 | optional_payload: dict, |
| 131 | page_ranges: Optional[str] = None, |
| 132 | batch_id: Optional[str] = None, |
no outgoing calls