diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 5f0272c10..3af69c4ad 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -7,9 +7,6 @@ from django.core.cache import cache as default_cache from django.core.exceptions import ImproperlyConfigured from rest_framework.settings import api_settings -from rest_framework.utils.throttling_duration_parser import ( - parse_quantity_and_unit -) class BaseThrottle: @@ -97,6 +94,24 @@ class SimpleRateThrottle(BaseThrottle): msg = "No default throttle rate set for '%s' scope" % self.scope 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): """ Given the request rate string, return a two tuple of: @@ -105,7 +120,7 @@ class SimpleRateThrottle(BaseThrottle): if rate is None: return (None, None) num, period = rate.split('/') - quantity, unit = parse_quantity_and_unit(period) + quantity, unit = self.parse_quantity_and_unit(period) num_requests = int(num) duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[unit[0]] total_duration = duration * int(quantity) diff --git a/rest_framework/utils/throttling_duration_parser.py b/rest_framework/utils/throttling_duration_parser.py deleted file mode 100644 index b8d2b30be..000000000 --- a/rest_framework/utils/throttling_duration_parser.py +++ /dev/null @@ -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) diff --git a/tests/test_throttling.py b/tests/test_throttling.py index dbbaf9453..d0aa0584c 100644 --- a/tests/test_throttling.py +++ b/tests/test_throttling.py @@ -15,10 +15,7 @@ from rest_framework.settings import api_settings from rest_framework.test import APIRequestFactory, force_authenticate from rest_framework.throttling import ( AnonRateThrottle, BaseThrottle, ScopedRateThrottle, SimpleRateThrottle, - UserRateThrottle -) -from rest_framework.utils.throttling_duration_parser import ( - parse_quantity_and_unit + UserRateThrottle, parse_quantity_and_unit ) from rest_framework.views import APIView