from .models import APILog

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

    def __call__(self, request):
        if not request.path.startswith('/admin'):

            user = request.user if request.user.is_authenticated else None


            request_url = request.path

            request_params = request.GET if request.GET else None


            request_method = request.method


            request_body = request.body.decode('utf-8')


            APILog.objects.create(user=user, request_url=request_url, request_params=request_params, request_method=request_method, request_body=request_body)

        response = self.get_response(request)
        return response
