Module

middleware.protocol

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.

Classes

Middleware 1
Protocol for chirp middleware. Accepts both functions and callable objects:: # Function middl…

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:
        ...

Methods

Internal Methods 1
__call__ 2 AnyResponse
async
async def __call__(self, request: Request, next: Next) -> AnyResponse
Parameters
Name Type Description
request
next
Returns
AnyResponse