"""Model for a message from a kafka replication topic.""" import base64 class MSKMessage: """MSK event record. Message structure: { topic: {topic name} partition: {partition name} offset: {message offset} timestamp: {unix timestamp} timestampType: {timestamp type} key: {base64 encoded message key} value: {base64 encoded message value} } """ def __init__(self, record): """Initialize.""" self.topic = record.get('topic') self.partition = record.get('partition') self.offset = record.get('offset') self.timestamp = record.get('timestamp') self.timestamp_type = record.get('timestampType') self._key = record.get('key') self._value = record.get('value') self._decoded_key = None self._decoded_value = None @property def value(self): """Lazily parse value property.""" if self._decoded_value is not None: return self._decoded_value if self._value is None: return None self._decoded_value = self._decode(self._value) return self._decoded_value @property def key(self): """Lazily parse key property.""" if self._decoded_key is not None: return self._decoded_key if self._key is None: return None self._decoded_key = self._decode(self._key) return self._decoded_key @staticmethod def _decode(value): """Parse packed event data.""" return base64.b64decode(value)