Skip to content

Commit 1c24bcc

Browse files
Lucasal072lucas alvesItayTheDar
authored
refactor controller routes (#46)
* refactor: refactor route decorators to use a common helper function and remove redundant code * run black * add event triggres to run actions * fix: HTTP methods in controller decorators * move http methods decorators to seperate file --------- Co-authored-by: lucas alves <[email protected]> Co-authored-by: ItayTheDar <[email protected]>
1 parent 881ef5a commit 1c24bcc

File tree

11 files changed

+107
-126
lines changed

11 files changed

+107
-126
lines changed

.github/workflows/cli_test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: CLI Test
22

3-
on: [ push, workflow_call ]
3+
on: [ push, workflow_call, pull_request ]
44

55
jobs:
66
test:

.github/workflows/integration_test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Integration Test
22

3-
on: [push, workflow_call]
3+
on: [push, workflow_call, pull_request]
44

55
jobs:
66
test:

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Tests
22

3-
on: [push, workflow_call]
3+
on: [push, workflow_call, pull_request]
44

55
jobs:
66
test:

nest/common/templates/mysql_template.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def requirements_file(self):
3636
"""
3737

3838

39-
4039
class AsyncMySQLTemplate(AsyncORMTemplate, ABC):
4140
def __init__(self, module_name: str):
4241
super().__init__(

nest/common/templates/orm_template.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ def config_file(self):
225225
def requirements_file(self):
226226
pass
227227

228-
229228
def entity_file(self):
230229
return f"""from config import config
231230
from sqlalchemy import Integer, String

nest/core/app.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44

55
class App(FastAPI):
66
def __init__(
7-
self,
8-
description: str,
9-
modules: List,
10-
title: str = "PyNest Service",
11-
**kwargs
7+
self, description: str, modules: List, title: str = "PyNest Service", **kwargs
128
):
139
"""
1410
Initializes the App instance.
@@ -18,9 +14,7 @@ def __init__(
1814
modules (List): A list of modules to register.
1915
2016
"""
21-
super().__init__(
22-
description=description, title=title, **kwargs
23-
)
17+
super().__init__(description=description, title=title, **kwargs)
2418
self.modules = modules
2519
self._register_controllers()
2620

nest/core/decorators/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
from nest.core.decorators.controller import Controller, Get, Post, Delete, Put, Patch
1+
from nest.core.decorators.controller import Controller
2+
from nest.core.decorators.http_methods import Get, Post, Delete, Put, Patch
23
from nest.core.decorators.database import db_request_handler, async_db_request_handler

nest/core/decorators/controller.py

Lines changed: 5 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from typing import Any
12
from fastapi.routing import APIRouter
2-
from nest.core.decorators.helpers import class_based_view as ClassBasedView
3+
from nest.core.decorators.helpers import (
4+
class_based_view as ClassBasedView,
5+
route_decorator,
6+
)
37

48

59
def Controller(tag: str = None, prefix: str = None):
@@ -58,113 +62,3 @@ def get_router() -> APIRouter:
5862
return ClassBasedView(router=router, cls=cls)
5963

6064
return wrapper
61-
62-
63-
def Get(path: str, **kwargs):
64-
"""
65-
Decorator that defines a GET route for the controller.
66-
67-
Args:
68-
path (str): The URL path for the route.
69-
**kwargs: Additional keyword arguments to configure the route.
70-
71-
Returns:
72-
function: The decorated function.
73-
74-
"""
75-
76-
def decorator(func):
77-
func.method = "GET"
78-
func.__path__ = path
79-
func.__kwargs__ = kwargs
80-
return func
81-
82-
return decorator
83-
84-
85-
def Post(path: str, **kwargs):
86-
"""
87-
Decorator that defines a POST route for the controller.
88-
89-
Args:
90-
path (str): The URL path for the route.
91-
**kwargs: Additional keyword arguments to configure the route.
92-
93-
Returns:
94-
function: The decorated function.
95-
96-
"""
97-
98-
def decorator(func):
99-
func.method = "POST"
100-
func.__path__ = path
101-
func.__kwargs__ = kwargs
102-
return func
103-
104-
return decorator
105-
106-
107-
def Delete(path: str, **kwargs):
108-
"""
109-
Decorator that defines a DELETE route for the controller.
110-
111-
Args:
112-
path (str): The URL path for the route.
113-
**kwargs: Additional keyword arguments to configure the route.
114-
115-
Returns:
116-
function: The decorated function.
117-
118-
"""
119-
120-
def decorator(func):
121-
func.method = "DELETE"
122-
func.__path__ = path
123-
func.__kwargs__ = kwargs
124-
return func
125-
126-
return decorator
127-
128-
129-
def Put(path: str, **kwargs):
130-
"""
131-
Decorator that defines a PUT route for the controller.
132-
133-
Args:
134-
path (str): The URL path for the route.
135-
**kwargs: Additional keyword arguments to configure the route.
136-
137-
Returns:
138-
function: The decorated function.
139-
140-
"""
141-
142-
def decorator(func):
143-
func.method = "PUT"
144-
func.__path__ = path
145-
func.__kwargs__ = kwargs
146-
return func
147-
148-
return decorator
149-
150-
151-
def Patch(path: str, **kwargs):
152-
"""
153-
Decorator that defines a PATCH route for the controller.
154-
155-
Args:
156-
path (str): The URL path for the route.
157-
**kwargs: Additional keyword arguments to configure the route.
158-
159-
Returns:
160-
function: The decorated function.
161-
162-
"""
163-
164-
def decorator(func):
165-
func.method = "PATCH"
166-
func.__path__ = path
167-
func.__kwargs__ = kwargs
168-
return func
169-
170-
return decorator

nest/core/decorators/helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from starlette.routing import Route, WebSocketRoute
1313

1414
T = TypeVar("T")
15+
K = TypeVar("K", bound=Callable[..., Any])
16+
1517

1618
CBV_CLASS_KEY = "__cbv_class__"
1719

@@ -102,3 +104,13 @@ def _update_cbv_route_endpoint_signature(
102104
]
103105
new_signature = old_signature.replace(parameters=new_parameters)
104106
setattr(route.endpoint, "__signature__", new_signature)
107+
108+
109+
def route_decorator(path: str, method: str, **kwargs: Any):
110+
def decorator(func: K) -> K:
111+
setattr(func, "method", method)
112+
setattr(func, "__path__", path)
113+
setattr(func, "__kwargs__", kwargs)
114+
return func
115+
116+
return decorator

nest/core/decorators/http_methods.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from typing import Any
2+
from nest.core.decorators.helpers import route_decorator
3+
4+
5+
def Get(path: str, **kwargs: Any):
6+
"""
7+
Decorator that defines a GET route for the controller.
8+
9+
Args:
10+
path (str): The URL path for the route.
11+
**kwargs: Additional keyword arguments to configure the route.
12+
13+
Returns:
14+
function: The decorated function.
15+
16+
"""
17+
18+
return route_decorator(path, "GET", **kwargs)
19+
20+
21+
def Post(path: str, **kwargs: Any):
22+
"""
23+
Decorator that defines a POST route for the controller.
24+
25+
Args:
26+
path (str): The URL path for the route.
27+
**kwargs: Additional keyword arguments to configure the route.
28+
29+
Returns:
30+
function: The decorated function.
31+
32+
"""
33+
34+
return route_decorator(path, "POST", **kwargs)
35+
36+
37+
def Delete(path: str, **kwargs: Any):
38+
"""
39+
Decorator that defines a DELETE route for the controller.
40+
41+
Args:
42+
path (str): The URL path for the route.
43+
**kwargs: Additional keyword arguments to configure the route.
44+
45+
Returns:
46+
function: The decorated function.
47+
48+
"""
49+
50+
return route_decorator(path, "DELETE", **kwargs)
51+
52+
53+
def Put(path: str, **kwargs: Any):
54+
"""
55+
Decorator that defines a PUT route for the controller.
56+
57+
Args:
58+
path (str): The URL path for the route.
59+
**kwargs: Additional keyword arguments to configure the route.
60+
61+
Returns:
62+
function: The decorated function.
63+
64+
"""
65+
66+
return route_decorator(path, "PUT", **kwargs)
67+
68+
69+
def Patch(path: str, **kwargs: Any):
70+
"""
71+
Decorator that defines a PATCH route for the controller.
72+
73+
Args:
74+
path (str): The URL path for the route.
75+
**kwargs: Additional keyword arguments to configure the route.
76+
77+
Returns:
78+
function: The decorated function.
79+
80+
"""
81+
82+
return route_decorator(path, "PATCH", **kwargs)

tests/test_core/test_decorators/test_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from nest.core.decorators.controller import Controller, Get, Post, Delete, Put, Patch
3+
from nest.core import Controller, Get, Post, Delete, Put, Patch
44

55

66
@Controller(tag="test", prefix="/api/v1")

0 commit comments

Comments
 (0)