"""Lambda function module.""" import uuid from ddex_ingester_common.graphql.asset_queries import \ GET_ASSET_STATUS_V2_QUERY from ddex_ingester_common.helpers.asset import valid_artwork from ddex_ingester_common.lambda_exceptions import (ArtworkException, ArtworkFatalException) from ddex_ingester_common.logging import utils as logging_utils from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema import config from config import graphql_gateway from constants.asset import VALIDATION_ERROR logger = logging_utils.get_logger(config.app_logger) def handler(event, context): """Lambda entrypoint.""" context = StateMachineSchema().load(event) correlation_id = context.correlation_id or str(uuid.uuid4()) context.correlation_id = correlation_id logging_utils.update_logger_correlation_id(logger, correlation_id) logging_utils.update_logger_with_message_ids( logger, context.message_id, context.message_thread_id, context.execution_name ) graphql_gateway.set_headers( { 'Orchard-User-Id': config.OA_USER, 'Correlation-Id': correlation_id, } ) # Call ows assets for status of uploaded track asset check_artwork_valid( str(context.product.product_id), context.product.artwork.ows_assets_filename ) return StateMachineSchema().dump(context) def check_artwork_valid(product_id: str, filename: str): """Get artwork asset status from ows-assets.""" require_v1 = False if not valid_artwork(graphql_gateway, product_id, require_v1): logger.info('Artwork asset is not valid') result = graphql_gateway.execute( GET_ASSET_STATUS_V2_QUERY, {'filename': filename} )['data']['assetStatus'] status = result.get('status') if result else None if status == VALIDATION_ERROR: raise ArtworkFatalException( 'Artwork asset failed validation' ) raise ArtworkException( 'Artwork asset is not valid' )