feat: routers client and version prefixes
This commit is contained in:
@ -5,6 +5,8 @@ from types import ModuleType
|
||||
|
||||
from fastapi import FastAPI, APIRouter
|
||||
|
||||
names_to_ignore = ["routers", "modules"]
|
||||
|
||||
|
||||
def auto_include_routers(app: FastAPI, package: ModuleType, full_path: bool = False):
|
||||
"""
|
||||
@ -16,7 +18,6 @@ def auto_include_routers(app: FastAPI, package: ModuleType, full_path: bool = Fa
|
||||
"""
|
||||
|
||||
package_path = Path(package.__file__).parent
|
||||
base_pkg_name = package.__name__.replace("_", "-")
|
||||
|
||||
for _, name, _ in pkgutil.walk_packages(package.__path__, package.__name__ + "."):
|
||||
module = importlib.import_module(name)
|
||||
@ -33,7 +34,7 @@ def auto_include_routers(app: FastAPI, package: ModuleType, full_path: bool = Fa
|
||||
parts: list[str] = list(relative_path.parts)
|
||||
|
||||
# Remove "routers" folder(s) from prefix parts
|
||||
parts = [p for p in parts if p != "routers"]
|
||||
parts = [p for p in parts if p not in names_to_ignore]
|
||||
|
||||
# Drop extension from filename
|
||||
parts[-1] = parts[-1].replace(".py", "")
|
||||
@ -41,7 +42,6 @@ def auto_include_routers(app: FastAPI, package: ModuleType, full_path: bool = Fa
|
||||
if full_path:
|
||||
# Use full path
|
||||
prefix_parts = [p.replace("_", "-") for p in parts if p != "__init__"]
|
||||
prefix_parts.insert(0, base_pkg_name)
|
||||
else:
|
||||
# Only use last file name
|
||||
prefix_parts = [parts[-1].replace("_", "-")]
|
||||
|
||||
Reference in New Issue
Block a user