Skip to content

controller

NexosAIAPIEndpointController dataclass

NexosAIAPIEndpointController(api_service=...)

Bases: Generic[EndpointRequestType, EndpointResponseType]

Abstract base class for NexosAI endpoint controllers. This class defines the structure for endpoint controllers in the Nexos AI API.

Source code in src/nexosapi/api/controller.py
structured_response._response = response

Operations

Enum to define operations for the NexosAIEndpointController. This enum can be extended to include specific operations for different controllers.

validate_endpoint classmethod

validate_endpoint(endpoint: str) -> None

Validates the endpoint format. Raises ValueError if the endpoint does not match the expected format.

Parameters:

Name Type Description Default
endpoint str

The API endpoint to validate.

required
Source code in src/nexosapi/api/controller.py
@classmethod
def validate_endpoint(cls, endpoint: str) -> None:
    """
    Validates the endpoint format.
    Raises ValueError if the endpoint does not match the expected format.

    :param endpoint: The API endpoint to validate.
    """
    verbs = ("get:", "post:", "put:", "delete:", "patch:")
    if not isinstance(cls.endpoint, str) or not endpoint.startswith(verbs):
        raise InvalidControllerEndpointError(
            f"Invalid endpoint format: {endpoint}. Must start with one of {verbs}."
        )
    if not cls.endpoint or not isinstance(cls.endpoint, str):
        raise InvalidControllerEndpointError(f"Endpoint must be a non-empty string for {cls.__class__.__name__}.")
    if not re.match(cls.VALID_ENDPOINT_REGEX, cls.endpoint):
        raise InvalidControllerEndpointError(
            f"Invalid endpoint format for {cls.__class__.__name__}: {cls.endpoint}. Expected format: 'verb:/path'."
        )

on_response async

on_response(
    response: EndpointResponseType,
) -> EndpointResponseType

Hook for processing the response before returning it. Can be overridden in subclasses to add custom response handling.

Parameters:

Name Type Description Default
response EndpointResponseType

The response object to process.

required

Returns:

Type Description
EndpointResponseType

The processed response object.

Source code in src/nexosapi/api/controller.py
async def on_response(self, response: EndpointResponseType) -> EndpointResponseType:
    """
    Hook for processing the response before returning it.
    Can be overridden in subclasses to add custom response handling.

    :param response: The response object to process.
    :return: The processed response object.
    """
    return response

on_error async

on_error(response: Response) -> EndpointResponseType

Hook for handling errors that occur during the request. Can be overridden in subclasses to add custom error handling.

Parameters:

Name Type Description Default
response Response

The HTTP response object which contains the error.

required

Returns:

Type Description
EndpointResponseType

A null response object or a custom error response.

Source code in src/nexosapi/api/controller.py
async def on_error(self, response: httpx.Response) -> EndpointResponseType:
    """
    Hook for handling errors that occur during the request.
    Can be overridden in subclasses to add custom error handling.

    :param response: The HTTP response object which contains the error.
    :return: A null response object or a custom error response.
    """
    logging.error(f"[SDK] Encountered an error during the request: {response.status_code} - {response.text}")
    logging.warning("[SDK] Returning null response due to error.")
    return self.response_model.null()