|
| 1 | +import datetime |
| 2 | +import typing |
| 3 | + |
| 4 | +from azure.functions import _servicebus as azf_sbus |
| 5 | + |
| 6 | +from . import meta |
| 7 | +from .. import protos |
| 8 | + |
| 9 | + |
| 10 | +class ServiceBusMessage(azf_sbus.ServiceBusMessage): |
| 11 | + """An HTTP response object.""" |
| 12 | + |
| 13 | + def __init__( |
| 14 | + self, *, |
| 15 | + body: bytes, |
| 16 | + content_type: typing.Optional[str]=None, |
| 17 | + correlation_id: typing.Optional[str]=None, |
| 18 | + expiration_time: typing.Optional[datetime.datetime]=None, |
| 19 | + label: typing.Optional[str]=None, |
| 20 | + message_id: str, |
| 21 | + partition_key: typing.Optional[str]=None, |
| 22 | + reply_to: typing.Optional[str]=None, |
| 23 | + reply_to_session_id: typing.Optional[str]=None, |
| 24 | + scheduled_enqueue_time: typing.Optional[datetime.datetime]=None, |
| 25 | + session_id: typing.Optional[str]=None, |
| 26 | + time_to_live: typing.Optional[datetime.timedelta]=None, |
| 27 | + to: typing.Optional[str]=None, |
| 28 | + user_properties: typing.Dict[str, object]) -> None: |
| 29 | + |
| 30 | + self.__body = body |
| 31 | + self.__content_type = content_type |
| 32 | + self.__correlation_id = correlation_id |
| 33 | + self.__expiration_time = expiration_time |
| 34 | + self.__label = label |
| 35 | + self.__message_id = message_id |
| 36 | + self.__partition_key = partition_key |
| 37 | + self.__reply_to = reply_to |
| 38 | + self.__reply_to_session_id = reply_to_session_id |
| 39 | + self.__scheduled_enqueue_time = scheduled_enqueue_time |
| 40 | + self.__session_id = session_id |
| 41 | + self.__time_to_live = time_to_live |
| 42 | + self.__to = to |
| 43 | + self.__user_properties = user_properties |
| 44 | + |
| 45 | + def get_body(self) -> bytes: |
| 46 | + return self.__body |
| 47 | + |
| 48 | + @property |
| 49 | + def content_type(self) -> typing.Optional[str]: |
| 50 | + return self.__content_type |
| 51 | + |
| 52 | + @property |
| 53 | + def correlation_id(self) -> typing.Optional[str]: |
| 54 | + return self.__correlation_id |
| 55 | + |
| 56 | + @property |
| 57 | + def expiration_time(self) -> typing.Optional[datetime.datetime]: |
| 58 | + return self.__expiration_time |
| 59 | + |
| 60 | + @property |
| 61 | + def label(self) -> typing.Optional[str]: |
| 62 | + return self.__label |
| 63 | + |
| 64 | + @property |
| 65 | + def message_id(self) -> str: |
| 66 | + return self.__message_id |
| 67 | + |
| 68 | + @property |
| 69 | + def partition_key(self) -> typing.Optional[str]: |
| 70 | + return self.__partition_key |
| 71 | + |
| 72 | + @property |
| 73 | + def reply_to(self) -> typing.Optional[str]: |
| 74 | + return self.__reply_to |
| 75 | + |
| 76 | + @property |
| 77 | + def reply_to_session_id(self) -> typing.Optional[str]: |
| 78 | + return self.__reply_to_session_id |
| 79 | + |
| 80 | + @property |
| 81 | + def scheduled_enqueue_time(self) -> typing.Optional[datetime.datetime]: |
| 82 | + return self.__scheduled_enqueue_time |
| 83 | + |
| 84 | + @property |
| 85 | + def session_id(self) -> typing.Optional[str]: |
| 86 | + return self.__session_id |
| 87 | + |
| 88 | + @property |
| 89 | + def time_to_live(self) -> typing.Optional[datetime.timedelta]: |
| 90 | + return self.__time_to_live |
| 91 | + |
| 92 | + @property |
| 93 | + def to(self) -> typing.Optional[str]: |
| 94 | + return self.__to |
| 95 | + |
| 96 | + @property |
| 97 | + def user_properties(self) -> typing.Dict[str, object]: |
| 98 | + return self.__user_properties |
| 99 | + |
| 100 | + def __repr__(self) -> str: |
| 101 | + return ( |
| 102 | + f'<azure.functions.ServiceBusMessage ' |
| 103 | + f'message_id={self.message_id} ' |
| 104 | + f'at 0x{id(self):0x}>' |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +class ServiceBusMessageInConverter(meta.InConverter, |
| 109 | + binding='serviceBusTrigger', trigger=True): |
| 110 | + |
| 111 | + @classmethod |
| 112 | + def check_input_type_annotation(cls, pytype: type) -> bool: |
| 113 | + return issubclass(pytype, azf_sbus.ServiceBusMessage) |
| 114 | + |
| 115 | + @classmethod |
| 116 | + def from_proto(cls, data: protos.TypedData, *, |
| 117 | + pytype: typing.Optional[type], |
| 118 | + trigger_metadata) -> typing.Any: |
| 119 | + data_type = data.WhichOneof('data') |
| 120 | + |
| 121 | + if data_type == 'string': |
| 122 | + body = data.string.encode('utf-8') |
| 123 | + |
| 124 | + elif data_type == 'bytes': |
| 125 | + body = data.bytes |
| 126 | + |
| 127 | + elif data_type == 'json': |
| 128 | + body = data.json.encode('utf-8') |
| 129 | + |
| 130 | + else: |
| 131 | + raise NotImplementedError( |
| 132 | + f'unsupported queue payload type: {data_type}') |
| 133 | + |
| 134 | + if trigger_metadata is None: |
| 135 | + raise NotImplementedError( |
| 136 | + f'missing trigger metadata for ServiceBus message input') |
| 137 | + |
| 138 | + return ServiceBusMessage( |
| 139 | + body=body, |
| 140 | + content_type=cls._decode_trigger_metadata_field( |
| 141 | + trigger_metadata, 'ContentType', python_type=str), |
| 142 | + correlation_id=cls._decode_trigger_metadata_field( |
| 143 | + trigger_metadata, 'CorrelationId', python_type=str), |
| 144 | + expiration_time=cls._parse_datetime_metadata( |
| 145 | + trigger_metadata, 'ExpirationTime'), |
| 146 | + label=cls._decode_trigger_metadata_field( |
| 147 | + trigger_metadata, 'Label', python_type=str), |
| 148 | + message_id=cls._decode_trigger_metadata_field( |
| 149 | + trigger_metadata, 'MessageId', python_type=str), |
| 150 | + partition_key=cls._decode_trigger_metadata_field( |
| 151 | + trigger_metadata, 'PartitionKey', python_type=str), |
| 152 | + reply_to=cls._decode_trigger_metadata_field( |
| 153 | + trigger_metadata, 'ReplyTo', python_type=str), |
| 154 | + reply_to_session_id=cls._decode_trigger_metadata_field( |
| 155 | + trigger_metadata, 'ReplyToSessionId', python_type=str), |
| 156 | + scheduled_enqueue_time=cls._parse_datetime_metadata( |
| 157 | + trigger_metadata, 'ScheduledEnqueueTime'), |
| 158 | + session_id=cls._decode_trigger_metadata_field( |
| 159 | + trigger_metadata, 'SessionId', python_type=str), |
| 160 | + time_to_live=cls._parse_timedelta_metadata( |
| 161 | + trigger_metadata, 'TimeToLive'), |
| 162 | + to=cls._decode_trigger_metadata_field( |
| 163 | + trigger_metadata, 'To', python_type=str), |
| 164 | + user_properties=cls._decode_trigger_metadata_field( |
| 165 | + trigger_metadata, 'UserProperties', python_type=dict), |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +class ServiceBusMessageOutConverter(meta.OutConverter, binding='serviceBus'): |
| 170 | + |
| 171 | + @classmethod |
| 172 | + def check_output_type_annotation(cls, pytype: type) -> bool: |
| 173 | + return issubclass(pytype, (str, bytes)) |
| 174 | + |
| 175 | + @classmethod |
| 176 | + def to_proto(cls, obj: typing.Any, *, |
| 177 | + pytype: typing.Optional[type]) -> protos.TypedData: |
| 178 | + if isinstance(obj, str): |
| 179 | + return protos.TypedData(string=obj) |
| 180 | + |
| 181 | + elif isinstance(obj, bytes): |
| 182 | + return protos.TypedData(bytes=obj) |
| 183 | + |
| 184 | + raise NotImplementedError |
0 commit comments