Exceptions (fastapi_responseschema.routing
)
Class SchemaAPIRoute
class SchemaAPIRoute(APIRoute)
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
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]]
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]]
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 Modelresponse_model
Type[Any] - response_model set for APIRouteresponse_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
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