"""Utilities for reading message data.""" from datetime import datetime import uuid from switchboard_consumer.constants import message_type, system from switchboard_consumer.constants.message_fields import ISRC from switchboard_consumer.formatters.format_datetime import format_datetime def get_system_local_id(message, system=system.ORCHARD): """Get the localId and system pair.""" for x in message['ids']: if x['system'] == system: return x return None def create_track_identifiers(identifiers, isrc): """Create a track identifier.""" track_identifiers = [] for identifier in identifiers: track_identifier = { 'localId': str(identifier['localId']), 'system': identifier['system'], 'businessKeyType': ISRC, 'businessKey': isrc } track_identifiers.append(track_identifier) return track_identifiers def create_processing_result(local_ids, message, entity_type=None, errors=None, exception=None): """Create a processing result message.""" result = { 'entityType': entity_type or message.entity_type, 'messageType': message_type.PROCESSING_RESULT, 'messageId': str(uuid.uuid4()), 'sendingSystem': system.ORCHARD, 'correlationId': message.correlation_id, 'ids': local_ids, 'messageCreatedDateTime': format_datetime( datetime.utcnow(), ), 'errors': [] } if errors: result['errors'] += errors if exception: result['errors'].append(exception) return result