import json

from botocore.client import BaseClient
from lambda_invoker import LambdaInvoker


class Boto3LambdaInvoker(LambdaInvoker):
    def __init__(
        self,
        lambda_client: BaseClient,
        function_name: str,
    ):
        self.lambda_client = lambda_client
        self.function_name = function_name

    def invoke(self, event=None) -> bytes:
        response = self.lambda_client.invoke(
            FunctionName=self.function_name,
            InvocationType="RequestResponse",
            LogType="None",
            Payload=json.dumps(event).encode(),
        )
        if response["StatusCode"] != 200:
            raise RuntimeError(f"Error invoking function: {response['StatusCode']}")

        return response["Payload"].read()
