template-fastapi-react icon indicating copy to clipboard operation
template-fastapi-react copied to clipboard

feat(api): add common tag to all instances of `APIRouter`

Open netr0m opened this issue 3 years ago • 0 comments

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

  1. Define APP_NAME / SHARED_TAG in config.py

  2. Use APP_NAME / SHARED_TAG as a shared tag

  3. Define variable:

# config.py
[...]
class Config(BaseSettings):
    APP_NAME: str = Field("API", env="APP_NAME")
    [...]
  1. 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")
  1. Rerun openapi-generator-cli:

  2. Update API client wrapper: https://github.com/equinor/template-fastapi-react/blob/bc33372bd57d7b8710cd7460f1c18c6b55c38634/web/src/api/TodoAPI.tsx#L1-L3

Additional Details

netr0m avatar Aug 17 '22 08:08 netr0m