From ca5fd2cd5a81efba5038cf60128250cb283779dc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Sun, 3 May 2026 08:32:02 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/docs/how-to/custom-request-and-route.md | 10 +++++----- docs_src/custom_api_router/tutorial001.py | 11 ++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/en/docs/how-to/custom-request-and-route.md b/docs/en/docs/how-to/custom-request-and-route.md index 4c56a09af2..62db622ccb 100644 --- a/docs/en/docs/how-to/custom-request-and-route.md +++ b/docs/en/docs/how-to/custom-request-and-route.md @@ -108,29 +108,29 @@ In this example, the *path operations* under the `router` will use the custom `T {* ../../docs_src/custom_request_and_route/tutorial003_py310.py hl[13:20] *} -## Custom `APIRouter` class in a router +## Custom `APIRouter` class in a router { #custom-apirouter-class-in-a-router } You can also set the `router_class` parameter of an `APIRouter`: {* ../../docs_src/custom_api_router/tutorial001.py hl[100:102] *} -#### 🚀 Custom FastAPI Router with Timed Responses +#### 🚀 Custom FastAPI Router with Timed Responses { #custom-fastapi-router-with-timed-responses } This example enhances FastAPI with structured routing and response timing, making APIs more organized and observable. -##### ✨ Features +##### ✨ Features { #features } - **`TimedRoute`**: Measures request duration and adds `X-Response-Time` to response headers. - **`AppRouter`**: A custom router that: - Supports **nested routers** with automatic hierarchical route naming. - Includes a **built-in `/healthz` endpoint** for every router. - Ensures **clean API structure** with logical parent-child relationships. -##### 📌 API Structure +##### 📌 API Structure { #api-structure } - **`/healthz`**: Health check endpoint for the main router. it path name is `Global.health-check`. - **`/model/create`**: Model creation endpoint for the model router with path name `Model.create`. - **`/model/{model_id}/item/create`**: Item creation endpoint for the item router and its child router of model router with path name `Model.Item.create`. -##### 🔥 Benefits +##### 🔥 Benefits { #benefits } - **Clear & maintainable API design** with structured route naming. - **Built-in health checks** for easier observability. diff --git a/docs_src/custom_api_router/tutorial001.py b/docs_src/custom_api_router/tutorial001.py index 9fd1040444..cf37fe4236 100644 --- a/docs_src/custom_api_router/tutorial001.py +++ b/docs_src/custom_api_router/tutorial001.py @@ -1,5 +1,6 @@ import time -from typing import Any, Awaitable, Callable, List, Optional, Set, Union +from collections.abc import Awaitable, Callable +from typing import Any, List, Set, Union from fastapi import APIRouter, FastAPI, Request, Response from fastapi.responses import JSONResponse @@ -36,7 +37,7 @@ class AppRouter(APIRouter): tags = tags or [] tags.insert(0, name) super().__init__(prefix=prefix, tags=tags, **kwargs) - self._parent: Optional[AppRouter] = None + self._parent: AppRouter | None = None self._add_health_check() @property @@ -82,9 +83,9 @@ class AppRouter(APIRouter): def add_route( self, path: str, - endpoint: Callable[[Request], Union[Awaitable[Response], Response]], - methods: Union[List[str], None] = None, - name: Union[str, None] = None, + endpoint: Callable[[Request], Awaitable[Response] | Response], + methods: list[str] | None = None, + name: str | None = None, include_in_schema: bool = True, ) -> None: name = f"{self.request_name_prefix}.{name}"