Skip to content

Commit 3b20123

Browse files
committed
Updated docs
1 parent 37032c9 commit 3b20123

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

docs/tutorial/throttling.md

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Throttling can be seen as a permission that determines if a request should be au
44
It indicates a temporary state used to control the rate of requests that clients can make to an API.
55

66
```python
7-
from ninja_extra import NinjaExtraAPI, throttle
7+
from ninja_extra import NinjaExtraAPI
8+
from ninja_extra.throttling import UserRateThrottle, AnonRateThrottle
89
api = NinjaExtraAPI()
910

10-
@api.get('/users')
11-
@throttle # this will apply default throttle classes [UserRateThrottle, AnonRateThrottle]
11+
@api.get('/users', throttle=[AnonRateThrottle(), UserRateThrottle()])
1212
def my_throttled_endpoint(request):
1313
return 'foo'
1414
```
@@ -23,7 +23,7 @@ constraints, which could be burst throttling rate or sustained throttling rates,
2323
for example, you might want to limit a user to a maximum of 60 requests per minute, and 1000 requests per day.
2424

2525
```python
26-
from ninja_extra import NinjaExtraAPI, throttle
26+
from ninja_extra import NinjaExtraAPI
2727
from ninja_extra.throttling import UserRateThrottle
2828
api = NinjaExtraAPI()
2929

@@ -36,8 +36,7 @@ class User1000PerDayRateThrottle(UserRateThrottle):
3636
rate = "1000/day"
3737
scope = "days"
3838

39-
@api.get('/users')
40-
@throttle(User60MinRateThrottle, User1000PerDayRateThrottle)
39+
@api.get('/users', throttle=[User60MinRateThrottle(), User1000PerDayRateThrottle()])
4140
def my_throttled_endpoint(request):
4241
return 'foo'
4342

@@ -61,13 +60,12 @@ NINJA_EXTRA = {
6160
The rate descriptions used in `THROTTLE_RATES` may include `second`, `minute`, `hour` or `day` as the throttle period.
6261

6362
```python
64-
from ninja_extra import NinjaExtraAPI, throttle
63+
from ninja_extra import NinjaExtraAPI
6564
from ninja_extra.throttling import UserRateThrottle
6665

6766
api = NinjaExtraAPI()
6867

69-
@api.get('/users')
70-
@throttle(UserRateThrottle)
68+
@api.get('/users', throttle=UserRateThrottle())
7169
def my_throttled_endpoint(request):
7270
return 'foo'
7371
```
@@ -162,17 +160,15 @@ NINJA_EXTRA = {
162160

163161
```python
164162
# api.py
165-
from ninja_extra import NinjaExtraAPI, throttle
163+
from ninja_extra import NinjaExtraAPI
166164
from ninja_extra.throttling import DynamicRateThrottle
167165
api = NinjaExtraAPI()
168166

169-
@api.get('/users')
170-
@throttle(DynamicRateThrottle, scope='burst')
167+
@api.get('/users', throttle=DynamicRateThrottle(scope='burst'))
171168
def get_users(request):
172169
return 'foo'
173170

174-
@api.get('/users/<int:id>')
175-
@throttle(DynamicRateThrottle, scope='sustained')
171+
@api.get('/users/<int:id>', throttle=DynamicRateThrottle(scope='sustained'))
176172
def get_user_by_id(request, id: int):
177173
return 'foo'
178174
```
@@ -187,21 +183,15 @@ Here, we dynamically applied `sustained` rates and `burst` rates to `get_users`
187183
```python
188184
# api.py
189185
from ninja_extra import (
190-
NinjaExtraAPI, throttle, api_controller, ControllerBase,
186+
NinjaExtraAPI, api_controller, ControllerBase,
191187
http_get
192188
)
193189
from ninja_extra.throttling import DynamicRateThrottle
194190
api = NinjaExtraAPI()
195191

196-
@api_controller("/throttled-controller")
192+
@api_controller("/throttled-controller", throttle=[DynamicRateThrottle(scope="sustained")])
197193
class ThrottlingControllerSample(ControllerBase):
198-
throttling_classes = [
199-
DynamicRateThrottle,
200-
]
201-
throttling_init_kwargs = dict(scope="sustained")
202-
203-
@http_get("/endpoint_1")
204-
@throttle(DynamicRateThrottle, scope='burst')
194+
@http_get("/endpoint_1", throttle=DynamicRateThrottle(scope="burst"))
205195
def endpoint_1(self, request):
206196
# this will override the generally throttling applied at the controller
207197
return "foo"
@@ -216,4 +206,4 @@ class ThrottlingControllerSample(ControllerBase):
216206

217207

218208
api.register_controllers(ThrottlingControllerSample)
219-
```
209+
```

ninja_extra/controllers/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ def api_controller(
531531
def api_controller(
532532
prefix_or_class: str = "",
533533
auth: Any = NOT_SET,
534+
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
534535
tags: Union[Optional[List[str]], str] = None,
535536
permissions: Optional["PermissionType"] = None,
536537
auto_import: bool = True,
@@ -543,6 +544,7 @@ def api_controller(
543544
def api_controller(
544545
prefix_or_class: Union[str, Union[ControllerClassType, Type]] = "",
545546
auth: Any = NOT_SET,
547+
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
546548
tags: Union[Optional[List[str]], str] = None,
547549
permissions: Optional["PermissionType"] = None,
548550
auto_import: bool = True,
@@ -554,6 +556,7 @@ def api_controller(
554556
tags=tags,
555557
permissions=permissions,
556558
auto_import=auto_import,
559+
throttle=throttle,
557560
)(prefix_or_class)
558561

559562
def _decorator(cls: ControllerClassType) -> ControllerClassType:
@@ -563,6 +566,7 @@ def _decorator(cls: ControllerClassType) -> ControllerClassType:
563566
tags=tags,
564567
permissions=permissions,
565568
auto_import=auto_import,
569+
throttle=throttle,
566570
)(cls)
567571

568572
return _decorator

0 commit comments

Comments
 (0)