From 88b9a850b32e25ff7cc69b2ee98bf159a9d8db6b Mon Sep 17 00:00:00 2001 From: ItayTheDar Date: Thu, 4 Jan 2024 10:20:35 +0200 Subject: [PATCH 1/2] test pull request --- .github/workflows/integration_test.yaml | 30 ++++++------ nest/core/app.py | 10 +--- nest/core/decorators/controller.py | 61 +++++++------------------ 3 files changed, 34 insertions(+), 67 deletions(-) diff --git a/.github/workflows/integration_test.yaml b/.github/workflows/integration_test.yaml index 171a5c6..4959841 100644 --- a/.github/workflows/integration_test.yaml +++ b/.github/workflows/integration_test.yaml @@ -1,6 +1,6 @@ name: Integration Test -on: [push, workflow_call] +on: [ push, workflow_call ] jobs: test: @@ -8,7 +8,7 @@ jobs: strategy: matrix: - app_type: ["Blank", "SyncORM", "AsyncORM"] + app_type: [ "Blank", "SyncORM", "AsyncORM" ] steps: - name: Check out repository code @@ -48,18 +48,18 @@ jobs: pynest g module -n user uvicorn "app:app" --host "0.0.0.0" --port 8000 --reload & - - name: Wait for the server to start - run: sleep 10 + - name: Wait for the server to start + run: sleep 10 - - name: Test the application - run: | - curl -f http://localhost:8000/docs - curl -f -X 'POST' \ - "http://localhost:8000/user/" \ - -H 'accept: application/json' \ - -H 'Content-Type: application/json' \ - -d '{"name": "Example Name"}' - curl -f http://localhost:8000/user/ + - name: Test the application + run: | + curl -f http://localhost:8000/docs + curl -f -X 'POST' \ + "http://localhost:8000/user/" \ + -H 'accept: application/json' \ + -H 'Content-Type: application/json' \ + -d '{"name": "Example Name"}' + curl -f http://localhost:8000/user/ - - name: Kill the server - run: kill $(jobs -p) || true + - name: Kill the server + run: kill $(jobs -p) || true 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: """ From 1bd3ab09bfbf6453695df20507576dd0e8204aab Mon Sep 17 00:00:00 2001 From: ItayTheDar Date: Thu, 4 Jan 2024 10:23:41 +0200 Subject: [PATCH 2/2] test pull request --- .github/workflows/integration_test.yaml | 30 ++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/integration_test.yaml b/.github/workflows/integration_test.yaml index 4959841..1a59459 100644 --- a/.github/workflows/integration_test.yaml +++ b/.github/workflows/integration_test.yaml @@ -1,6 +1,6 @@ name: Integration Test -on: [ push, workflow_call ] +on: [push, workflow_call] jobs: test: @@ -8,7 +8,7 @@ jobs: strategy: matrix: - app_type: [ "Blank", "SyncORM", "AsyncORM" ] + app_type: ["Blank", "SyncORM", "AsyncORM"] steps: - name: Check out repository code @@ -48,18 +48,18 @@ jobs: pynest g module -n user uvicorn "app:app" --host "0.0.0.0" --port 8000 --reload & - - name: Wait for the server to start - run: sleep 10 + - name: Wait for the server to start + run: sleep 10 - - name: Test the application - run: | - curl -f http://localhost:8000/docs - curl -f -X 'POST' \ - "http://localhost:8000/user/" \ - -H 'accept: application/json' \ - -H 'Content-Type: application/json' \ - -d '{"name": "Example Name"}' - curl -f http://localhost:8000/user/ + - name: Test the application + run: | + curl -f http://localhost:8000/docs + curl -f -X 'POST' \ + "http://localhost:8000/user/" \ + -H 'accept: application/json' \ + -H 'Content-Type: application/json' \ + -d '{"name": "Example Name"}' + curl -f http://localhost:8000/user/ - - name: Kill the server - run: kill $(jobs -p) || true + - name: Kill the server + run: kill $(jobs -p) || true \ No newline at end of file