updating the docs and removing the extra file from package

This commit is contained in:
Pravin Kamble 2023-08-22 23:47:00 +05:30
parent 8a77717188
commit 4d54da3176
3 changed files with 20 additions and 29 deletions

View File

@ -7,9 +7,6 @@ from django.core.cache import cache as default_cache
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.utils.throttling_duration_parser import (
parse_quantity_and_unit
)
class BaseThrottle: class BaseThrottle:
@ -97,6 +94,24 @@ class SimpleRateThrottle(BaseThrottle):
msg = "No default throttle rate set for '%s' scope" % self.scope msg = "No default throttle rate set for '%s' scope" % self.scope
raise ImproperlyConfigured(msg) raise ImproperlyConfigured(msg)
def parse_quantity_and_unit(self, quantity_unit_string):
"""
Parse a combined quantity and unit string and return a tuple with parsed values.
Returns:
tuple: A tuple containing the parsed values (quantity, unit).
"""
i = 0
while i < len(quantity_unit_string) and quantity_unit_string[i].isnumeric():
i += 1
if i == 0:
return (1, quantity_unit_string)
else:
quantity = int(quantity_unit_string[:i])
unit = quantity_unit_string[i:]
return (quantity, unit)
def parse_rate(self, rate): def parse_rate(self, rate):
""" """
Given the request rate string, return a two tuple of: Given the request rate string, return a two tuple of:
@ -105,7 +120,7 @@ class SimpleRateThrottle(BaseThrottle):
if rate is None: if rate is None:
return (None, None) return (None, None)
num, period = rate.split('/') num, period = rate.split('/')
quantity, unit = parse_quantity_and_unit(period) quantity, unit = self.parse_quantity_and_unit(period)
num_requests = int(num) num_requests = int(num)
duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[unit[0]] duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[unit[0]]
total_duration = duration * int(quantity) total_duration = duration * int(quantity)

View File

@ -1,21 +0,0 @@
def parse_quantity_and_unit(quantity_unit_string):
"""
Parse a combined quantity and unit string and return a dictionary containing the parsed values.
Args:
quantity_unit_string (str): A string that combines a numeric quantity and a unit, e.g., "5min", "10h".
Returns:
dict: A dictionary containing the parsed quantity and unit, with keys 'quantity' and 'unit'.
If the input string contains only a unit (e.g., "m"), quantity will be set to 1.
"""
i = 0
while i < len(quantity_unit_string) and quantity_unit_string[i].isnumeric():
i += 1
if i == 0:
return (1, quantity_unit_string)
else:
quantity = int(quantity_unit_string[:i])
unit = quantity_unit_string[i:]
return (quantity, unit)

View File

@ -15,10 +15,7 @@ from rest_framework.settings import api_settings
from rest_framework.test import APIRequestFactory, force_authenticate from rest_framework.test import APIRequestFactory, force_authenticate
from rest_framework.throttling import ( from rest_framework.throttling import (
AnonRateThrottle, BaseThrottle, ScopedRateThrottle, SimpleRateThrottle, AnonRateThrottle, BaseThrottle, ScopedRateThrottle, SimpleRateThrottle,
UserRateThrottle UserRateThrottle, parse_quantity_and_unit
)
from rest_framework.utils.throttling_duration_parser import (
parse_quantity_and_unit
) )
from rest_framework.views import APIView from rest_framework.views import APIView