django石墨烯速率限制(调节)

问题描述 投票:0回答:1

我正在Django RF上构建REST Api。我需要从IP设置请求限制。对于views.py中的常规Api端点,只需添加以下设置即可轻松做到这一点

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.AnonRateThrottle',
        'rest_framework.throttling.UserRateThrottle'
    ],
    'DEFAULT_THROTTLE_RATES': {
        'anon': '100/day',
        'user': '1000/day'
    }
}

但是我也有一个graphql api的Graphene django。

如何为该视图设置速率限制?我已经尝试过django-ratelimit,但是对我来说不起作用。

throttling rate-limiting graphene-django
1个回答
0
投票

通过将GraphQL视图定制为以下内容解决了该问题:

from graphene_django.views import GraphQLView


class CustomGraphQL(GraphQLView):
    def parse_body(self, request):
        if isinstance(request, Request):
            return request.data
        return super().parse_body(request)

    @classmethod
    def as_view(cls, *args, **kwargs):
        view = super().as_view(*args, **kwargs)
        view = authentication_classes((TokenAuthentication,))(view)
        view = throttle_classes((AnonRateThrottle, AnonMinutesThrottle,
                                 AnonSecondsThrottle, UserRateThrottle))(view)
        view = api_view(['GET', 'POST'])(view)
        return view
© www.soinside.com 2019 - 2024. All rights reserved.