'Throttling is also restart as our django server restart

i need help of yours. I want to limit my GET API. firstly i can hit this API only for one time(forever with the same system) then i signup then i can hit this same API for 5 times(forever with the same system) then i have to pay some amount then i can hit infinite times. At this time no throttle is used.

Below is my code:- Settings.py

'DEFAULT_THROTTLE_RATES': { 'Auserlimit': '1/infinite', 'userlimit': '5/infinite', }

throttling.py:-

from rest_framework.throttling import AnonRateThrottle, UserRateThrottle import random

class limitAUserThrottle(AnonRateThrottle): scope = 'Auserlimit'

def parse_rate(self, rate):
    """
    Given the request rate string, return a two tuple of:
    <allowed number of requests>, <period of time in seconds>
    """
    if rate is None:
        return (None, None)
    num, period = rate.split('/')
    num_requests = int(num)
    duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400, 'i': 315360000000}[period[0]]
    return num_requests, duration

class limitUserThrottle(UserRateThrottle): scope = 'userlimit'

def allow_request(self, request, view):
    if not request.GET.get('call_type') == 'seu':
        self.scope = 'userlimit'
        self.rate = '5/infinite'
        return True
    return super().allow_request(request, view)

def parse_rate(self, rate):
    """
    Given the request rate string, return a two tuple of:
    <allowed number of requests>, <period of time in seconds>
    """
    if rate is None:
        return (None, None)
    num, period = rate.split('/')
    num_requests = int(num)
    duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400, 'i': 315360000000}[period[0]]
    return num_requests, duration

views.py:-

throttle_classes = [limitAUserThrottle, limitUserThrottle]

I used this line in my API class and also import this limitAUserThrottle, limitUserThrottle



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source