@@ -4,11 +4,11 @@ Throttling can be seen as a permission that determines if a request should be au
44It 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
89api = NinjaExtraAPI()
910
10- @api.get (' /users' )
11- @throttle # this will apply default throttle classes [UserRateThrottle, AnonRateThrottle]
11+ @api.get (' /users' , throttle = [AnonRateThrottle(), UserRateThrottle()])
1212def my_throttled_endpoint (request ):
1313 return ' foo'
1414```
@@ -23,7 +23,7 @@ constraints, which could be burst throttling rate or sustained throttling rates,
2323for 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
2727from ninja_extra.throttling import UserRateThrottle
2828api = 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()])
4140def my_throttled_endpoint (request ):
4241 return ' foo'
4342
@@ -61,13 +60,12 @@ NINJA_EXTRA = {
6160The 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
6564from ninja_extra.throttling import UserRateThrottle
6665
6766api = NinjaExtraAPI()
6867
69- @api.get (' /users' )
70- @throttle (UserRateThrottle)
68+ @api.get (' /users' , throttle = UserRateThrottle())
7169def 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
166164from ninja_extra.throttling import DynamicRateThrottle
167165api = NinjaExtraAPI()
168166
169- @api.get (' /users' )
170- @throttle (DynamicRateThrottle, scope = ' burst' )
167+ @api.get (' /users' , throttle = DynamicRateThrottle(scope = ' burst' ))
171168def 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' ))
176172def 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
189185from ninja_extra import (
190- NinjaExtraAPI, throttle, api_controller, ControllerBase,
186+ NinjaExtraAPI, api_controller, ControllerBase,
191187 http_get
192188)
193189from ninja_extra.throttling import DynamicRateThrottle
194190api = NinjaExtraAPI()
195191
196- @api_controller (" /throttled-controller" )
192+ @api_controller (" /throttled-controller" , throttle = [DynamicRateThrottle( scope = " sustained " )] )
197193class 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
218208api.register_controllers(ThrottlingControllerSample)
219- ```
209+ ```
0 commit comments