diff --git a/.github/workflows/integration_test.yaml b/.github/workflows/integration_test.yaml index 171a5c6..1a59459 100644 --- a/.github/workflows/integration_test.yaml +++ b/.github/workflows/integration_test.yaml @@ -62,4 +62,4 @@ jobs: curl -f http://localhost:8000/user/ - name: Kill the server - run: kill $(jobs -p) || true + run: kill $(jobs -p) || true \ No newline at end of file diff --git a/nest/core/app.py b/nest/core/app.py index 300e3f5..1432515 100644 --- a/nest/core/app.py +++ b/nest/core/app.py @@ -4,11 +4,7 @@ class App(FastAPI): def __init__( - self, - description: str, - modules: List, - title: str = "PyNest Service", - **kwargs + self, description: str, modules: List, title: str = "PyNest Service", **kwargs ): """ Initializes the App instance. @@ -18,9 +14,7 @@ def __init__( modules (List): A list of modules to register. """ - super().__init__( - description=description, title=title, **kwargs - ) + super().__init__(description=description, title=title, **kwargs) self.modules = modules self._register_controllers() diff --git a/nest/core/decorators/controller.py b/nest/core/decorators/controller.py index fe02f87..1f0222f 100644 --- a/nest/core/decorators/controller.py +++ b/nest/core/decorators/controller.py @@ -26,52 +26,25 @@ def Controller(tag: str = None, prefix: str = None): def wrapper(cls) -> ClassBasedView: router = APIRouter(tags=[tag] if tag else None) + http_method_names = ("GET", "POST", "PUT", "DELETE", "PATCH") + for name, method in cls.__dict__.items(): if callable(method) and hasattr(method, "method"): - if not method.__path__: - raise Exception("Missing path") - else: - if prefix: - method.__path__ = prefix + method.__path__ - if not method.__path__.startswith("/"): - method.__path__ = "/" + method.__path__ - if method.method == "GET": - router.add_api_route( - method.__path__, - method, - methods=["GET"], - **method.__kwargs__, - ) - elif method.method == "POST": - router.add_api_route( - method.__path__, - method, - methods=["POST"], - **method.__kwargs__, - ) - elif method.method == "PUT": - router.add_api_route( - method.__path__, - method, - methods=["PUT"], - **method.__kwargs__, - ) - elif method.method == "DELETE": - router.add_api_route( - method.__path__, - method, - methods=["DELETE"], - **method.__kwargs__, - ) - elif method.method == "PATCH": - router.add_api_route( - method.__path__, - method, - methods=["PATCH"], - **method.__kwargs__, - ) - else: - raise Exception("Invalid method") + # Check if method is decorated with an HTTP method decorator + assert ( + hasattr(method, "__path__") and method.__path__ + ), f"Missing path for method {name}" + + http_method = method.method + # Ensure that the method is a valid HTTP method + assert http_method in http_method_names, f"Invalid method {http_method}" + if prefix: + method.__path__ = prefix + method.__path__ + if not method.__path__.startswith("/"): + method.__path__ = "/" + method.__path__ + router.add_api_route( + method.__path__, method, methods=[http_method], **method.__kwargs__ + ) def get_router() -> APIRouter: """