Пример #1
0
def parse_http_response(response: Response, resources_info: Dict, scar_info: Dict) -> None:
    '''Process the response generated by an API Gateway invocation.'''
    output_type = scar_info.get('cli_output')
    function_name = resources_info.get('lambda').get('name')
    asynch = resources_info.get('lambda').get('asynchronous')
    text_message = ""
    if response.ok:
        if output_type == OutputType.BINARY.value:
            output_file = scar_info.get('output_file', '')
            with open(output_file, "wb") as out:
                out.write(StrUtils.decode_base64(response.text))
            text_message = f"Output saved in file '{output_file}'"
        else:
            text_message = f"Request Id: {response.headers['amz-lambda-request-id']}"
            if asynch:
                text_message += f"\nFunction '{function_name}' launched correctly"
            else:
                text_message += f"\nLog Group Name: {response.headers['amz-log-group-name']}\n"
                text_message += f"Log Stream Name: {response.headers['amz-log-stream-name']}\n"
                text_message += StrUtils.base64_to_utf8_string(response.text)
    else:
        if asynch and response.status_code == 502:
            text_message = f"Function '{function_name}' launched successfully."
        else:
            error = json.loads(response.text)
            if 'message' in error:
                text_message = f"Error ({response.reason}): {error['message']}"
            else:
                text_message = f"Error ({response.reason}): {error['exception']}"
    logger.info(text_message)
Пример #2
0
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'])
        else:
            body = response['Payload']['body']

        text_message = (f"Request Id: {request_id}\n"
                        f"Log Group Name: {log_group_name}\n"
                        f"Log Stream Name: {log_stream_name}\n")
        text_message += body

        json_message = {'LambdaOutput' : {'StatusCode' : response['StatusCode'],
                                        'Payload' : body,
                                        'LogGroupName' : log_group_name,
                                        'LogStreamName' : log_stream_name,
                                        'RequestId' : request_id}}
    output_file = None
    if 'OutputFile' in kwargs and kwargs['OutputFile']:
        output_file = kwargs['OutputFile']
    _print_generic_response(response, kwargs['OutputType'], 'LambdaOutput', text_message,
                            json_output=json_message, output_file=output_file)
Пример #3
0
def _parse_base64_response_values(value):
    value['LogResult'] = StrUtils.base64_to_utf8_string(value['LogResult'])
    value['ResponseMetadata']['HTTPHeaders']['x-amz-log-result'] = \
        StrUtils.base64_to_utf8_string(value['ResponseMetadata']['HTTPHeaders']['x-amz-log-result'])