Skip to content

Exceptions (fastapi_responseschema.routing)

Class SchemaAPIRoute

class SchemaAPIRoute(APIRoute)

[view_source]

An APIRoute class to wrap response_model(s) with a ResponseSchema Must be subclassed setting at least SchemaAPIRoute.response_model.

Usage:

from typing import Generic, TypeVar
from fastapi_responseschema.interfaces import AbstractResponseSchema

T = TypeVar("T")
class MyResponseSchema(AbstractResponseSchema[T], Generic[T]):
    ...

class MyAPIRoute(SchemaAPIRoute):
    response_schema = MyResponseSchema

from fastapi import APIRouter

router = APIRouter(route_class=MyAPIRoute)

is_error_state

def is_error_state(status_code: Optional[int] = None) -> bool

[view_source]

Handles the error_state for the operation evaluating the status_code. This method gets called internally and can be overridden to modify the error state of the operation.

Arguments:

  • status_code Optional[int], optional - Operation status code. Defaults to None.

Returns:

  • bool - wether or not the operation returns an error

get_wrapper_model

def get_wrapper_model(
        is_error: bool,
        response_model: Type[Any]) -> Type[AbstractResponseSchema[Any]]

[view_source]

Implements the ResponseSchema selection logic. This method gets called internally and can be overridden to gain control over the ResponseSchema selection logic.

Arguments:

  • is_error int - wheteher or not the operation returns an error.
  • response_model Type[Any] - response_model set for APIRoute.

Returns:

  • Type[AbstractResponseSchema[Any]] - The ResponseSchema to wrap the response_model.

override_response_model

def override_response_model(
        wrapper_model: Type[AbstractResponseSchema[Any]],
        response_model: Type[Any]) -> Type[AbstractResponseSchema[Any]]

[view_source]

Wraps the given response_model with the ResponseSchema. This method gets called internally and can be overridden to gain control over the response_model wrapping logic.

Arguments:

  • wrapper_model Type[AbstractResponseSchema[Any]] - ResponseSchema Model
  • response_model Type[Any] - response_model set for APIRoute
  • response_model_include Optional[Union[SetIntStr, DictIntStrAny]], optional - Pydantic BaseModel include. Defaults to None.
  • response_model_exclude Optional[Union[SetIntStr, DictIntStrAny]], optional - Pydantic BaseModel exclude. Defaults to None.

Returns:

  • Type[AbstractResponseSchema[Any]] - The response_model wrapped in response_schema

respond

def respond(response_content: Optional[Any] = None,
            **metadata: Any) -> ResponseWithMetadata

[view_source]

Returns the response content with optional metadata

Arguments:

  • response_content Optional[Any], optional - Response Content. Defaults to None.
  • **metadata - Arbitrary metadata

Returns:

  • ResponseWithMetadata - An intermediate data structure to add metadatato a ResponseSchema serialization