"""Class that represents a Kafka message.""" import json import uuid from switchboard_consumer.constants import system class BadMessageFormat(Exception): """BadMessageFormat Exception.""" pass class KafkaMessage: """KafkaMessage.""" def __init__(self, message): """Init Message.""" try: self.message = json.loads(message) except json.JSONDecodeError: raise BadMessageFormat('Failed to decode message.') @property def sending_system(self): """The sending system of the message..""" return self.message.get('sendingSystem') @property def ids(self): """The ids of the message.""" return self.message.get('ids') @property def correlation_id(self): """Create or get the existing correlation id.""" return str(self.message.get('correlationId', str(uuid.uuid4()))) @property def orchard_local_id(self): """Get the Orchard Identifier (localId and system).""" for identifier in self.message.get('ids'): if identifier.get('system') == system.ORCHARD: return identifier return None @property def orchard_local_ids(self): """Get all the Orchard Identifier (localId and system).""" identifiers = [] for identifier in self.message.get('ids'): if identifier.get('system') == system.ORCHARD: identifiers.append(identifier) return identifiers @property def sending_system_local_id(self): """Get the originating system's Identifier (localId and system).""" for identifier in self.message.get('ids'): if identifier.get('system') == self.sending_system: return identifier return None @property def entity_type(self): """The entityType of the message. E.g., PROJECT.""" return self.message.get('entityType') @property def message_type(self): """The messageType of the message. E.g., METADATA_UPDATE.""" return self.message.get('messageType')