from authlib.oauth2 import ResourceProtector as _ResourceProtector
from aws_lambda_powertools.event_handler import APIGatewayHttpResolver
from aws_lambda_powertools.event_handler.middlewares import NextMiddleware

from .requests import APIGatewayJsonRequest


class ResourceProtector(_ResourceProtector):
    def __call__(self, scopes=None, optional=False, **kwargs):
        claims = kwargs
        # backward compatibility
        claims['scopes'] = scopes

        def wrapper(app: APIGatewayHttpResolver, next_middleware: NextMiddleware):
            request = APIGatewayJsonRequest(app.current_event)
            print(request)
            return next_middleware(app)

        return wrapper
