forked from Unicaronas/Unicaronas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrottling.py
36 lines (27 loc) · 1.02 KB
/
throttling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from rest_framework.throttling import SimpleRateThrottle
class ApplicationRateThrottle(SimpleRateThrottle):
"""
Limits the rate of API calls that may be made by a given application.
The application id will be used as a unique cache key if the call was made
through OAuth2.
"""
scope = 'application'
def get_cache_key(self, request, view):
if getattr(request, 'auth', None) is not None:
ident = request.auth.application.id
else:
return None # Only throttle applications
return self.cache_format % {
'scope': self.scope,
'ident': ident
}
class ApplicationBurstRateThrottle(ApplicationRateThrottle):
"""
Limits the burst rate of API calls that may be made by a given application.
"""
scope = 'application_burst'
class ApplicationSustainedRateThrottle(ApplicationRateThrottle):
"""
Limits the sustained rate of API calls that may be made by a given application.
"""
scope = 'application_sustained'