import json

from django.http import HttpResponse
from core.utils import is_private_api_call_allowed


def user_has_license_middleware(get_response):

    def middleware(request):
        if request.path == '/api/graphql/':
            if not is_private_api_call_allowed(request.user, request.body):
                return HttpResponse(json.dumps({'errors': ['no active license']}), status=402)

        return get_response(request)

    return middleware
