yoyowallet/django-idempotency-key

Decorator and class based views

Opened this issue · 1 comments

Hello, does this package supports class based views? I've tried to use it along with ExemptIdempotencyKeyMiddleware but the idempotency_key decorator doesn't work, it allows creating multiple instances, I added a custom middleware to inspect the request and the header with the key exists, could anyone provide an example about how should it be used with drf?

class DebugMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if request.method == 'POST':
            print('Headers', request.headers)
            print('META', request.META)
            idempotency_key = request.META.get("HTTP_IDEMPOTENCY_KEY")
            print(f'idempotency_key: {idempotency_key}')
        response = self.get_response(request)
        return response
class OrdersCreateViews(CreateAPIView):
    """
    View to create new Orders.
    """
    serializer_class = OrderSerializer
    authentication_classes = (TokenAuthentication,)

    @idempotency_key
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

I'm also having this issue. The problem seems to stem from the @idempotency_key decorator adding properties to the http method (ie. MyView.post) while the middleware callback parameter in IdempotencyKeyMiddleware.process_view seems to target the view class as a function, ie. <function MyView at 0x135077280> which does not have access to the required properties.