import json
from typing import Dict


class KongRequest(object):
    def __init__(self, event: Dict):
        self.path = str(event['request_uri']).split('?')[0]
        self.method = event['request_method']
        self.args = event['request_uri_args']
        self.headers = event['request_headers']
        self.data = self._parse_request_body(event)

    @staticmethod
    def _parse_request_body(event: Dict) -> bytes:
        request_body = event.get('request_body')
        if request_body is not None:
            if isinstance(request_body, dict):
                request_body = json.dumps(request_body, default=str)
            if not isinstance(request_body, str):
                request_body = str(request_body)
            return request_body.encode("utf-8")
        return b""
