template-fastapi-react
template-fastapi-react copied to clipboard
feat(api): add common tag to all instances of `APIRouter`
Describe Problem
Currently, each feature has an APIRouter instance defined in the *_feature.py (controller) which includes a list of tags:
https://github.com/equinor/template-fastapi-react/blob/bc33372bd57d7b8710cd7460f1c18c6b55c38634/api/src/features/todo/todo_feature.py#L18
When the OpenAPI docs are generated, it groups endpoints by these tags. When generating API clients with openapi-generator-cli, the script will generate separate API client classes per group (tag) - e.g. one API client class for the endpoints tagged with "todo", another for endpoints tagged with "whoami", and so on.
In order to simplify usage of the auto-generated API clients, we should add a shared tag to all instances of APIRouter so that a single API client class is generated.
Suggest Solution
-
Define
APP_NAME/SHARED_TAGin config.py -
Use
APP_NAME/SHARED_TAGas a shared tag -
Define variable:
# config.py
[...]
class Config(BaseSettings):
APP_NAME: str = Field("API", env="APP_NAME")
[...]
- Use shared tag:
# features/<feature>/<feature>_feature.py
# features/todo/todo_feature.py
from config import config
router = APIRouter(tags=[config.APP_NAME, "todos"], prefix="/todos")
-
Rerun
openapi-generator-cli: -
Update API client wrapper: https://github.com/equinor/template-fastapi-react/blob/bc33372bd57d7b8710cd7460f1c18c6b55c38634/web/src/api/TodoAPI.tsx#L1-L3