# Path: scar/utils.py
# class StrUtils:
# 
#     """Common methods for string management."""
# 
#     @staticmethod
#     def decode_base64(value: Union[bytes, str]) -> bytes:
#         """Decode a Base64 encoded bytes-like object or
#         ASCII string and return the decoded bytes"""
#         return base64.b64decode(value)
# 
#     @staticmethod
#     def encode_base64(value: bytes) -> bytes:
#         """Encode a bytes-like object using Base64
#         and return the encoded bytes."""
#         return base64.b64encode(value)
# 
#     @staticmethod
#     def base64_to_utf8_string(value: Union[bytes, str]) -> str:
#         """Decode a Base64 encoded bytes-like object or ASCII
#         string and return the decoded value as a string."""
#         return StrUtils.decode_base64(value).decode('utf-8')
# 
#     @staticmethod
#     def utf8_to_base64_string(value: str) -> str:
#         """Encode a 'utf-8' string using Base64 and return
#         the encoded value as a string."""
#         return StrUtils.encode_base64(bytes(value, 'utf-8')).decode('utf-8')
# 
#     @staticmethod
#     def bytes_to_base64str(value, encoding='utf-8') -> str:
#         """Encode a 'utf-8' string using Base64 and return
#         the encoded value as a string."""
#         return StrUtils.encode_base64(value).decode(encoding)
# 
#     @staticmethod
#     def dict_to_base64_string(value: Dict) -> str:
#         """Encodes a dictionary to base64 and returns a string."""
#         return StrUtils.utf8_to_base64_string(json.dumps(value))
# 
#     @staticmethod
#     def find_expression(string_to_search: str, rgx_pattern: str) -> Optional[str]:
#         """Returns the first group that matches the rgx_pattern in the string_to_search."""
#         if string_to_search:
#             pattern = re.compile(rgx_pattern)
#             match = pattern.search(string_to_search)
#             if match:
#                 return match.group()
#         return None
# 
#     @staticmethod
#     def get_random_uuid4_str() -> str:
#         """Returns a random generated uuid4 string."""
#         return str(uuid.uuid4())
# 
#     @staticmethod
#     def compare_versions(ver1: str, ver2: str) -> int:
#         """Returns value < 0 to indicate that ver1 is less than ver2.
#         Returns value > 0 to indicate that ver1 is greater than ver2.
#         Returns value == 0 to indicate that ver1 is equal to ver2."""
#         res = 0
#         if version.parse(ver1) < version.parse(ver2):
#             res = -1
#         elif version.parse(ver1) > version.parse(ver2):
#             res = 1
#         return res


import json
import scar.logger as logger
from typing import Dict
from enum import Enum
from tabulate import tabulate
from scar.utils import StrUtils
from requests import Response
    if 'Payload' in value and value['Payload']:
        payload = value['Payload'].read()
        if len(payload) > 0:
            value['Payload'] = json.loads(payload.decode("utf-8"))


def _parse_asynchronous_invocation_response(response, output_type, function_name):
    if response:
        aws_output = 'LambdaOutput'
        text_message = f"Request Id: {response['ResponseMetadata']['RequestId']}\n"
        text_message += f"Function '{function_name}' launched correctly"
        json_message = {aws_output : {'StatusCode' : response['StatusCode'],
                                       'RequestId' : response['ResponseMetadata']['RequestId']}}
        _print_generic_response(response, output_type, aws_output, text_message, json_output=json_message)


def _parse_requestresponse_invocation_response(**kwargs):
    response = kwargs['Response']

    if 'errorMessage' in response['Payload']:
        json_message = response['Payload']
        text_message = response['Payload']['errorMessage']
    else:
        request_id = response['ResponseMetadata']['RequestId']
        log_group_name = response['Payload']['headers']['amz-log-group-name']
        log_stream_name = response['Payload']['headers']['amz-log-stream-name']
        if "exception" in response['Payload']['body']:
            body = ("ERROR launching udocker container: \n "
                    f"{json.loads(response['Payload']['body'])['exception']}")
        elif response['Payload']['isBase64Encoded']:

# Path: scar/utils.py
# class StrUtils:
# 
#     """Common methods for string management."""
# 
#     @staticmethod
#     def decode_base64(value: Union[bytes, str]) -> bytes:
#         """Decode a Base64 encoded bytes-like object or
#         ASCII string and return the decoded bytes"""
#         return base64.b64decode(value)
# 
#     @staticmethod
#     def encode_base64(value: bytes) -> bytes:
#         """Encode a bytes-like object using Base64
#         and return the encoded bytes."""
#         return base64.b64encode(value)
# 
#     @staticmethod
#     def base64_to_utf8_string(value: Union[bytes, str]) -> str:
#         """Decode a Base64 encoded bytes-like object or ASCII
#         string and return the decoded value as a string."""
#         return StrUtils.decode_base64(value).decode('utf-8')
# 
#     @staticmethod
#     def utf8_to_base64_string(value: str) -> str:
#         """Encode a 'utf-8' string using Base64 and return
#         the encoded value as a string."""
#         return StrUtils.encode_base64(bytes(value, 'utf-8')).decode('utf-8')
# 
#     @staticmethod
#     def bytes_to_base64str(value, encoding='utf-8') -> str:
#         """Encode a 'utf-8' string using Base64 and return
#         the encoded value as a string."""
#         return StrUtils.encode_base64(value).decode(encoding)
# 
#     @staticmethod
#     def dict_to_base64_string(value: Dict) -> str:
#         """Encodes a dictionary to base64 and returns a string."""
#         return StrUtils.utf8_to_base64_string(json.dumps(value))
# 
#     @staticmethod
#     def find_expression(string_to_search: str, rgx_pattern: str) -> Optional[str]:
#         """Returns the first group that matches the rgx_pattern in the string_to_search."""
#         if string_to_search:
#             pattern = re.compile(rgx_pattern)
#             match = pattern.search(string_to_search)
#             if match:
#                 return match.group()
#         return None
# 
#     @staticmethod
#     def get_random_uuid4_str() -> str:
#         """Returns a random generated uuid4 string."""
#         return str(uuid.uuid4())
# 
#     @staticmethod
#     def compare_versions(ver1: str, ver2: str) -> int:
#         """Returns value < 0 to indicate that ver1 is less than ver2.
#         Returns value > 0 to indicate that ver1 is greater than ver2.
#         Returns value == 0 to indicate that ver1 is equal to ver2."""
#         res = 0
#         if version.parse(ver1) < version.parse(ver2):
#             res = -1
#         elif version.parse(ver1) > version.parse(ver2):
#             res = 1
#         return res


# Path: scar/providers/aws/response.py
import json
import scar.logger as logger
from typing import Dict
from enum import Enum
from tabulate import tabulate
from scar.utils import StrUtils
from requests import Response

    if 'Payload' in value and value['Payload']:
        payload = value['Payload'].read()
        if len(payload) > 0:
            value['Payload'] = json.loads(payload.decode("utf-8"))


def _parse_asynchronous_invocation_response(response, output_type, function_name):
    if response:
        aws_output = 'LambdaOutput'
        text_message = f"Request Id: {response['ResponseMetadata']['RequestId']}\n"
        text_message += f"Function '{function_name}' launched correctly"
        json_message = {aws_output : {'StatusCode' : response['StatusCode'],
                                       'RequestId' : response['ResponseMetadata']['RequestId']}}
        _print_generic_response(response, output_type, aws_output, text_message, json_output=json_message)


def _parse_requestresponse_invocation_response(**kwargs):
    response = kwargs['Response']

    if 'errorMessage' in response['Payload']:
        json_message = response['Payload']
        text_message = response['Payload']['errorMessage']
    else:
        request_id = response['ResponseMetadata']['RequestId']
        log_group_name = response['Payload']['headers']['amz-log-group-name']
        log_stream_name = response['Payload']['headers']['amz-log-stream-name']
        if "exception" in response['Payload']['body']:
            body = ("ERROR launching udocker container: \n "
                    f"{json.loads(response['Payload']['body'])['exception']}")
        elif response['Payload']['isBase64Encoded']:

            body = StrUtils.base64_to_utf8_string(response['Payload']['body'])