Skip to content

http

NexosAIAPIService dataclass

NexosAIAPIService()

Abstract class for asynchronous services.

request async

request(
    verb: str,
    url: str,
    override_base: bool = False,
    **kwargs: Any,
) -> Response

Send an HTTP request using the configured client.

Parameters:

Name Type Description Default
verb str

The HTTP method to use (e.g., 'GET', 'POST').

required
url str

The URL to which the request is sent. If override_base is True, it will not prepend the base URL.

required
override_base bool

If True, the base URL will not be prepended to the provided URL.

False

Returns:

Type Description
Response

The HTTP response.

Source code in src/nexosapi/services/http.py
async def request(self, verb: str, url: str, override_base: bool = False, **kwargs: typing.Any) -> httpx.Response:
    """
    Send an HTTP request using the configured client.

    :param verb: The HTTP method to use (e.g., 'GET', 'POST').
    :param url: The URL to which the request is sent. If `override_base` is True, it will not prepend the base URL.
    :param override_base: If True, the base URL will not be prepended to the provided URL.
    :return: The HTTP response.
    """
    full_url = url if override_base else f"{self.base_url}/{url.lstrip('/')}"
    logging.debug(f"[API] Requesting {verb} {full_url} with params: {kwargs.get('json', {})}")
    async with self.client() as spawned_client:
        return await spawned_client.request(
            method=verb, url=full_url, follow_redirects=self.follow_redirects, **kwargs
        )

construct_headers

construct_headers(
    config: NexosAIAPIConfiguration,
) -> dict[str, str]

Construct headers for the HTTP request.

Parameters:

Name Type Description Default
config NexosAIAPIConfiguration

The configuration for the API service.

required

Returns:

Type Description
dict[str, str]

A dictionary of headers.

Source code in src/nexosapi/services/http.py
def construct_headers(self, config: NexosAIAPIConfiguration) -> dict[str, str]:
    """
    Construct headers for the HTTP request.

    :param config: The configuration for the API service.
    :return: A dictionary of headers.
    """
    return {
        NEXOSAI_AUTH_HEADER_NAME: f"{NEXOSAI_AUTH_HEADER_PREFIX} {config.api_key}",
    }

construct_auth

construct_auth(
    config: NexosAIAPIConfiguration,
) -> Auth | None

Construct authentication for the HTTP request.

Parameters:

Name Type Description Default
config NexosAIAPIConfiguration

The configuration for the API service.

required

Returns:

Type Description
Auth | None

A httpx.Auth object or None if no authentication is needed.

Source code in src/nexosapi/services/http.py
def construct_auth(self, config: NexosAIAPIConfiguration) -> httpx.Auth | None:  # noqa: ARG002
    """
    Construct authentication for the HTTP request.

    :param config: The configuration for the API service.
    :return: A httpx.Auth object or None if no authentication is needed.
    """
    return None

disconnect async

disconnect() -> None

Disconnect the HTTP client.

Source code in src/nexosapi/services/http.py
async def disconnect(self) -> None:
    """
    Disconnect the HTTP client.
    """
    self.loop = None