"""Module for working with status notifications.""" import datetime import json import boto3 from . import common_config from .util import boto3_error_handling client = boto3.client('sns', region_name=common_config.AWS_REGION) @boto3_error_handling def send_notification(topic_arn, message_payload): """Send SNS message. Args: topic_arn (str): SNS topic ARN. message_payload (dict): SNS message data. Returns: dict: Info about published message. """ return client.publish( TopicArn=topic_arn, Message=json.dumps(message_payload)) def _remove_none_entries(source): """Remove entries with None values from dictionary. Args: source (dict): Source dictionary containing entries with None values. Returns: dict: Return a new dictionary with None values filtered out. """ return {key: val for key, val in source.items() if val is not None} def send_general_status(function, status_name, filename, bucket, errors=None): """Send general status notification about asset file processing by lambda. Args: function (str): Status source lambda function name. status_name (str): Status. filename (str): Current processed asset filename. bucket (str): Current processed asset source bucket name. errors (dict): An object of errors if present. Returns: dict: Info about published status message. """ message = { 'function': function, 'status': status_name, 'filename': filename, 'bucket': bucket, 'status_time': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ'), 'errors': errors } return send_notification(common_config.SNS_GENERAL_STATUS_TOPIC_ARN, _remove_none_entries(message)) def send_encoding_status(status_name, key, bucket, errors=None, final_assets=None): """Send encoding status notification about asset file processing by lambda. Args: status_name (str): Status. key (str): S3 object key. bucket (str): S3 bucket name. errors (dict): Errors if any description. final_assets (list): List of final assets bodies. Returns: dict: Info about published status message. """ message = { 'status': status_name, 'errors': errors, 'final_assets': final_assets, 'input': { 'key': key, 'bucket': bucket } } return send_notification(common_config.SNS_ENCODING_STATUS_TOPIC_ARN, _remove_none_entries(message))