I am trying to throttle my API routes but I am unable to do it with functions using Django Rest Framework. All the documentations are for class-based views. I even tried a workaround as suggested here: Django Rest Framework Scope Throttling on function based view
but it did not work.
Here is my code snippets:
Views:
from django.core.paginator import Paginator
from rest_framework.throttling import UserRateThrottle, AnonRateThrottle
# Create your views here.
def home(request):
throttle_scope = 'perday'
return render(request, 'home.html')
def search(request):
throttle_scope = 'search'
query = request.GET['query']
response = requests.get('https://api.stackexchange.com//2.2/search/advanced?order=desc&sort=activity&q=' + query + '&site=stackoverflow')
apiResponse = response.json()
allData = []
for eachtitle in apiResponse['items']:
allData.append(eachtitle)
paginator = Paginator(allData, 10)
pageNumber = request.GET.get('page', 1)
page = paginator.get_page(pageNumber)
return render(request, 'search.html', {"eachdata": page, "query": query})
Settings of my project:
REST_FRAMEWORK = REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'example.throttles.BurstRateThrottle',
'example.throttles.SustainedRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'perday': '2/min',
'search': '5/day'
}
}
I am not getting any errors, it just does not work. I have also added rest-framework in installed apps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…