fastlife.config.configurator
¶
The configurator registers routes in a FastAPI application while adding support for dependency injection during the configuration phase.
FastAPI does not provide any built-in support for dependency injection during the configuration phase. Instead, it only resolves dependencies at request time, ensuring they are dynamically handled per request.
The configurator is designed to handle the setup during the configuration phase.
Module Contents¶
Classes¶
Configure and build an application. |
|
Configure and build an application. |
Functions¶
Decorator used to attach route in a submodule while using the configurator.include. |
|
Fix the router. |
API¶
- exception fastlife.config.configurator.ConfigurationError¶
Bases:
Exception
Error raised during configuration, to avoid errors at runtime.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- class fastlife.config.configurator.Configurator(settings: fastlife.config.settings.Settings)¶
Bases:
fastlife.config.configurator.GenericConfigurator
[fastlife.config.registry.DefaultRegistry
]Configure and build an application.
Initialize the app from the settings.
Initialization
- Parameters:
settings – application settings.
- class fastlife.config.configurator.GenericConfigurator(settings: fastlife.config.settings.Settings)¶
Bases:
typing.Generic
[fastlife.config.registry.TRegistry
]Configure and build an application.
Initialize the app from the settings.
Initialization
- Parameters:
settings – application settings.
- add_api_route(name: str, path: str, endpoint: collections.abc.Callable[..., Any], *, permission: str | None = None, status_code: int | None = None, tags: list[str | enum.Enum] | None = None, summary: str | None = None, description: str | None = None, response_description: str = 'Successful Response', deprecated: bool | None = None, methods: list[str] | None = None, operation_id: str | None = None, response_model_include: fastapi.types.IncEx | None = None, response_model_exclude: fastapi.types.IncEx | None = 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, openapi_extra: dict[str, Any] | None = None) Self ¶
Add an API route to the app.
Fastlife does not use a decorator to attach routes, instead the decorator
fastlife.config.configurator.configure()
has to be used to inject routes inside a method and call the add_route method.This route has to be used to add API Route, by API, to expose it in the documentation.
To add a route that serve HTML user the method
Configurator.add_route()
- Parameters:
name – name of the route, used to build route from the helper
fastlife.request.request.Request.url_for()
in order to create links.path – path of the route, use
{curly_brace}
to inject FastAPI Path parameters.endpoint – the function that will reveive the request.
permission – a permission to validate by the security policy.
methods – restrict route to a list of http methods.
description – OpenAPI description for the route.
summary – OpenAPI summary for the route.
response_description – OpenAPI description for the response.
operation_id – OpenAPI optional unique string used to identify an operation.
tags – OpenAPI tags for the route.
deprecated – OpenAPI deprecated annotation for the route.
response_model_include – customize fields list to include in repsonse.
response_model_exclude – customize fields list to exclude in repsonse.
response_model_by_alias – serialize fields by alias or by name if False.
response_model_exclude_unset – exclude fields that are not explicitly set in response.
response_model_exclude_defaults – exclude default value of response fields.
response_model_exclude_none – exclude fields instead of serialize to null value.
include_in_schema – expose or not the route in the doc.
openapi_extra – OpenAPI documentation extra fields.
- Returns:
the configurator.
- add_exception_handler(status_code_or_exc: int | type[Exception], handler: collections.abc.Callable[..., Response | InlineTemplate], *, status_code: int = 500) Self ¶
Add an exception handler the application.
- add_middleware(middleware_class: type[fastlife.middlewares.base.AbstractMiddleware], **options: Any) Self ¶
Add a starlette middleware to the FastAPI app.
- add_openapi_tag(tag: fastlife.config.openapiextra.OpenApiTag) Self ¶
Register a tag description in the OpenAPI documentation.
- add_renderer(file_ext: str, renderer: fastlife.services.templates.AbstractTemplateRendererFactory) Self ¶
Add a render for a given file extension.
- Parameters:
file_ext – the file extention of your templates.
renderer – the renderer that will render the template.
- add_renderer_global(name: str, value: Any, *, evaluate: bool = True) None ¶
Add a rendering global value.
- Parameters:
name – the name or key of the global value available in the template.
value – a value, or a callable or an async function with a request in parameter that will evaluate the value.
evaluate – set to false if you want to inject helper methods in the template.
- add_route(name: str, path: str, endpoint: collections.abc.Callable[..., Any], *, permission: str | None = None, template: str | None = None, status_code: int | None = None, methods: list[str] | None = None) Self ¶
Add a route to the app.
Fastlife does not use the FastAPI decorator to attach routes, instead the decorator
@configure
has to be used to inject routes inside a method and call the add_route method. Or the decorator@view_config
can decorate view functions.- Parameters:
name – name of the route, used to build route from the helper
fastlife.request.request.Request.url_for()
in order to create links.path – path of the route, use
{curly_brace}
to inject FastAPI Path parameters.endpoint – the function that will reveive the request.
permission – a permission to validate by the
Security Policy
.status_code – customize response status code.
methods – restrict route to a list of http methods.
- Returns:
the configurator.
- add_static_route(route_path: str, directory: pathlib.Path, name: str = 'static') Self ¶
Mount a directory to an http endpoint.
- Parameters:
route_path – the root path for the statics.
directory – the directory on the filesystem where the statics files are.
name – a name for the route in the starlette app.
- Returns:
the configurator
- add_template_search_path(path: str | pathlib.Path) Self ¶
Add a template search path directly from the code.
- Parameters:
path – template path.
- add_translation_dirs(locales_dir: str) Self ¶
Add a translation directory for localization.
Usually, the locales is a directory from a package and has to be passed with a
:
separator: {package_name}:locales.- Parameters:
locales_dir – the directory contains local inside a python package.
- property all_registered_permissions: collections.abc.Sequence[str]¶
Get the list of registered permissions.
This is usefull for introspection or testing purpose.
- build_asgi_app() fastapi.FastAPI ¶
Build the app after configuration in order to start after beeing configured.
- Returns:
FastAPI application.
- include(module: str | types.ModuleType, route_prefix: str = '', ignore: fastlife.config.configurator.venusian_ignored_item | collections.abc.Sequence[fastlife.config.configurator.venusian_ignored_item] | None = None) Self ¶
Include a module in order to load its configuration.
It will scan and load all the submodule until you add an ignore rule.
The
ignore
argument allows you to ignore certain modules. If it is a scrint, it can be an absolute module name or a relative one, if starts with a dot.Here is an example.
from fastlife import Configurator, configure def home() -> dict[str, str]: return {"hello": "world"} @configure def includeme(config: Configurator) -> None: config.add_route("home", "/", home)
- Parameters:
module – a module to include.
route_prefix – prepend all included route with a prefix
ignore – ignore submodules
- set_api_documentation_info(title: str, version: str, description: str, *, summary: str | None = None, swagger_ui_url: str | None = None, redoc_url: str | None = None) Self ¶
Set your api documentation title for application that expose an API.
- Parameters:
title – OpenAPI documentation title
version – OpenAPI api version
description – OpenAPI documentation description. Use markdown here.
summary – OpenAPI documentation summary. A short description: text only.
swagger_ui_url – Endpoint for Swagger UI served by FastAPI
redoc_url – Endpoint for Redoc served by FastAPI
- set_locale_negociator(locale_negociator: fastlife.services.locale_negociator.LocaleNegociator) Self ¶
Install a locale negociator for the app.
- set_security_policy(security_policy: type[AbstractSecurityPolicy[Any, TRegistry]]) Self ¶
Set a security policy for the application.
Important
The security policy is per route_prefix. It means that if the application is splitted via multiple route_prefix using the
Configurator.include()
, they all have a distinct security policy. A secutity policy has to be install by all of those include call.- param security_policy:
The security policy that will applied for the app portion behind the route prefix.
- fastlife.config.configurator.configure(wrapped: collections.abc.Callable[[fastlife.config.configurator.TConfigurator], None]) collections.abc.Callable[[Any], None] ¶
Decorator used to attach route in a submodule while using the configurator.include.
- fastlife.config.configurator.rebuild_router(router: fastlife.routing.router.Router) fastlife.routing.router.Router ¶
Fix the router.
FastAPI routers has dependencies that are injected to routes where they are added.
It means that if you add a dependencies in the router after the route has been added, then the dependencies is missing in the route added before.
To prenvents issues, we rebuild the router route with the dependency.
- Parameters:
router – the router to rebuild
- Returns:
a new router with fixed routes.