Skip to content

patera

The top-level package: the application class, configuration, requests, responses and helpers.

Patera

Patera(cli_mode: bool = False)

Bases: Injectable, Generic[ConfT]

Patera class implementation. Used to create a new application instance

Init function

controllers property

controllers: dict[str, Controller]

Registered controller instances by controller class name.

json_spec property

json_spec: dict | None

OpenAPI specification of the app (built on first access).

router property

router: Router

Router instance property of the app.

configs property

configs: ConfT

Returns configuration object.

root_path property

root_path: str

Returns root path of application.

url_prefix property

url_prefix: str

The URL_PREFIX setting: the prefix of every controller route ('' for none).

app property

app

Returns self.

For compatibility with the Controller class, which contains the app object on the app property.

static_files_path property

static_files_path: str

Static files path.

request_class property

request_class: Type[Request]

Returns the Request class used by the application. Can be overridden in configs.

response_class property

response_class: Type[Response]

Returns the Response class used by the application. Can be overridden in configs.

extensions property

extensions: dict[str, Injectable]

Returns dictionary with all registered extensions.

get_conf

get_conf(config_name: str, default: Any = None) -> Any

Returns app configuration with provided config_name.

Dotted names ("DATABASE.URI") read nested attributes or mapping keys. Returns default if the configuration is missing or None.

abort_route_not_found async

abort_route_not_found(send)

Aborts request because route was not found.

abort_method_not_allowed async

abort_method_not_allowed(send, allowed_methods: list[str])

Aborts request because method is not allowed.

abort_bad_request async

abort_bad_request(send) -> None

Send a 400 Bad Request response.

redirect_request async

redirect_request(send, location: str, status_code: int) -> None

Send an HTTP redirect response.

send_response async

send_response(res: Response, send, response_type: Optional[Type[Any]] = None)

Sends response.

Serialization is delegated to the response renderer / serializer registry. This method focuses on transport concerns: - zero-copy responses - streaming responses - normal ASGI start/body sending

add_request_observer

add_request_observer(observer: RequestObserver) -> None

Calls observer(record) with a RequestRecord after every HTTP request, including those answered by the router (404, 405), static resources and failed requests. Observers run in the request's task once its response has been sent, so they must be quick and must not block; an observer that raises is logged and skipped.

default_exception_response

default_exception_response(req: Request, exc: Exception) -> Optional[Response]

Fallback responses for exceptions without a registered handler.

BaseHttpException subclasses (abort(), RequestValidationError from @consumes, auth exceptions, ...) become a JSON response with the exception's status code.

Returns None for any other exception, which is then treated as an unhandled error (500). That includes a Pydantic ValidationError raised outside request-body validation: it is the server's data that failed. Override in the app class to customize.

register_middleware

register_middleware(middleware_class: Type[MiddlewareBase], *, order: int | None = None) -> None

Registers a middleware class with the application.

Middleware instances are not created immediately. They are instantiated only when the HTTP application is built.

This allows extensions to register middleware during app initialization without forcing the HTTP app to build in CLI mode.

build

build() -> None

Build the final app by wrapping self._app in all middleware.

Lower middleware order values are processed first and become outer middleware.

add_extension

add_extension(extension: Injectable)

Adds extension to extension map.

get_extension

get_extension(extension_type: Type[ExtT]) -> ExtT

Returns a registered extension by class.

Example: db_manager = app.get_extension(SqlDatabaseManager)

register_controller

register_controller(*ctrls: type[Controller], with_url_prefix: bool = True)

Registers controller classes with the application. Their routes get the URL_PREFIX setting in front unless with_url_prefix is False.

get_controller

get_controller(ctrl: type[Controller] | str) -> Optional[Controller]

Returns the registered controller instance for a controller class (or class name), or None if it is not registered.

register_exception_handler

register_exception_handler(*handlers: type[ExceptionHandler])

Registers exception handler classes with the application.

A handler class that is already registered is skipped. A different class with the same name raises ValueError.

url_for

url_for(endpoint: str, **values) -> str

Returns URL for endpoint method.

build_openapi_spec

build_openapi_spec()

Builds open api spec.

add_on_startup_method

add_on_startup_method(func: Callable)

Adds method to on_startup collection.

add_on_shutdown_method

add_on_shutdown_method(func: Callable)

Adds method to on_shutdown collection.

register_alias

register_alias(alias: str, endpoint: str)

Registers an alias for an endpoint name. Useful for url_for lookups.

cli_commands

cli_commands() -> list[CLICommand]

Lists every registered CLI command, sorted by name.

print_cli_commands

print_cli_commands(file: Any = None) -> None

Prints the registered CLI commands with their arguments and help.

run_cli

run_cli(command_name: str, *args, **kwargs) -> None

Runs the command command_name, written as <controller>:<command>. An invalid name, an unknown controller or an unknown command prints an error to standard error and exits with status 1.

add_template_path

add_template_path(path: str)

Adds a template path (searched after the already registered ones).

add_template_prefix

add_template_prefix(prefix: str, path: str) -> None

Makes the templates in path available as "/