Ushka

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() and Path() 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, dict or list annotation is the JSON body;
  • an UploadFile (or list[UploadFile]) annotation is a file of a multipart form;
  • Depends(fn) runs fn first and passes you its result.

request, response and config keep working by name or annotation.

In this module

class ParamBase of Query, Header, Cookie, Path, Body and Form.
class Query
class HeaderA 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 Dependsuser = 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 ResolverPer-request state: the dependency cache and what to clean up afterwards.

class Param (Field)

Base of Query, Header, Cookie, Path, Body and Form.

class Query (Param)

class Header (Param)

A request header. user_agent is read as user-agent unless you set alias.

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) -> bool

True 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() -> None

call

async def call(func: Callable, supplied: dict | None = None) -> dict

Builds the keyword arguments for func. supplied holds what the router already injected (request, response, config, path values). Raises RequestValidationError listing every problem.