Skip to content

Commit 1f9245c

Browse files
committed
added options, head and routeprefix decorators
fixed dependencies syntax in pyproject.toml
1 parent 3e1174e commit 1f9245c

File tree

8 files changed

+56
-23
lines changed

8 files changed

+56
-23
lines changed

pyproject.toml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ classifiers = [
1717
"Operating System :: OS Independent",
1818
]
1919

20+
dependencies = [
21+
"annotated-types = 0.6.0",
22+
"anyio = 4.2.0",
23+
"click = 8.1.7",
24+
"colorama = 0.4.6",
25+
"fastapi = 0.109.0",
26+
"h11 = 0.14.0",
27+
"idna = 3.6",
28+
"pydantic = 2.5.3",
29+
"pydantic_core = 2.14.6",
30+
"sniffio = 1.3.0",
31+
"starlette = 0.35.1",
32+
"typing_extensions = 4.9.0",
33+
"uvicorn = 0.27.0"
34+
]
35+
2036
[project.urls]
2137
Homepage = "https://github.com/codingflowdev/web-api-controllers"
2238

@@ -28,17 +44,3 @@ exclude = ["src/app.py"]
2844
# Exclude app.py from the source distribution
2945
exclude = ["src/app.py"]
3046

31-
[dependencies]
32-
annotated-types = "0.6.0"
33-
anyio = "4.2.0"
34-
click = "8.1.7"
35-
colorama = "0.4.6"
36-
fastapi = "0.109.0"
37-
h11 = "0.14.0"
38-
idna = "3.6"
39-
pydantic = "2.5.3"
40-
pydantic_core = "2.14.6"
41-
sniffio = "1.3.0"
42-
starlette = "0.35.1"
43-
typing_extensions = "4.9.0"
44-
uvicorn = "0.27.0"

src/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from fastapi import FastAPI
2-
from src.webapicontrollers import APIController, Get, Post
2+
from src.webapicontrollers import APIController, Get, Post, RoutePrefix
33

44

5+
#@RoutePrefix('/test')
56
class TestController(APIController):
67

78
def __init__(self, app: FastAPI) -> None:

src/webapicontrollers/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
from .routing.delete import Delete
66
from .routing.patch import Patch
77
from .routing.route import Route
8+
from .routing.head import Head
9+
from .routing.options import Options
10+
from .routing.route_prefix import RoutePrefix

src/webapicontrollers/controllers/api_controller.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,40 @@ class APIController:
1010
routes = []
1111

1212
def __init__(self,
13-
app: FastAPI = None,
13+
app: FastAPI,
1414
cors_origins=None,
15+
generate_options_endpoints=True,
16+
generate_head_endpoints=True
1517
) -> None:
16-
if app is None:
17-
app = FastAPI()
18-
self.__app = app
18+
self.__app = app
19+
self.__generate_options_endpoints = generate_options_endpoints
20+
self.__generate_head_endpoints = generate_head_endpoints
1921
if cors_origins is not None:
2022
self.__add_cors(cors_origins)
23+
2124
self.__register_routes()
2225

2326
def __register_routes(self) -> None:
2427
container = DIContainer(Registry())
2528
registry = container.get(Registry)
26-
self.__routes = registry.get_routes()
29+
self.__routes = registry.get_routes()
30+
2731
for func, path, method in self.__routes:
28-
bound_method = getattr(self, func.__name__)
32+
if hasattr(self,'_route_prefix'):
33+
path = self._route_prefix + path
34+
bound_method = getattr(self, func.__name__)
2935
self.__add_route(bound_method, method, path)
3036

31-
self.__add_options_endpoints()
37+
if self.__generate_options_endpoints:
38+
self.__add_options_endpoints()
3239

3340
def __add_route(self, bound_method, method, path):
3441
self.__app.add_api_route(
3542
path=path,
3643
endpoint=bound_method,
3744
methods=[method.value]
3845
)
39-
if method.value == 'GET':
46+
if method.value == 'GET' and self.__generate_head_endpoints:
4047
self.__add_head(path)
4148

4249
def __add_head(self, path):

src/webapicontrollers/routing/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
from .put import Put
66
from .delete import Delete
77
from .patch import Patch
8+
from .head import Head
9+
from .options import Options
10+
from .route_prefix import RoutePrefix
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .route import Route
2+
from ..enums import HTTPMethods
3+
4+
class Head(Route):
5+
def __init__(self, path: str):
6+
super().__init__(path, HTTPMethods.HEAD)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .route import Route
2+
from ..enums import HTTPMethods
3+
4+
class Options(Route):
5+
def __init__(self, path: str):
6+
super().__init__(path, HTTPMethods.OPTIONS)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def RoutePrefix(prefix: str):
2+
def decorator(cls):
3+
cls._route_prefix = prefix
4+
return cls
5+
return decorator

0 commit comments

Comments
 (0)