mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-26 07:40:57 -05:00
* 🌐 Refactor file structure to support internationalization * ✅ Update tests changed after i18n * 🔀 Merge Typer style from master * 🔧 Update MkConfig with Typer-styles * 🎨 Format mkdocs.yml with cannonical form * 🎨 Format mkdocs.yml * 🔧 Update MkDocs config * ➕ Add docs translation scripts dependencies * ✨ Add Typer scripts to handle translations * ✨ Add missing translation snippet to include * ✨ Update contributing docs, add docs for translations * 🙈 Add docs_build to gitignore * 🔧 Update scripts with new locations and docs scripts * 👷 Update docs deploy action with translations * 📝 Add note about languages not supported in the theme * ✨ Add first translation, for Spanish
40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
import time
|
|
from typing import Callable
|
|
|
|
from fastapi import APIRouter, FastAPI, Request, Response
|
|
from fastapi.routing import APIRoute
|
|
|
|
|
|
class TimedRoute(APIRoute):
|
|
def get_route_handler(self) -> Callable:
|
|
original_route_handler = super().get_route_handler()
|
|
|
|
async def custom_route_handler(request: Request) -> Response:
|
|
before = time.time()
|
|
response: Response = await original_route_handler(request)
|
|
duration = time.time() - before
|
|
response.headers["X-Response-Time"] = str(duration)
|
|
print(f"route duration: {duration}")
|
|
print(f"route response: {response}")
|
|
print(f"route response headers: {response.headers}")
|
|
return response
|
|
|
|
return custom_route_handler
|
|
|
|
|
|
app = FastAPI()
|
|
router = APIRouter(route_class=TimedRoute)
|
|
|
|
|
|
@app.get("/")
|
|
async def not_timed():
|
|
return {"message": "Not timed"}
|
|
|
|
|
|
@router.get("/timed")
|
|
async def timed():
|
|
return {"message": "It's the time of my life"}
|
|
|
|
|
|
app.include_router(router)
|