|
| 1 | +# coding=utf-8 |
| 2 | +# -------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | +# Code generated by Microsoft (R) AutoRest Code Generator. |
| 6 | +# Changes may cause incorrect behavior and will be lost if the code is regenerated. |
| 7 | +# -------------------------------------------------------------------------- |
| 8 | + |
| 9 | +from typing import TYPE_CHECKING |
| 10 | +import json |
| 11 | +import six |
| 12 | +import logging |
| 13 | + |
| 14 | +from azure.core import PipelineClient |
| 15 | +from msrest import Deserializer, Serializer |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + # pylint: disable=unused-import,ungrouped-imports |
| 19 | + from typing import Any |
| 20 | + |
| 21 | +_LOGGER = logging.getLogger(__name__) |
| 22 | + |
| 23 | +from ._models import DeserializedEvent |
| 24 | + |
| 25 | +class EventGridConsumer(object): |
| 26 | + """ |
| 27 | + A consumer responsible for deserializing event handler messages, to allow for access to strongly typed Event objects. |
| 28 | + """ |
| 29 | + |
| 30 | + def deserialize_event(self, event, **kwargs): |
| 31 | + # type: (Union[str, dict, bytes]) -> models.DeserializedEvent |
| 32 | + """Single event following CloudEvent/EventGridEvent schema will be parsed and returned as DeserializedEvent. |
| 33 | + :param event: The event to be deserialized. |
| 34 | + :type event: Union[str, dict, bytes] |
| 35 | + :rtype: models.DeserializedEvent |
| 36 | +
|
| 37 | + :raise: :class:`ValueError`, when events do not follow CloudEvent or EventGridEvent schema. |
| 38 | + """ |
| 39 | + encode = kwargs.pop('encoding', 'utf-8') |
| 40 | + try: |
| 41 | + if isinstance(event, six.binary_type): |
| 42 | + event = json.loads(event.decode(encode)) |
| 43 | + elif isinstance(event, six.string_types): |
| 44 | + event = json.loads(event) |
| 45 | + return DeserializedEvent(event) |
| 46 | + except Exception as err: |
| 47 | + _LOGGER.error('Error: cannot deserialize event. Event does not have a valid format. Event must be a string, dict, or bytes following the CloudEvent/EventGridEvent schema.') |
| 48 | + _LOGGER.error('Your event: {}'.format(event)) |
| 49 | + _LOGGER.error(err) |
| 50 | + raise ValueError('Error: cannot deserialize event. Event does not have a valid format. Event must be a string, dict, or bytes following the CloudEvent/EventGridEvent schema.') |
0 commit comments