插件

class litestar.plugins.CLIPlugin

基类:CLIPluginProtocol

Plugin protocol to extend the CLI Server Lifespan.

class litestar.plugins.CLIPluginProtocol

基类:Protocol

Plugin protocol to extend the CLI.

on_cli_init(cli: Group) None

Called when the CLI is initialized.

This can be used to extend or override existing commands.

参数:

cli -- The root click.Group of the Litestar CLI

示例

from litestar import Litestar
from litestar.plugins import CLIPluginProtocol
from click import Group


class CLIPlugin(CLIPluginProtocol):
    def on_cli_init(self, cli: Group) -> None:
        @cli.command()
        def is_debug_mode(app: Litestar):
            print(app.debug)


app = Litestar(plugins=[CLIPlugin()])
__init__(*args, **kwargs)
class litestar.plugins.DIPlugin

基类:ABC

Extend dependency injection

abstractmethod has_typed_init(type_: Any) bool

Return True if type_ has type information available for its __init__() method that cannot be extracted from this method's type annotations (e.g. a Pydantic BaseModel subclass), and DIPlugin.get_typed_init() supports extraction of these annotations.

abstractmethod get_typed_init(type_: Any) tuple[Signature, dict[str, Any]]

Return signature and type information about the type_s __init__() method.

class litestar.plugins.InitPlugin

基类:InitPluginProtocol

Protocol used to define plugins that affect the application's init process.

on_app_init(app_config: AppConfig) AppConfig

Receive the AppConfig instance after on_app_init hooks have been called.

示例

from litestar import Litestar, get
from litestar.di import Provide
from litestar.plugins import InitPluginProtocol


def get_name() -> str:
    return "world"


@get("/my-path")
def my_route_handler(name: str) -> dict[str, str]:
    return {"hello": name}


class MyPlugin(InitPluginProtocol):
    def on_app_init(self, app_config: AppConfig) -> AppConfig:
        app_config.dependencies["name"] = Provide(get_name)
        app_config.route_handlers.append(my_route_handler)
        return app_config


app = Litestar(plugins=[MyPlugin()])
参数:

app_config -- The AppConfig instance.

返回:

The app config object.

class litestar.plugins.InitPluginProtocol

基类:Protocol

Protocol used to define plugins that affect the application's init process.

自 2.15 版本弃用: Use 'InitPlugin' instead

on_app_init(app_config: AppConfig) AppConfig

Receive the AppConfig instance after on_app_init hooks have been called.

示例

from litestar import Litestar, get
from litestar.di import Provide
from litestar.plugins import InitPluginProtocol


def get_name() -> str:
    return "world"


@get("/my-path")
def my_route_handler(name: str) -> dict[str, str]:
    return {"hello": name}


class MyPlugin(InitPluginProtocol):
    def on_app_init(self, app_config: AppConfig) -> AppConfig:
        app_config.dependencies["name"] = Provide(get_name)
        app_config.route_handlers.append(my_route_handler)
        return app_config


app = Litestar(plugins=[MyPlugin()])
参数:

app_config -- The AppConfig instance.

返回:

The app config object.

__init__(*args, **kwargs)
class litestar.plugins.OpenAPISchemaPlugin

基类:OpenAPISchemaPluginProtocol

Plugin to extend the support of OpenAPI schema generation for non-library types.

static is_plugin_supported_type(value: Any) bool

Given a value of indeterminate type, determine if this value is supported by the plugin.

This is called by the default implementation of is_plugin_supported_field() for backwards compatibility. User's should prefer to override that method instead.

参数:

value -- An arbitrary value.

返回:

A bool indicating whether the value is supported by the plugin.

is_plugin_supported_field(field_definition: FieldDefinition) bool

Given a FieldDefinition that represents an indeterminate type, determine if this value is supported by the plugin

参数:

field_definition -- A parsed type.

返回:

Whether the type is supported by the plugin.

static is_undefined_sentinel(value: Any) bool

Return True if value should be treated as an undefined field

static is_constrained_field(field_definition: FieldDefinition) bool

Return True if the field should be treated as constrained. If returning True, constraints should be defined in the field's extras

class litestar.plugins.OpenAPISchemaPluginProtocol

基类:Protocol

Plugin protocol to extend the support of OpenAPI schema generation for non-library types.

static is_plugin_supported_type(value: Any) bool

Given a value of indeterminate type, determine if this value is supported by the plugin.

参数:

value -- An arbitrary value.

返回:

A typeguard dictating whether the value is supported by the plugin.

to_openapi_schema(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema

Given a type annotation, transform it into an OpenAPI schema class.

参数:
  • field_definition -- An OpenAPI instance.

  • schema_creator -- An instance of the openapi SchemaCreator.

返回:

An OpenAPI instance.

__init__(*args, **kwargs)
class litestar.plugins.ReceiveRoutePlugin

基类:object

Receive routes as they are added to the application.

receive_route(route: BaseRoute) None

Receive routes as they are registered on an application.

class litestar.plugins.SerializationPlugin

基类:SerializationPluginProtocol, ABC

Abstract base class for plugins that extend DTO functionality

abstractmethod supports_type(field_definition: FieldDefinition) bool

Given a value of indeterminate type, determine if this value is supported by the plugin.

参数:

field_definition -- A parsed type.

返回:

Whether the type is supported by the plugin.

abstractmethod create_dto_for_type(field_definition: FieldDefinition) type[AbstractDTO]

Given a parsed type, create a DTO class.

参数:

field_definition -- A parsed type.

返回:

A DTO class.

class litestar.plugins.SerializationPluginProtocol

基类:Protocol

Protocol used to define a serialization plugin for DTOs.

自 2.15 版本弃用: Use 'litestar.plugins.SerializationPluginProtocol' instead

supports_type(field_definition: FieldDefinition) bool

Given a value of indeterminate type, determine if this value is supported by the plugin.

参数:

field_definition -- A parsed type.

返回:

Whether the type is supported by the plugin.

create_dto_for_type(field_definition: FieldDefinition) type[AbstractDTO]

Given a parsed type, create a DTO class.

参数:

field_definition -- A parsed type.

返回:

A DTO class.

__init__(*args, **kwargs)