You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/overview/custom_decorators.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -401,19 +401,19 @@ The `version` decorator takes a list of values as an argument, for example `@ver
401
401
This indicates that the `get_item_v2_v3` route function will handle version 2 and version 3 requests of the /create endpoint.
402
402
This allows for multiple versions of the same endpoint to be handled by different route functions, each with their own logic and implementation.
403
403
404
-
### **GUARDS**
405
-
**@Guards()** is a decorator that applies a protection class of type `GuardCanActivate` to a route function.
404
+
### **UseGuards**
405
+
**@UseGuards()** is a decorator that applies a protection class of type `GuardCanActivate` to a route function.
406
406
These protection classes have a `can_execute` function that is called to determine whether a route function should be executed.
407
407
408
408
This decorator allows you to apply certain conditions or checks before a route function is executed, such as `authentication` or `authorization` checks.
409
409
This can help to ensure that only authorized users can access certain resources.
410
410
411
411
More information on how to use this decorator can be found in the [Guard Documentation]()
412
412
413
-
A quick example on how to use `Guards` decorator:
413
+
A quick example on how to use `UseGuards` decorator:
414
414
```python
415
415
import typing as t
416
-
from ellar.common import get, Guards
416
+
from ellar.common import get, UseGuards
417
417
from ellar.core import APIKeyQuery, HTTPConnection
418
418
419
419
@@ -425,11 +425,11 @@ class MyAPIKeyQuery(APIKeyQuery):
425
425
426
426
427
427
@get("/")
428
-
@Guards(MyAPIKeyQuery(), )
428
+
@UseGuards(MyAPIKeyQuery(), )
429
429
asyncdefget_guarded_items(self):
430
430
return {'message': 'worked fine with `key`=`supersecret`'}
431
431
```
432
-
The `Guards` decorator, like the `version` decorator, takes a list of values as an argument.
432
+
The `UseGuards` decorator, like the `version` decorator, takes a list of values as an argument.
433
433
During a request, the provided guards are called in the order in which they are provided.
434
434
435
435
This allows you to apply multiple guards to a single route function and have them executed in a specific order.
Copy file name to clipboardExpand all lines: docs/overview/guards.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,54 +57,54 @@ class RoleGuard(GuardCanActivate):
57
57
58
58
## **Applying guards**
59
59
Guards can be **`controller-scoped`**, **`method-scoped`**, or **`global-scoped`**. We apply guards to controllers or route function by using `@Guards`.
60
-
The `@Guards` takes a single argument, or a comma-separated list of arguments of `GuardCanActivate` types or instances.
60
+
The `@UseGuards` takes a single argument, or a comma-separated list of arguments of `GuardCanActivate` types or instances.
We set up controller scoped guards on controller by using `@Guards` decorator. For example:
72
+
We set up controller scoped guards on controller by using `@UseGuards` decorator. For example:
73
73
```python
74
74
# project_name/cars/controllers.py
75
-
from ellar.common import Controller, Guards
75
+
from ellar.common import Controller, UseGuards
76
76
from .guards import RoleGuard
77
77
78
78
@Controller()
79
-
@Guards(RoleGuard)
79
+
@UseGuards(RoleGuard)
80
80
classCarsController:
81
81
...
82
82
83
83
```
84
84
The above example attaches the guard to every handler declared by this controller.
85
-
If we wish the guard to apply only to a single method, we apply the `@Guards()` decorator at the method level.
85
+
If we wish the guard to apply only to a single method, we apply the `@UseGuards()` decorator at the method level.
86
86
87
87
### **Method-scoped**
88
-
We can also use `@Guards()` on route-function when necessary.
88
+
We can also use `@UseGuards()` on route-function when necessary.
89
89
```python
90
90
# project_name/cars/controllers.py
91
-
from ellar.common import Controller, Guards, get
91
+
from ellar.common import Controller, UseGuards, get
92
92
from .guards import RoleGuard
93
93
94
94
@Controller()
95
-
@Guards(RoleGuard)
95
+
@UseGuards(RoleGuard)
96
96
classCarsController:
97
-
@Guards(RoleGuard())
97
+
@UseGuards(RoleGuard())
98
98
@get('/guarded-route')
99
99
defguarded_route(self):
100
100
return"Passed Guard"
101
101
102
102
```
103
-
In the example, we decorated `guarded_route` with `@Guards(RoleGuard())` with an instance of `RoleGuard`.
103
+
In the example, we decorated `guarded_route` with `@UseGuards(RoleGuard())` with an instance of `RoleGuard`.
104
104
When request execution for `/guarded-route`, `guarded_route` guard definition will be precedence over `CarsController` guard definitions.
105
105
106
106
### **Global-scope**
107
-
Global guards are used across the whole application, for every controller and every route function but individual controller or route function `@Guards` definition can override `global` scoped guards.
107
+
Global guards are used across the whole application, for every controller and every route function but individual controller or route function `@UseGuards` definition can override `global` scoped guards.
108
108
109
109
Global guards are applied at the `global_guards` parameter of the `AppFactory` creation level as shown below:
The above is also a valid configuration for `ThrottleModule` registration if you want to work with config.
53
53
54
54
If you add the `ThrottlerGuard` to your application `global_guards`, then all the incoming requests will be throttled by default.
55
-
This can also be omitted in favor of `@Guards(ThrottlerGuard)`. The global guard check can be skipped using the `@skip_throttle()` decorator mentioned later.
55
+
This can also be omitted in favor of `@UseGuards(ThrottlerGuard)`. The global guard check can be skipped using the `@skip_throttle()` decorator mentioned later.
56
56
57
-
Example with `@Guards(ThrottlerGuard)`
57
+
Example with `@UseGuards(ThrottlerGuard)`
58
58
```python
59
59
# project_name/controller.py
60
-
from ellar.common import Controller, Guards
60
+
from ellar.common import Controller, UseGuards
61
61
from ellar_throttler import throttle, ThrottlerGuard
62
62
63
63
@Controller()
64
64
classAppController:
65
65
66
-
@Guards(ThrottlerGuard)
66
+
@UseGuards(ThrottlerGuard)
67
67
@throttle(limit=5, ttl=30)
68
68
defnormal(self):
69
69
pass
@@ -95,12 +95,12 @@ a class that is skipped.
95
95
96
96
```python
97
97
# project_name/controller.py
98
-
from ellar.common import Controller, Guards
98
+
from ellar.common import Controller, UseGuards
99
99
from ellar_throttler import ThrottlerGuard, skip_throttle
100
100
101
101
@skip_throttle()
102
102
@Controller()
103
-
@Guards(ThrottlerGuard)
103
+
@UseGuards(ThrottlerGuard)
104
104
classAppController:
105
105
106
106
defdo_skip(self):
@@ -193,7 +193,7 @@ class ThrottlerBehindProxyGuard(ThrottlerGuard):
193
193
from .throttler_behind_proxy import ThrottlerBehindProxyGuard
0 commit comments