import logging
from typing import Dict

from httpclient import HttpRequest
from httpmessagehandler import HttpMessageHandler
from ._KongRequest import KongRequest
from ._LambdaResponse import LambdaResponse

logger = logging.getLogger()


class KongLambdaMessageHandler(object):

    def __init__(
        self,
        http_message_handler: HttpMessageHandler,
    ) -> None:

        self.http_message_handler = http_message_handler

    def handle_messages(self, event: Dict) -> LambdaResponse:

        request = KongRequest(event)

        http_request = HttpRequest(
            url=request.path,
            method=request.method,
            query=request.args,
            headers=request.headers,
            body=request.data,
        )
        http_response = self.http_message_handler.handle_request(request=http_request)
        response = LambdaResponse(
            status=http_response.status_code,
            response=http_response.body,
            headers=http_response.headers,
        )
        return response.response
