ushka.http.params
Declared handler parameters: typed path/query/header/cookie/body and Depends.
A handler states what it needs in its signature and Ushka resolves it, then
validates it (see ushka.http.validation):
- a path parameter whose name is in the route (
[item_id]) is converted to its annotation; Query(),Header(),Cookie(),Body(),Form()andPath()pick the source explicitly (Annotated[Signup, Form()]fills a whole dataclass from an HTML form);- a plain scalar annotation (
limit: int = 10) is a query parameter; - a dataclass,
TypedDict,dictorlistannotation is the JSON body; - an
UploadFile(orlist[UploadFile]) annotation is a file of a multipart form; Depends(fn)runsfnfirst and passes you its result.
request, response and config keep working by name or annotation.
In this module
class Param | Base of Query, Header, Cookie, Path, Body and Form. |
class Query | |
class Header | A request header. user_agent is read as user-agent unless you set alias. |
class Cookie | |
class Path | |
class Body | |
class Form | |
def on_invalid() | Names the function that answers when a route's parameters fail validation. |
class Depends | user = Depends(current_user): call dependency and inject its result. |
def wants_upload() | True for UploadFile, UploadFile | None, list[UploadFile], Annotated[...] of those. |
def build_plan() | |
class Resolver | Per-request state: the dependency cache and what to clean up afterwards. |
class Param (Field)
class Query (Param)
class Header (Param)
A request header. user_agent is read as user-agent unless you set alias.
class Cookie (Param)
class Path (Param)
class Body (Param)
class Form (Param)
def on_invalid
def on_invalid(handler: Callable[..., Any]) -> Callable[[Callable], Callable]Names the function that answers when a route's parameters fail validation.
For file routes (@app.post("/x", on_invalid=fn) does the same):
@on_invalid(show_form)
async def post(lead: Annotated[Lead, Form()]): ...
handler is an ordinary handler: it may take request, Depends(...)
and errors: RequestValidationError (with .fields and .values).
class Depends
user = Depends(current_user): call dependency and inject its result.
The dependency may be sync or async, may itself declare parameters and
Depends, and is called once per request (use_cache=False to call
it every time it is asked for). A generator dependency (yield) can
clean up after the handler returns. Override in tests with
app.dependency_overrides[original] = replacement.
def wants_upload
def wants_upload(tp: Any) -> boolTrue for UploadFile, UploadFile | None, list[UploadFile], Annotated[...] of those.
def build_plan
def build_plan(
func: Callable,
path_names: frozenset,
method: str,
) -> list[_Spec]class Resolver
Per-request state: the dependency cache and what to clean up afterwards.
close
async def close() -> Nonecall
async def call(func: Callable, supplied: dict | None = None) -> dictBuilds the keyword arguments for func. supplied holds what the
router already injected (request, response, config, path values).
Raises RequestValidationError listing every problem.