Skip to content

Commit ea2ecbc

Browse files
committed
changed interceptors to UseInterceptors
1 parent fbd2fde commit ea2ecbc

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

ellar/common/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
Controller,
99
Module,
1010
UseGuards,
11+
UseInterceptors,
1112
Version,
1213
exception_handler,
1314
extra_args,
1415
file,
15-
interceptors,
1616
middleware,
1717
openapi_info,
1818
render,
@@ -198,7 +198,7 @@
198198
"IInterceptorsConsumer",
199199
"IGuardsConsumer",
200200
"EllarInterceptor",
201-
"interceptors",
201+
"UseInterceptors",
202202
]
203203

204204

ellar/common/decorators/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .file import file
88
from .guards import UseGuards
99
from .html import render, template_filter, template_global
10-
from .interceptor import interceptors
10+
from .interceptor import UseInterceptors
1111
from .middleware import middleware
1212
from .modules import Module
1313
from .openapi import openapi_info
@@ -29,7 +29,7 @@
2929
"openapi_info",
3030
"Module",
3131
"extra_args",
32-
"interceptors",
32+
"UseInterceptors",
3333
]
3434

3535

ellar/common/decorators/interceptor.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@
88
from ellar.common import EllarInterceptor
99

1010

11-
def interceptors(
11+
def UseInterceptors(
1212
*args: t.Union[t.Type["EllarInterceptor"], "EllarInterceptor"]
1313
) -> t.Callable:
1414
"""
15-
=========FUNCTION DECORATOR ==============
15+
=========CONTROLLER AND FUNCTION DECORATOR ==============
1616
17-
Programmatically adds extra route function parameter.
18-
see https://github.com/eadwinCode/ellar/blob/main/tests/test_routing/test_extra_args.py for usages
19-
:param args: Collection EllarInterceptor
17+
Decorator that binds interceptors to the scope of the controller or method,
18+
depending on its context.
19+
20+
When `@UseInterceptors` is used at the controller level, the interceptor will
21+
be applied to every handler (method) in the controller.
22+
23+
When `@UseInterceptors` is used at the individual handler level, the interceptor
24+
will apply only to that specific method.
25+
26+
Note
27+
Interceptors can also be set up globally for all controllers and routes
28+
using `app.use_global_interceptors()`.
29+
30+
:param args: A single EllarInterceptor instance or class, or a list of EllarInterceptor instances or classes.
2031
:return:
2132
"""
2233
return set_meta(ROUTE_INTERCEPTORS, list(args))

ellar/common/decorators/middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def middleware() -> t.Callable:
1616
"""
1717
========= MODULE DECORATOR ==============
1818
19-
Defines middle functions at module level
19+
Defines middleware functions at @Module decorated class level
2020
:return: Function
2121
"""
2222

tests/test_interceptors/test_interceptor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
ControllerBase,
66
EllarInterceptor,
77
IExecutionContext,
8+
UseInterceptors,
89
get,
9-
interceptors,
1010
ws_route,
1111
)
1212
from ellar.di import injectable
@@ -41,19 +41,19 @@ async def intercept(
4141
return {"message": str(cex)}
4242

4343

44+
@UseInterceptors(Interceptor1)
4445
@Controller("")
4546
class InterceptorControllerTest(ControllerBase):
46-
@interceptors(Interceptor1)
47+
@UseInterceptors(Interceptor1)
4748
@get("/interceptor-1")
4849
async def interceptor_1(self):
4950
return {"message": "intercepted okay"}
5051

51-
@interceptors(InterceptCustomException())
52+
@UseInterceptors(InterceptCustomException())
5253
@get("/interceptor-exception")
5354
async def interceptor_exception(self):
5455
raise CustomException("Wrong data!!")
5556

56-
@interceptors(Interceptor1)
5757
@ws_route("/interceptor-ws")
5858
async def interceptor_1_ws(self):
5959
ws = self.context.switch_to_websocket().get_client()

0 commit comments

Comments
 (0)