-
-
Notifications
You must be signed in to change notification settings - Fork 144
Description
Hi Koudai, I want to take the time to appreciate what you have created here. It's incredibly easy to use and amenable to customization.
I am using this awesome tool in a way I think is interesting (maybe to me alone). I modified the templates such that generated codes are never modified. Instead, I create another directory, which I call services, with modules corresponding to each router module. Each service module implements the corresponding endpoints defined in the router module. My templates already ensure that the right service module is imported into the router module and called with the same parameters.
Now the question: I want to implement dependency injection while keeping to the promise of not modifying the generated files but I'm not sure how best to achieve this. So, I would like to know if the way I'm approaching the problem is the right way or there are better ways.
Below, I provide the structure of my API.
api
├── data_models.py
├── database.py
├── dependencies.py
├── main.py
├── models.py
├── routers
│ ├── authentication.py
│ ├── dashboard.py
│ ├── model_execution.py
│ ├── settings.py
│ └── user_management.py
├── services
│ ├── authentication.py
│ ├── dashboard.py
│ ├── model_execution.py
│ ├── settings.py
│ └── user_management.py
└── utils
└── auth.py
Also, below is routers.jinja2 template
from __future__ import annotations
from fastapi import APIRouter
from ..dependencies import *
{% for operation in operations %}
{% if operation.tags[0] == tag %}
from ..services import {{ operation.tags[0].lower().replace(" ","_") }}
{% endif %}
{% endfor %}
router = APIRouter(
tags=["{{tag}}"]
)
{% for operation in operations %}
{% if operation.tags[0] == tag %}
@router.{{operation.type}}("{{operation.snake_case_path}}", response_model={{operation.response}}
{% if operation.additional_responses %}
, responses={
{% for status_code, models in operation.additional_responses.items() %}
"{{ status_code }}": {
{% for key, model in models.items() %}
"{{ key }}": {{ model }}{% if not loop.last %},{% endif %}
{% endfor %}
}{% if not loop.last %},{% endif %}
{% endfor %}
}
{% endif %}
{% if operation.tags%}
, tags={{operation.tags}}
{% endif %})
async def {{operation.function_name}}({{operation.snake_case_arguments}}) -> {{operation.return_type}}:
{%- if operation.summary %}
"""
{{ operation.summary }}
"""
{%- endif %}
{{ operation.tags[0].lower().replace(" ","_") }}.{{ operation.function_name}}({{ operation.snake_case_arguments.split(":")[0] }})
pass
{% endif %}
{% endfor %}