Browse Source

🎨 Auto format

pull/13483/head
pre-commit-ci-lite[bot] 1 month ago
committed by GitHub
parent
commit
ca5fd2cd5a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 10
      docs/en/docs/how-to/custom-request-and-route.md
  2. 11
      docs_src/custom_api_router/tutorial001.py

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

11
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}"

Loading…
Cancel
Save