refactor: move versioning efforts to initalization

This commit is contained in:
Alexandre Teles
2023-11-21 01:51:44 -03:00
committed by Alexandre Teles (afterSt0rm)
parent 8d36663610
commit 42c88290b1
14 changed files with 101 additions and 49 deletions

View File

@@ -2,15 +2,26 @@
from sanic import Blueprint
import importlib
import pkgutil
from api.utils.versioning import get_version
blueprints = []
for _, module_name, _ in pkgutil.iter_modules(["api"]):
# Import the module
module = importlib.import_module(f"api.{module_name}")
# Dynamically import all modules in the 'api' package, excluding subdirectories
versioned_blueprints = {}
for finder, module_name, ispkg in pkgutil.iter_modules(["api"]):
if not ispkg:
# Import the module
module = importlib.import_module(f"api.{module_name}")
# Add the module's blueprint to the list, if it exists
if hasattr(module, module_name):
blueprints.append(getattr(module, module_name))
# Add the module's blueprint to the versioned list, if it exists
if hasattr(module, module_name):
blueprint = getattr(module, module_name)
version = get_version(module_name)
versioned_blueprints.setdefault(version, []).append(blueprint)
# Create the Blueprint group with the dynamically imported blueprints
api = Blueprint.group(*blueprints, url_prefix="/")
# Create Blueprint groups for each version
api = []
for version, blueprints in versioned_blueprints.items():
if version == "old":
group = Blueprint.group(*blueprints, url_prefix="/")
else:
group = Blueprint.group(*blueprints, version=version, url_prefix="/")
api.append(group)