"""Product Review Event Message.""" from kafka_utils.consumer.message.base import BaseEventMessage from kafka_utils.exceptions import EventProducerMessageException class ProductEventMessage(BaseEventMessage): """ProductEventMessage. Example deserialized message body: { "operation": { "type": "create", "context": "reject", "timestamp": "2022-05-24T20:54:56.353Z" }, "payload": { "product_id": 3880989, "review_queue_id": 4502, "review_note": "Something smells.", "user_id": "c54e8dae-aab5-487a-9441-14f16fcc6e49" } } """ def __init__( self, message, topic, value_deserializer, ): """ Initialize. Args: message (dict): the string for AVRO serialized message topic (str): The topic for the message. value_deserializer (BaseDeserializer subclass): Deserializer """ super().__init__(message, topic) self.value_deserializer = value_deserializer self.schema_id = self.value_deserializer.get_schema_id(message) self.message = self.value_deserializer.deserialize(self.message, self.topic, 'value') if 'operation' not in self.message or not self.message.get('operation'): raise EventProducerMessageException( 'Missing required data: operation') if 'payload' not in self.message or not self.message.get('payload'): raise EventProducerMessageException( 'Missing required data: payload') if 'type' not in self.message['operation'] or not self.message[ 'operation'].get('type'): raise EventProducerMessageException( 'Missing required data: operation.type') def _access_inner_field(self, wrapper, field): """Return message.wrapper.field value if it exists.""" if field in self.message.get(wrapper): return self.message.get(wrapper).get(field) @property def product_id(self): """Event Product ID.""" return self._access_inner_field('payload', 'product_id') @property def review_queue_id(self): """Event Review Queue ID.""" return self._access_inner_field('payload', 'review_queue_id') @property def submission_type(self): """Event Review Submission Type.""" return self._access_inner_field('payload', 'submission_type') @property def operation_type(self): """Event operation type.""" return self._access_inner_field('operation', 'type') @property def operation_context(self): """Event operation context.""" return self._access_inner_field('operation', 'context')