tiangolo / fastapi

FastAPI framework, high performance, easy to learn, fast to code, ready for production
https://fastapi.tiangolo.com/
MIT License
73.34k stars 6.18k forks source link

📝 Add annotations to HTTP middleware example #11530

Open Kilo59 opened 2 months ago

Kilo59 commented 2 months ago

Update documentation example for HTTP middleware to complete type annotations in the signature.

import time
from typing import Awaitable, Callable

from fastapi import FastAPI, Request, Response

app = FastAPI()

@app.middleware("http")
async def add_process_time_header(
    request: Request, call_next: Callable[[Request], Awaitable[Response]]
) -> Response:
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response
tadasgedgaudas commented 1 month ago

looks good :+1: