|
| 1 | +import datetime |
| 2 | +import json |
| 3 | +import typing |
| 4 | + |
| 5 | +from azure.functions import _abc as azf_abc |
| 6 | + |
| 7 | +from . import meta |
| 8 | +from .. import protos |
| 9 | + |
| 10 | + |
| 11 | +class EventGridEvent(azf_abc.EventGridEvent): |
| 12 | + """An EventGrid event message.""" |
| 13 | + |
| 14 | + def __init__(self, *, |
| 15 | + id: str, |
| 16 | + data: typing.Dict[str, object], |
| 17 | + topic: str, |
| 18 | + subject: str, |
| 19 | + event_type: str, |
| 20 | + event_time: datetime.datetime, |
| 21 | + data_version: str) -> None: |
| 22 | + self.__id = id |
| 23 | + self.__data = data |
| 24 | + self.__subject = subject |
| 25 | + self.__topic = topic |
| 26 | + self.__event_type = event_type |
| 27 | + self.__event_time = event_time |
| 28 | + self.__data_version = data_version |
| 29 | + |
| 30 | + @property |
| 31 | + def id(self) -> str: |
| 32 | + return self.__id |
| 33 | + |
| 34 | + def get_json(self) -> typing.Any: |
| 35 | + return self.__data |
| 36 | + |
| 37 | + @property |
| 38 | + def topic(self) -> str: |
| 39 | + return self.__topic |
| 40 | + |
| 41 | + @property |
| 42 | + def subject(self) -> str: |
| 43 | + return self.__subject |
| 44 | + |
| 45 | + @property |
| 46 | + def event_type(self) -> str: |
| 47 | + return self.__event_type |
| 48 | + |
| 49 | + @property |
| 50 | + def event_time(self) -> datetime.datetime: |
| 51 | + return self.__event_time |
| 52 | + |
| 53 | + @property |
| 54 | + def data_version(self) -> str: |
| 55 | + return self.__data_version |
| 56 | + |
| 57 | + def __repr__(self) -> str: |
| 58 | + return ( |
| 59 | + f'<azure.EventGridEvent id={self.id} ' |
| 60 | + f'topic={self.topic} ' |
| 61 | + f'subject={self.subject} ' |
| 62 | + f'at 0x{id(self):0x}>' |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +class EventGridEventInConverter(meta.InConverter, |
| 67 | + binding='eventGridTrigger', trigger=True): |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def check_input_type_annotation(cls, pytype: type) -> bool: |
| 71 | + return issubclass(pytype, azf_abc.EventGridEvent) |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def from_proto(cls, data: protos.TypedData, *, |
| 75 | + pytype: typing.Optional[type], |
| 76 | + trigger_metadata) -> typing.Any: |
| 77 | + data_type = data.WhichOneof('data') |
| 78 | + |
| 79 | + if data_type == 'json': |
| 80 | + body = json.loads(data.json) |
| 81 | + else: |
| 82 | + raise NotImplementedError( |
| 83 | + f'unsupported event grid payload type: {data_type}') |
| 84 | + |
| 85 | + if trigger_metadata is None: |
| 86 | + raise NotImplementedError( |
| 87 | + f'missing trigger metadata for event grid input') |
| 88 | + |
| 89 | + return EventGridEvent( |
| 90 | + id=body.get('id'), |
| 91 | + topic=body.get('topic'), |
| 92 | + subject=body.get('subject'), |
| 93 | + event_type=body.get('eventType'), |
| 94 | + event_time=cls._parse_datetime(body.get('eventTime')), |
| 95 | + data=body.get('data'), |
| 96 | + data_version=body.get('dataVersion'), |
| 97 | + ) |
0 commit comments