Interfaces (fastapi_responseschema.interfaces
)
Class AbstractResponseSchema
class AbstractResponseSchema(PydanticGenericModel, Generic[T], ABC)
Abstract generic model for building response schema interfaces.
from_api_route
@classmethod
@abstractmethod
def from_api_route(
cls: Type[TResponseSchema],
content: T,
path: str,
status_code: int,
response_model: Optional[Type[BaseModel]] = None,
tags: Optional[List[str]] = None,
summary: Optional[str] = None,
description: Optional[str] = None,
response_description: str = "Successful Response",
deprecated: Optional[bool] = None,
name: Optional[str] = None,
methods: Optional[Union[Set[str], List[str]]] = None,
operation_id: Optional[str] = None,
response_model_include: Optional[Union[SetIntStr,
DictIntStrAny]] = None,
response_model_exclude: Optional[Union[SetIntStr,
DictIntStrAny]] = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: Optional[Type[Response]] = None,
**extra_params: Any) -> TResponseSchema
Builds an instance of response model from an API Route constructor. This method must be overridden by subclasses.
Arguments:
content
Any - The response content.path
str - Response path info.status_code
int - response status code.response_model
Optional[Type[BaseModel]], optional - The route response model. Defaults to None.tags
Optional[List[str]], optional - OpenAPI Tags configured in the API Route. Defaults to None.summary
Optional[str], optional - OpenAPI Summary. Defaults to None.description
Optional[str], optional - OpenAPI description. Defaults to None.response_description
str, optional - A string describing the response. Defaults to "Successful Response".deprecated
Optional[bool], optional - OpenAPI deprecation flag. Defaults to None.name
Optional[str], optional - Operation name. Defaults to None.methods
Optional[Union[Set[str], List[str]]], optional - supoported methods. Defaults to None.operation_id
Optional[str], optional - OpenAPI operation ID. Defaults to None.response_model_include
Optional[Union[SetIntStr, DictIntStrAny]], optional -response_model
\ Included fields. Defaults to None.response_model_exclude
Optional[Union[SetIntStr, DictIntStrAny]], optional -response_model
\ Excluded fields. Defaults to None.response_model_by_alias
bool, optional - Enable or disable field aliases inresponse_model
. \ Defaults to True.response_model_exclude_unset
bool, optional - excludes unset values inresponse_model
. Defaults to False.response_model_exclude_defaults
bool, optional - excludes default values inresponse_model
. Defaults to False.response_model_exclude_none
bool, optional - excludes None values inresponse_model
. Defaults to False.include_in_schema
bool, optional - wether or not include this operation in the OpenAPI Schema. Defaults to True.response_class
Optional[Type[Response]], optional - FastaAPI/Starlette Response Class. Defaults to None.
Returns:
TResponseSchema
- A ResponseSchema instance
from_exception
@classmethod
@abstractmethod
def from_exception(cls: Type[TResponseSchema],
request: Request,
reason: T,
status_code: int,
headers: Optional[dict] = None,
**extra_params: Any) -> TResponseSchema
Builds a ResponseSchema instance from an exception. This method must be overridden by subclasses.
Arguments:
request
Request - A FastaAPI/Starlette Request.reason
str - TheException
description or response data.status_code
int - the response status code.headers
dict - the response_headers
Returns:
TResponseSchema
- A ResponseSchema instance
from_exception_handler
@classmethod
def from_exception_handler(
cls: Type[TResponseSchema], request: Request,
exception: Union[RequestValidationError, StarletteHTTPException,
FastAPIHTTPException, BaseGenericHTTPException]
) -> TResponseSchema
Used in exception handlers to build a ResponseSchema instance. This method should not be overridden by subclasses.
Arguments:
request
Request - A FastaAPI/Starlette Request.exception
Union[RequestValidationError, StarletteHTTPException, FastAPIHTTPException, BaseGenericHTTPException] - The instantiated raised exception.
Returns:
TResponseSchema
- A ResponseSchema instance
Class ResponseWithMetadata
class ResponseWithMetadata(NamedTuple)
This Interface wraps the response content with the additional metadata
Arguments:
metadata
dict - A dictionary containing the metadata fields.response_content
Optional[Any] - The content of the response. Default to None.