import logging
from httpclient import HttpRequest
from httpmessagehandler import HttpMessageHandler

from . import KongRequest, LambdaResponse

logger = logging.getLogger()


class KongLambdaMessageHandler(object):
    def __init__(self, http_message_handler):
        # type: (HttpMessageHandler) -> None
        self.http_message_handler = http_message_handler

    def handle_messages(self, event):
        # type: (dict) -> LambdaResponse
        request = KongRequest(event)

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