Skip to content

patera.controller

Controllers and endpoint decorators. See the controllers guide.

Controller subpackage

post module-attribute

post = endpoint_decorator_factory(HttpMethod.POST)

put module-attribute

put = endpoint_decorator_factory(HttpMethod.PUT)

patch module-attribute

patch = endpoint_decorator_factory(HttpMethod.PATCH)

delete module-attribute

delete = endpoint_decorator_factory(HttpMethod.DELETE)

Controller

Controller(app: AppT, url_path: str = '/', open_api_spec: bool = True, open_api_tags: Optional[list[str]] = None, alias: Optional[str] = None)

Bases: Injectable, Generic[AppT]

endpoints_map property

endpoints_map: dict[str, dict[str, str | Callable | dict]]

Returns map of all endpoints

path property

path: str

Path variable of the class

open_api_spec property

open_api_spec: bool

If it is included in the open api specs

app property

app: AppT

App object

alias property

alias: Optional[str]

Alias for the controller, used in url_for

get_endpoint_methods

get_endpoint_methods() -> dict[str, dict[str, str | Callable]]

Returns the controller's endpoints by HTTP method and path.

Raises DuplicateRouteError if two different methods are registered for the same HTTP method and path.

Descriptor

Descriptor(status: int | HttpStatus = BAD_REQUEST, description: Optional[str] = None, media_type: MediaType = APPLICATION_JSON, body: Optional[Type[BaseModel]] = None)

path

path(url_path: str = '/', open_api_spec: bool = True, tags: Optional[list[str]] = None, alias: Optional[str] = None) -> 'Callable'

get

get(url_path: str, open_api_spec: bool = True, tags: Optional[list[str]] = None) -> _EndpointDecorator

GET http handler decorator.

socket

socket(url_path: str) -> _EndpointDecorator

SOCKET http handler decorator.

consumes

consumes(media_type: MediaType) -> _EndpointDecorator

Decorator indicating what media type the endpoint consumes.

produces

produces(media_type: MediaType, status_code: HttpStatus = OK) -> _EndpointDecorator

Decorator indicating what media type the endpoint produces and what the default status code is.

Important: - this acts as metadata and default behavior - it does not override an explicit response media type or explicit content-type header

before_request

before_request(func: Callable[..., Any]) -> Callable[..., Any]

after_request

after_request(func: Callable[..., Any]) -> Callable[..., Any]

after_init

after_init(func: Callable[..., Any]) -> Callable[..., Any]

open_api_docs

open_api_docs(*args: Descriptor)

Adds descriptions for error responses to OpenAPI documentation

cors

cors(*, allow_origins: Optional[list[str]] = None, allow_methods: Optional[list[str]] = None, allow_headers: Optional[list[str]] = None, expose_headers: Optional[list[str]] = None, allow_credentials: Optional[bool] = None, max_age: Optional[int] = None) -> Callable

Per-endpoint CORS override. Any provided option overrides the global config. Usage:

    @path("/items")
    @cors(allow_origins=["https://app.example.com"], allow_credentials=True)
    async def list_items(self, req: Request) -> Response: ...

no_cors

no_cors(func: Callable) -> Callable

Decorator to disable CORS for a specific endpoint handler. Usage:

    @path("/internal")
    @no_cors
    async def internal(self, req: Request) -> Response:
        return req.res.text("No CORS here")

max_content_length

max_content_length(limit: Optional[int]) -> Callable[[Callable], Callable]

Sets the largest request body, in bytes, one endpoint accepts. It overrides the MAX_CONTENT_LENGTH setting for that endpoint; None removes the limit. Larger bodies are answered with 413.

Usage:

    @post("/uploads")
    @max_content_length(200 * 1024 * 1024)
    async def upload(self, req: Request) -> Response:
        ...