aixplain.v2.client
Client module for making HTTP requests to the aiXplain API.
default_timeout
def default_timeout() -> Tuple[float, float]
Resolve the default (connect, read) timeout, honouring env overrides.
AIXPLAIN_HTTP_CONNECT_TIMEOUT / AIXPLAIN_HTTP_READ_TIMEOUT (seconds)
override the built-in defaults per environment without a code change.
normalize_origin
def normalize_origin(url: str) -> Optional[Origin]
Return the normalized (scheme, host, port) triple for url, or None.
Uses hostname/port rather than netloc so that userinfo
(https://platform-api.aixplain.com@evil.example.com/x) cannot disguise the
real target and so :443 compares equal to the implicit default. Returns
None for anything that is not a parseable http(s) URL with a host, and for
URLs carrying userinfo, which a legitimate aiXplain endpoint never does.
build_trusted_origins
def build_trusted_origins(urls: Iterable[str]) -> FrozenSet[Origin]
Build the trusted origin set from configured endpoints, defaults and env.
Scheme is part of the origin, so plain http to an aiXplain host is not
trusted; http is only ever trusted when an operator explicitly configured
an http endpoint (e.g. BACKEND_URL=http://localhost:8000), which is a
deliberate opt-in on a host they control rather than something a response
body can induce.
_AixplainSession Objects
class _AixplainSession(requests.Session)
Session that drops aiXplain credentials when a redirect leaves the trusted set.
requests strips only Authorization on a host change; a custom
x-api-key header is copied verbatim to the redirect target, so a single
302 from a trusted host would hand the team key to an attacker-chosen one.
Redirects stay enabled -- a legitimate 302 to a presigned S3 result URL
still resolves, it just arrives unauthenticated (presigned URLs carry their
own signature).
__init__
def __init__(trusted_origins: FrozenSet[Origin] = frozenset()) -> None
Initialize the session with the origins allowed to receive credentials.
rebuild_auth
def rebuild_auth(prepared_request: requests.PreparedRequest,
response: requests.Response) -> None
Strip aiXplain credentials when the redirect target is not trusted.
create_retry_session
def create_retry_session(total: Optional[int] = None,
backoff_factor: Optional[float] = None,
status_forcelist: Optional[List[int]] = None,
trusted_origins: FrozenSet[Origin] = frozenset(),
**kwargs: Any) -> requests.Session
Creates a requests.Session with a specified retry strategy.
Only GET is retried (see RETRY_ALLOWED_METHODS): POST is not
idempotent here, so a transport-level retry of /execute submits — and
bills — the run again. Callers that want a run re-submitted on a transient
failure use the SDK-level run_retries parameter instead.
Arguments:
totalint, optional - Total number of retries allowed. Defaults to 5.backoff_factorfloat, optional - Backoff factor to apply between retry attempts. Defaults to 0.1.status_forcelistlist, optional - List of HTTP status codes to force a retry on. Defaults to [429, 500, 502, 503, 504]. Retries are jittered and honourRetry-After(bounded by DEFAULT_RETRY_AFTER_MAX).trusted_originsfrozenset, optional -(scheme, host, port)triples allowed to receive aiXplain credentials across a redirect. Defaults to none, i.e. any redirect drops them.kwargsdict, optional - Additional keyword arguments for internal Retry object.
Returns:
requests.Session- A requests.Session object with the specified retry strategy.
AixplainClient Objects
class AixplainClient()
HTTP client for aiXplain API with retry support.
__init__
def __init__(
base_url: str,
aixplain_api_key: Optional[str] = None,
team_api_key: Optional[str] = None,
retry_total: int = DEFAULT_RETRY_TOTAL,
retry_backoff_factor: float = DEFAULT_RETRY_BACKOFF_FACTOR,
retry_status_forcelist: List[int] = DEFAULT_RETRY_STATUS_FORCELIST,
timeout: Optional[TimeoutType] = None,
trusted_urls: Optional[List[str]] = None) -> None
Initialize AixplainClient with authentication and retry configuration.
Arguments:
base_urlstr - The base URL for the API.aixplain_api_keystr, optional - The individual API key.team_api_keystr, optional - The team API key.retry_totalint - Total number of retries allowed. Defaults to 5.retry_backoff_factorfloat - Backoff factor between retry attempts. Defaults to 0.1.retry_status_forcelistlist - HTTP status codes that trigger a retry. Defaults to [429, 500, 502, 503, 504]. timeout (float or (float, float) tuple, optional): Default timeout for every request that doesn't pass its owntimeout=. Defaults to (AIXPLAIN_HTTP_CONNECT_TIMEOUT or 10, AIXPLAIN_HTTP_READ_TIMEOUT or 300) seconds. Individual calls can still override it per request.trusted_urlslist, optional - Extra endpoints allowed to receive the API key, on top ofbase_url, the aiXplain defaults andAIXPLAIN_TRUSTED_HOSTS. Any other URL raisesUntrustedURLErrorbefore a socket is opened.
resolve_url
def resolve_url(path: str) -> str
Resolve path against base_url unless it is already absolute.
is_trusted_url
def is_trusted_url(url: str) -> bool
Return True when url is an origin allowed to receive the API key.
ensure_trusted_url
def ensure_trusted_url(path: str) -> str
Resolve path and return it, raising unless it is a trusted origin.
Validation happens on the resolved URL: a relative input such as
//evil.example.com/x becomes an absolute foreign URL only after
urljoin, so checking the caller's string would be bypassable.
Raises:
UntrustedURLError- If the resolved URL is not a trusted aiXplain origin.
request_raw
def request_raw(method: str, path: str, **kwargs: Any) -> requests.Response
Sends an HTTP request.
Arguments:
methodstr - HTTP method (e.g. 'GET', 'POST')pathstr - URL path or full URLkwargsdict, optional - Additional keyword arguments for the request
Returns:
requests.Response- The response from the request
Raises:
UntrustedURLError- If the resolved URL is not a trusted aiXplain origin.
request
def request(method: str, path: str, **kwargs: Any) -> dict
Sends an HTTP request.
Arguments:
methodstr - HTTP method (e.g. 'GET', 'POST')pathstr - URL pathkwargsdict, optional - Additional keyword arguments for the request
Returns:
dict- The response from the request
get
def get(path: str, **kwargs: Any) -> dict
Sends an HTTP GET request.
Arguments:
pathstr - URL pathkwargsdict, optional - Additional keyword arguments for the request
Returns:
dict- The JSON response from the request
post
def post(path: str, **kwargs: Any) -> dict
Sends an HTTP POST request.
Arguments:
pathstr - URL pathkwargsdict, optional - Additional keyword arguments for the request
Returns:
dict- The JSON response from the request
request_stream
def request_stream(method: str, path: str, **kwargs: Any) -> requests.Response
Sends a streaming HTTP request.
This method is similar to request_raw but enables streaming mode, which is necessary for Server-Sent Events (SSE) responses.
Arguments:
methodstr - HTTP method (e.g. 'GET', 'POST')pathstr - URL path or full URLkwargsdict, optional - Additional keyword arguments for the request
Returns:
requests.Response- The streaming response (not consumed)
Raises:
APIError- If the request failsUntrustedURLError- If the resolved URL is not a trusted aiXplain origin.