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] *} {* ../../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`: You can also set the `router_class` parameter of an `APIRouter`:
{* ../../docs_src/custom_api_router/tutorial001.py hl[100:102] *} {* ../../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. 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. - **`TimedRoute`**: Measures request duration and adds `X-Response-Time` to response headers.
- **`AppRouter`**: A custom router that: - **`AppRouter`**: A custom router that:
- Supports **nested routers** with automatic hierarchical route naming. - Supports **nested routers** with automatic hierarchical route naming.
- Includes a **built-in `/healthz` endpoint** for every router. - Includes a **built-in `/healthz` endpoint** for every router.
- Ensures **clean API structure** with logical parent-child relationships. - 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`. - **`/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/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 - **`/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`. router with path name `Model.Item.create`.
##### 🔥 Benefits ##### 🔥 Benefits { #benefits }
- **Clear & maintainable API design** with structured route naming. - **Clear & maintainable API design** with structured route naming.
- **Built-in health checks** for easier observability. - **Built-in health checks** for easier observability.

11
docs_src/custom_api_router/tutorial001.py

@ -1,5 +1,6 @@
import time 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 import APIRouter, FastAPI, Request, Response
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
@ -36,7 +37,7 @@ class AppRouter(APIRouter):
tags = tags or [] tags = tags or []
tags.insert(0, name) tags.insert(0, name)
super().__init__(prefix=prefix, tags=tags, **kwargs) super().__init__(prefix=prefix, tags=tags, **kwargs)
self._parent: Optional[AppRouter] = None self._parent: AppRouter | None = None
self._add_health_check() self._add_health_check()
@property @property
@ -82,9 +83,9 @@ class AppRouter(APIRouter):
def add_route( def add_route(
self, self,
path: str, path: str,
endpoint: Callable[[Request], Union[Awaitable[Response], Response]], endpoint: Callable[[Request], Awaitable[Response] | Response],
methods: Union[List[str], None] = None, methods: list[str] | None = None,
name: Union[str, None] = None, name: str | None = None,
include_in_schema: bool = True, include_in_schema: bool = True,
) -> None: ) -> None:
name = f"{self.request_name_prefix}.{name}" name = f"{self.request_name_prefix}.{name}"

Loading…
Cancel
Save