Middleware protocol and Next type alias.
A middleware is any callable matching::
async def my_mw(request: Request, next: Next) -> AnyResponse: ...
No base class required. The framework checks the shape, not the lineage.
Thenext callable may return Response, StreamingResponse,
orSSEResponse. All three share the .with_header()/
.with_status()chainable API, so middleware can modify them
uniformly.
middleware.protocol
| Name | Type | Default | Description |
|---|---|---|---|
type
|
|
— | |
qualified_name
|
|
— | |
element_type
|
|
— | |
description
|
|
— | |
source_file
|
|
— | |
line_number
|
|
— | |
is_autodoc
|
|
— | |
autodoc_element
|
|
— | |
_autodoc_template
|
|
— | |
_autodoc_url_path
|
|
— | |
_autodoc_page_type
|
|
— | |
title
|
|
— | |
doc_content_hash
|
|
— |
Symbols on this page
Middleware
class
Protocol for chirp middleware.
Accepts both functions and callable objects::
# Function middleware
async def timing(request: Request, next: Next) -> AnyResponse:
start = time.monotonic()
response = await next(request)
elapsed = time.monotonic() - start
return response.with_header("X-Time", f"{elapsed:.3f}")
# Class middleware
class RateLimiter:
async def __call__(self, request: Request, next: Next) -> AnyResponse:
...
View source · /home/runner/work/chirp/chirp/site/../src/chirp/middleware/protocol.py:1