import base64
from typing import Dict, Optional, Any


class LambdaResponse(object):
    def __init__(self,
                 status: int,
                 response: Optional[Any],
                 headers: Optional[Dict[str, str]]):
        self.response = {'statusCode': status}
        is_base64 = False
        if response:
            if 'Content-Type' in headers:
                if headers['Content-Type'] != 'application/json; charset=utf-8':
                    response = base64.b64encode(response)
                    is_base64 = True
            self.response['body'] = response
        self.response['isBase64Encoded'] = is_base64
        if headers:
            self.response["headers"] = {}
            for header in headers:
                self.response["headers"][header] = headers[header]
