Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator and class based views #52

Open
adrian-martinez-dev opened this issue May 15, 2023 · 1 comment
Open

Decorator and class based views #52

adrian-martinez-dev opened this issue May 15, 2023 · 1 comment

Comments

@adrian-martinez-dev
Copy link

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)
@dashdanw
Copy link

dashdanw commented Jan 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants