datastructures¶
- class litestar.datastructures.URL[源代码]¶
基类:
objectRepresentation and modification utilities of a URL.
- static __new__(cls, url: str | SplitResult) URL[源代码]¶
Create a new instance.
- 参数:
url¶ -- url string or split result to represent.
- classmethod from_components(scheme: str = '', netloc: str = '', path: str = '', fragment: str = '', query: str = '') Self[源代码]¶
Create a new URL from components.
- classmethod from_scope(scope: Scope) Self[源代码]¶
Construct a URL from a
Scope- 参数:
scope¶ -- A scope
- 返回:
A URL
- property query_params: MultiDict¶
Query parameters of a URL as a
MultiDict- 返回:
A
MultiDictwith query parameters
备注
- The returned
MultiDictis mutable,URLitself is immutable, therefore mutating the query parameters will not directly mutate the
URL. If you want to modify query parameters, make modifications in the multidict and pass them back towith_replacements()
- The returned
- class litestar.datastructures.Accept[源代码]¶
基类:
objectAn
Acceptheader.- accepts(media_type: str) bool[源代码]¶
Check if the request accepts the specified media type.
If multiple media types can be provided, it is better to use
best_match().- 参数:
media_type¶ -- The media type to check for.
- 返回:
True if the request accepts
media_type.
- best_match(provided_types: list[str], default: str | None = None) str | None[源代码]¶
Find the best matching media type for the request.
- 参数:
- 返回:
The best matching media type. If the matching provided type contains wildcard characters, they are replaced with the corresponding part of the accepted type. Otherwise the provided type is returned as-is.
- class litestar.datastructures.Address[源代码]¶
基类:
NamedTupleJust a network address.
- class litestar.datastructures.CacheControlHeader[源代码]¶
基类:
HeaderA
cache-controlheader.- __init__(documentation_only: bool = False, max_age: int | None = None, s_maxage: int | None = None, no_cache: bool | None = None, no_store: bool | None = None, private: bool | None = None, public: bool | None = None, no_transform: bool | None = None, must_revalidate: bool | None = None, proxy_revalidate: bool | None = None, must_understand: bool | None = None, immutable: bool | None = None, stale_while_revalidate: int | None = None) None¶
- classmethod from_header(header_value: str) CacheControlHeader[源代码]¶
Create a
CacheControlHeaderinstance from the header value.- 参数:
header_value¶ -- the header value as string
- 返回:
An instance of
CacheControlHeader
- classmethod prevent_storing() CacheControlHeader[源代码]¶
Create a
cache-controlheader with theno-storedirective which indicates that any caches of any kind (private or shared) should not store this response.
- class litestar.datastructures.Cookie[源代码]¶
基类:
objectContainer class for defining a cookie using the
Set-Cookieheader.See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie for more details regarding this header.
- __eq__(other: Any) bool[源代码]¶
Determine whether two cookie instances are equal according to the cookie spec, i.e. hey have a similar path, domain and key.
- 参数:
other¶ -- An arbitrary value
- 返回:
A boolean
- __init__(key: str, path: str = '/', value: str | None = None, max_age: int | None = None, expires: int | None = None, domain: str | None = None, secure: bool | None = None, httponly: bool | None = None, samesite: Literal['lax', 'strict', 'none'] = 'lax', description: str | None = None, documentation_only: bool = False) None¶
- description: str | None = None¶
Description of the response cookie header for OpenAPI documentation.
- documentation_only: bool = False¶
Defines the Cookie instance as for OpenAPI documentation purpose only.
- path: str = '/'¶
Path fragment that must exist in the request url for the cookie to be valid.
Defaults to
/.
- samesite: Literal['lax', 'strict', 'none'] = 'lax'¶
Controls whether or not a cookie is sent with cross-site requests.
Defaults to 'lax'.
- property simple_cookie: SimpleCookie¶
Get a simple cookie object from the values.
- 返回:
- to_encoded_header() tuple[bytes, bytes][源代码]¶
Create encoded header for ASGI
send.- 返回:
A two tuple of bytes.
- class litestar.datastructures.FormMultiDict[源代码]¶
-
MultiDict for form data.
- classmethod from_form_data(form_data: dict[str, list[str] | str | UploadFile]) FormMultiDict[源代码]¶
Create a FormMultiDict from form data.
- 参数:
form_data¶ -- Form data to create the FormMultiDict from.
- 返回:
A FormMultiDict instance
- class litestar.datastructures.Header[源代码]¶
基类:
ABCAn abstract type for HTTP headers.
- documentation_only: bool = False¶
Defines the header instance as for OpenAPI documentation purpose only.
- abstractmethod classmethod from_header(header_value: str) Header[源代码]¶
Construct a header from its string representation.
- to_header(include_header_name: bool = False) str[源代码]¶
Get the header as string.
- 参数:
include_header_name¶ -- should include the header name in the return value. If set to false the return value will only include the header value. if set to true the return value will be:
<header name>: <header value>. Defaults to false.
- class litestar.datastructures.Headers[源代码]¶
基类:
CIMultiDictProxy[str],MultiMixin[str]An immutable, case-insensitive multi dict for HTTP headers.
- __init__(headers: Mapping[str, str] | RawHeaders | MultiDict[str] | None = None) None[源代码]¶
Initialize
Headers.- 参数:
headers¶ -- Initial value.
- class litestar.datastructures.ImmutableMultiDict[源代码]¶
基类:
MultiDictProxy[T],MultiMixin[T],Generic[T]Immutable MultiDict, using class:MultiDictProxy <multidict.MultiDictProxy>.
- class litestar.datastructures.ImmutableState[源代码]¶
-
An object meant to store arbitrary state.
It can be accessed using dot notation while exposing dict like functionalities.
- __copy__() Self[源代码]¶
Return a shallow copy of the given state object.
Customizes how the builtin "copy" function will work.
- classmethod __get_validators__() Generator[Callable[[ImmutableState | dict[str, Any] | Iterable[tuple[str, Any]]], ImmutableState], None, None][源代码]¶
Pydantic compatible method to allow custom parsing of state instances in a SignatureModel.
- __getattr__(key: str) Any[源代码]¶
Get the value for the corresponding key from the wrapped state object using attribute notation.
- 参数:
key¶ -- Key to retrieve
- 抛出:
AttributeError -- if the given attribute is not set.
- 返回:
The retrieved value
- __getitem__(key: str) Any[源代码]¶
Get the value for the corresponding key from the wrapped state object using subscription notation.
- __init__(state: ImmutableState | Mapping[str, Any] | Iterable[tuple[str, Any]], deep_copy: bool = True) None[源代码]¶
Initialize an
ImmutableStateinstance.- 参数:
state¶ -- An object to initialize the state from. Can be a dict, an instance of
ImmutableState, or a tuple of key value paris.deep_copy¶ -- Whether to 'deepcopy' the passed in state.
示例
from litestar.datastructures import ImmutableState state_dict = {"first": 1, "second": 2, "third": 3, "fourth": 4} state = ImmutableState(state_dict) # state implements the Mapping type: assert len(state) == 3 assert "first" in state assert not "fourth" in state assert state["first"] == 1 assert [(k, v) for k, v in state.items()] == [("first", 1), ("second", 2), ("third", 3)] # state implements __bool__ assert state # state is true when it has values. assert not State() # state is empty when it has no values. # it has a 'dict' method to retrieve a shallow copy of the underlying dict inner_dict = state.dict() assert inner_dict == state_dict # you can also retrieve a mutable State by calling 'mutable_copy' mutable_state = state.mutable_copy() del state["first"] assert "first" not in state
- __iter__() Iterator[str][源代码]¶
Return an iterator iterating the wrapped state dict.
- 返回:
An iterator of strings
- classmethod validate(value: ImmutableState | dict[str, Any] | Iterable[tuple[str, Any]]) Self[源代码]¶
Parse a value and instantiate state inside a SignatureModel. This allows us to use custom subclasses of state, as well as allows users to decide whether state is mutable or immutable.
- 参数:
value¶ -- The value from which to initialize the state instance.
- 返回:
An ImmutableState instance
- class litestar.datastructures.MultiDict[源代码]¶
基类:
MultiDict[T],MultiMixin[T],Generic[T]MultiDict, using
MultiDict.- __init__(args: MultiMapping | Mapping[str, T] | Iterable[tuple[str, T]] | None = None) None[源代码]¶
Initialize
MultiDictfrom a`MultiMapping``,Mappingor an iterable of tuples.- 参数:
args¶ -- Mapping-like structure to create the
MultiDictfrom
- immutable() ImmutableMultiDict[T][源代码]¶
Create an.
ImmutableMultiDictview.- 返回:
An immutable multi dict
- class litestar.datastructures.MultiMixin[源代码]¶
基类:
Generic[T],MultiMapping[T],ABCMixin providing common methods for multi dicts, used by
ImmutableMultiDictandMultiDict
- class litestar.datastructures.MutableScopeHeaders[源代码]¶
-
A case-insensitive, multidict-like structure that can be used to mutate headers within a
Scope- __init__(scope: HeaderScope | None = None) None[源代码]¶
Initialize
MutableScopeHeadersfrom aHeaderScope.- 参数:
scope¶ -- The ASGI connection scope.
- extend_header_value(key: str, value: str) None[源代码]¶
Extend a multivalued header.
备注
A multivalues header is a header that can take a comma separated list.
If the header previously did not exist, it will be added.
- classmethod from_message(message: Message) MutableScopeHeaders[源代码]¶
Construct a header from a message object.
- 参数:
- 返回:
MutableScopeHeaders.
- 抛出:
ValueError -- If the message does not have a
headerskey.
- class litestar.datastructures.ResponseHeader[源代码]¶
基类:
objectContainer type for a response header.
- __init__(name: str, documentation_only: bool = False, value: str | None = None, description: str | None = None, required: bool = False, deprecated: bool = False, style: str | None = None, explode: bool | None = None, example: Any | None = None, examples: dict[str, Example] | None = None) None¶
- __post_init__() None[源代码]¶
Ensure that either value is set or the instance is for documentation_only.
- deprecated: bool = False¶
Specifies that a parameter is deprecated and SHOULD be transitioned out of usage.
Default value is false.
- description: str | None = None¶
A brief description of the parameter. This could contain examples of use.
[CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation.
- documentation_only: bool = False¶
Defines the ResponseHeader instance as for OpenAPI documentation purpose only.
- example: Any | None = None¶
Example of the parameter's potential value.
The example SHOULD match the specified schema and encoding properties if present. The example field is mutually exclusive of the examples field. Furthermore, if referencing a schema that contains an example, the example value SHALL _override_ the example provided by the schema. To represent examples of media types that cannot naturally be represented in JSON or YAML, a string value can contain the example with escaping where necessary.
- examples: dict[str, Example] | None = None¶
Examples of the parameter's potential value. Each example SHOULD contain a value in the correct format as specified in the parameter encoding. The examples field is mutually exclusive of the example field. Furthermore, if referencing a schema that contains an example, the examples value SHALL _override_ the example provided by the schema.
For more complex scenarios, the [content](https://spec.openapis.org/oas/v3.1.0#parameterContent) property can define the media type and schema of the parameter. A parameter MUST contain either a schema property, or a content property, but not both. When example or examples are provided in conjunction with the schema object, the example MUST follow the prescribed serialization strategy for the parameter.
- explode: bool | None = None¶
When this is true, parameter values of type array or object generate separate parameters for each value of the array or key-value pair of the map.
For other types of parameters this property has no effect. When [style](https://spec.openapis.org/oas/v3.1.0#parameterStyle) is form, the default value is true. For all other styles, the default value is false.
- required: bool = False¶
Determines whether this parameter is mandatory.
If the [parameter location](https://spec.openapis.org/oas/v3.1.0#parameterIn) is "path", this property is REQUIRED and its value MUST be true. Otherwise, the property MAY be included and its default value is false.
- class litestar.datastructures.SecretBytes[源代码]¶
基类:
SecretValue[bytes]Represents a secret bytes value.
- class litestar.datastructures.SecretString[源代码]¶
基类:
SecretValue[str]Represents a secret string value.
- class litestar.datastructures.State[源代码]¶
基类:
ImmutableState,MutableMapping[str,Any]An object meant to store arbitrary state.
It can be accessed using dot notation while exposing dict like functionalities.
- __delattr__(key: str) None[源代码]¶
Delete the value from the key from the wrapped state object using attribute notation.
- 参数:
key¶ -- Key to delete
- 抛出:
AttributeError -- if the given attribute is not set.
- 返回:
None
- __delitem__(key: str) None[源代码]¶
Delete the value from the key from the wrapped state object using subscription notation.
- __init__(state: ImmutableState | Mapping[str, Any] | Iterable[tuple[str, Any]] | None = None, deep_copy: bool = False) None[源代码]¶
Initialize a
Stateinstance with an optional value.- 参数:
Examples¶from litestar.datastructures import State state_dict = {"first": 1, "second": 2, "third": 3, "fourth": 4} state = State(state_dict) # state can be accessed using '.' notation assert state.fourth == 4 del state.fourth # state implements the Mapping type: assert len(state) == 3 assert "first" in state assert not "fourth" in state assert state["first"] == 1 assert [(k, v) for k, v in state.items()] == [("first", 1), ("second", 2), ("third", 3)] state["fourth"] = 4 assert "fourth" in state del state["fourth"] # state implements __bool__ assert state # state is true when it has values. assert not State() # state is empty when it has no values. # it has shallow copy copied_state = state.copy() del copied_state.first assert state.first # it has a 'dict' method to retrieve a shallow copy of the underlying dict inner_dict = state.dict() assert inner_dict == state_dict # you can get an immutable copy of the state by calling 'immutable_immutable_copy' immutable_copy = state.immutable_copy() del immutable_copy.first # raises AttributeError
- immutable_copy() ImmutableState[源代码]¶
Return a shallow copy of the state object, setting it to be frozen.
- 返回:
A
State
- class litestar.datastructures.UploadFile[源代码]¶
基类:
objectRepresentation of a file upload
- __init__(content_type: str, filename: str, file_data: bytes | None = None, headers: dict[str, str] | None = None, max_spool_size: int = 1048576) None[源代码]¶
Upload file in-memory container.
- async read(size: int = -1) bytes[源代码]¶
Proxy for data reading.
- 参数:
size¶ -- position from which to read.
- 返回:
Byte string.
- property rolled_to_disk: bool¶
Determine whether the spooled file exceeded the rolled-to-disk threshold and is no longer in memory.
- 返回:
A boolean flag