python 使用Django rest框架自定义用户级节流

eqoofvh9  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(106)

我正在尝试按层组设置API调用限制。基本上是一个免费层,一个付费层和另一个有更多限制的付费层。我正在使用Django rest框架,我一直遇到的问题是,无论用户被设置在什么级别,我仍然会以默认的速度被限制,在这种情况下是3/分钟。还想知道是否有一种方法可以为每个层级别提供自定义响应,而不是默认的节流响应。这些是我用来测试的假数字。Herre是我的代码
throttling.py

from rest_framework.throttling import UserRateThrottle

class TierBasedRateThrottle(UserRateThrottle):
    def allow_request(self, request, view):
        user = request.user
        if user.is_authenticated:
            if user.tier_level == 'tier_1':
                self.rate = '4/minute'  # Rate limit for tier 1 users
            elif user.tier_level == 'tier_2':
                self.rate = '6/minute'  # Rate limit for tier 2 users
            elif user.tier_level == 'tier_3':
                self.rate = '10/minute'  # Rate limit for tier 3 users

        return super().allow_request(request, view)

settings.py

REST_FRAMEWORK = {

'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),

# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
    'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
    'rest_framework.permissions.IsAuthenticated'
],
'DEFAULT_RENDERER_CLASSES': [
    'rest_framework.renderers.JSONRenderer',
    'rest_framework.renderers.BrowsableAPIRenderer',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
],
'DEFAULT_AUTHENTICATION_CLASSES':[
'knox.auth.TokenAuthentication',
],
'DEFAULT_THROTTLE_CLASSES': [
    'accounts.throttling.TierBasedRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
    'user': '3/minute',
    'tier1': '5/minute',  # Default limit for tier1 users
    'tier2': '10/minute',  # Limit for tier2 users
    'tier3': '15/minute',  # Limit for tier3 users
},

最后views.py

@api_view(['POST']) 
@authentication_classes([TokenAuthentication])
@permission_classes((IsAuthenticated, Tier2Permissions))
@throttle_classes([TierBasedRateThrottle])
def LiveSummary(request):
    queryset=liveSummary()
    return Response(queryset)

任何帮助都很感激

ogsagwnx

ogsagwnx1#

由于您没有在def __init__(self)中设置self.rate,因此还需要使用self.parse_rate(self.rate)更新self.num_requestsself.duration

from rest_framework.throttling import UserRateThrottle

class TierBasedRateThrottle(UserRateThrottle):
    def allow_request(self, request, view):
        user = request.user
        if user.is_authenticated:
            if user.tier_level == 'tier_1':
                self.rate = '4/minute'  # Rate limit for tier 1 users
            elif user.tier_level == 'tier_2':
                self.rate = '6/minute'  # Rate limit for tier 2 users
            elif user.tier_level == 'tier_3':
                self.rate = '10/minute'  # Rate limit for tier 3 users

            self.num_requests, self.duration = self.parse_rate(self.rate)  # Add this

        return super().allow_request(request, view)

相关问题