mirror of https://github.com/tiangolo/fastapi.git
📝 Add annotations to HTTP middleware example (#11530)
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
parent
9957956ef8
commit
dfe9dde982
|
|
@ -4515,14 +4515,17 @@ class FastAPI(Starlette):
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import time
|
import time
|
||||||
|
from typing import Awaitable, Callable
|
||||||
|
|
||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request, Response
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
@app.middleware("http")
|
@app.middleware("http")
|
||||||
async def add_process_time_header(request: Request, call_next):
|
async def add_process_time_header(
|
||||||
|
request: Request, call_next: Callable[[Request], Awaitable[Response]]
|
||||||
|
) -> Response:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
response = await call_next(request)
|
response = await call_next(request)
|
||||||
process_time = time.time() - start_time
|
process_time = time.time() - start_time
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue