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 %}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too