Client for the Tripo 3D Generation API.
| 19 | |
| 20 | |
| 21 | class TripoClient: |
| 22 | """Client for the Tripo 3D Generation API.""" |
| 23 | |
| 24 | # The base URL for the Tripo API as specified in the OpenAPI schema |
| 25 | BASE_URL = "https://api.tripo3d.ai/v2/openapi" |
| 26 | SEGMENT_V2_MODEL = "v2.0-20260430" |
| 27 | |
| 28 | def __init__( |
| 29 | self, |
| 30 | api_key: Optional[str] = None, |
| 31 | IS_GLOBAL: bool = True, |
| 32 | v3_base_url: Optional[str] = None, |
| 33 | ): |
| 34 | """ |
| 35 | Initialize the Tripo API client. |
| 36 | |
| 37 | Args: |
| 38 | api_key: The API key for authentication. If not provided, it will be read from the |
| 39 | TRIPO_API_KEY environment variable. |
| 40 | |
| 41 | Raises: |
| 42 | ValueError: If no API key is provided and the TRIPO_API_KEY environment variable is not set. |
| 43 | """ |
| 44 | self.api_key = api_key or os.environ.get("TRIPO_API_KEY") |
| 45 | if not self.api_key: |
| 46 | raise ValueError( |
| 47 | "API key is required. Provide it as an argument or set the TRIPO_API_KEY environment variable." |
| 48 | ) |
| 49 | |
| 50 | if not self.api_key.startswith('tsk_'): |
| 51 | raise ValueError("API key must start with 'tsk_'") |
| 52 | |
| 53 | if not IS_GLOBAL: |
| 54 | self.BASE_URL = "https://api.tripo3d.com/v2/openapi" |
| 55 | default_v3_base_url = "https://openapi.tripo3d.com" |
| 56 | else: |
| 57 | default_v3_base_url = "https://openapi.tripo3d.ai" |
| 58 | |
| 59 | self._v3_base_url = v3_base_url or default_v3_base_url |
| 60 | self._impl = ClientImpl(self.api_key, self.BASE_URL) |
| 61 | self._v3_impl = ClientImpl(self.api_key, self._v3_base_url) |
| 62 | |
| 63 | |
| 64 | async def close(self) -> None: |
| 65 | """Close any open connections.""" |
| 66 | await self._impl.close() |
| 67 | await self._v3_impl.close() |
| 68 | |
| 69 | def _is_ssl_error(self, error: Exception) -> bool: |
| 70 | """Check if the error is an SSL certificate verification error.""" |
| 71 | error_str = str(error).lower() |
| 72 | ssl_error_indicators = [ |
| 73 | 'ssl certificate verification error', |
| 74 | 'certificate_verify_failed', |
| 75 | 'unable to get local issuer certificate', |
| 76 | 'ssl: certificate_verify_failed', |
| 77 | 'sslcertverificationerror' |
| 78 | ] |
no outgoing calls