Skip to content

Commit

Permalink
decodes
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshith91 committed Jan 15, 2021
1 parent 8dc2099 commit 7a4fdd3
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 32 deletions.
2 changes: 2 additions & 0 deletions sdk/eventgrid/azure-eventgrid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
**Breaking Changes**
- `EventGridSharedAccessSignatureCredential` is deprecated in favor of `AzureSasCredential`.
- `EventGridConsumer` is now renamed to `EventGridDeserializer`.
- `decode_cloud_event` is renamed to `deserialize_cloud_events`.
- `decode_eventgrid_event` is renamed to `deserialize_eventgrid_events`.

**Bug Fixes**
- `EventGridEvent` has two additional required positional parameters namely, `data` and `data_version`.
Expand Down
4 changes: 2 additions & 2 deletions sdk/eventgrid/azure-eventgrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ eg_storage_dict = {
"topic":"/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.EventGrid/topics/eventgridegsub"
}

deserialized_event = consumer.decode_eventgrid_event(eg_storage_dict)
deserialized_event = consumer.deserialize_eventgrid_events(eg_storage_dict)

# both allow access to raw properties as strings
time_string = deserialized_event.event_time
Expand All @@ -173,7 +173,7 @@ cloud_storage_dict = {
"specversion":"1.0"
}

deserialized_event = consumer.decode_cloud_event(cloud_storage_dict)
deserialized_event = consumer.deserialize_cloud_events(cloud_storage_dict)

# both allow access to raw properties as strings
time_string = deserialized_event.time
Expand Down
4 changes: 2 additions & 2 deletions sdk/eventgrid/azure-eventgrid/azure/eventgrid/_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EventGridDeserializer(object):
A consumer responsible for deserializing event handler messages, to allow for
access to strongly typed Event objects.
"""
def decode_cloud_event(self, cloud_event, **kwargs): # pylint: disable=no-self-use
def deserialize_cloud_events(self, cloud_event, **kwargs): # pylint: disable=no-self-use
# type: (Union[str, dict, bytes], Any) -> CloudEvent
"""Single event following CloudEvent schema will be parsed and returned as Deserialized Event.
:param cloud_event: The event to be deserialized.
Expand All @@ -44,7 +44,7 @@ def decode_cloud_event(self, cloud_event, **kwargs): # pylint: disable=no-self-u
raise ValueError('Error: cannot deserialize event. Event does not have a valid format. \
Event must be a string, dict, or bytes following the CloudEvent schema.')

def decode_eventgrid_event(self, eventgrid_event, **kwargs): # pylint: disable=no-self-use
def deserialize_eventgrid_events(self, eventgrid_event, **kwargs): # pylint: disable=no-self-use
# type: (Union[str, dict, bytes], Any) -> EventGridEvent
"""Single event following EventGridEvent schema will be parsed and returned as Deserialized Event.
:param eventgrid_event: The event to be deserialized.
Expand Down
4 changes: 2 additions & 2 deletions sdk/eventgrid/azure-eventgrid/migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ The `publish_events` API is replaced with `send` in v2.0. Additionally, `send` A
In v2.0, `EventGridDeserializer` can be used to decode both Cloud Events and EventGrid Events. Please find the samples [here](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventgrid/azure-eventgrid/samples/consume_samples) to see detailed examples of the consumer.

```Python
EventGridDeserializer().decode_cloud_event(cloud_event_dict)
EventGridDeserializer().deserialize_cloud_events(cloud_event_dict)

EventGridDeserializer().decode_eventgrid_event(eventgrid_event_dict)
EventGridDeserializer().deserialize_eventgrid_events(eventgrid_event_dict)
```

## Additional samples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
with open(path, 'r') as f:
eg_event_received_message = json.loads(f.read())
# returns List[DeserializedEvent]
event = consumer.decode_eventgrid_event(eg_event_received_message)
event = consumer.deserialize_eventgrid_events(eg_event_received_message)

datetime_object = event.event_time
print(datetime_object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
eg_event_received_message = json.loads(f.read())

# returns List[DeserializedEvent]
event = consumer.decode_eventgrid_event(eg_event_received_message)
event = consumer.deserialize_eventgrid_events(eg_event_received_message)

# returns { "itemSku": "Contoso Item SKU #1" }
data_dict = event.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
cloud_event_received_message = json.loads(f.read())

# returns List[DeserializedEvent]
event = consumer.decode_cloud_event(cloud_event_received_message)
event = consumer.deserialize_cloud_events(cloud_event_received_message)

datetime_object = event.time
print(datetime_object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
cloud_custom_bytes = str(cloud_custom_string).encode("utf-8")

client = EventGridDeserializer()
deserialized_dict_event = client.decode_cloud_event(cloud_custom_dict)
deserialized_dict_event = client.deserialize_cloud_events(cloud_custom_dict)
print(deserialized_dict_event)

deserialized_str_event = client.decode_cloud_event(cloud_custom_string)
deserialized_str_event = client.deserialize_cloud_events(cloud_custom_string)
print(deserialized_str_event)

deserialized_bytes_event = client.decode_cloud_event(cloud_custom_bytes)
deserialized_bytes_event = client.deserialize_cloud_events(cloud_custom_bytes)
print(deserialized_bytes_event)
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
eg_storage_bytes = str(eg_storage_string).encode("utf-8")

client = EventGridDeserializer()
deserialized_dict_event = client.decode_eventgrid_event(eg_storage_dict)
deserialized_dict_event = client.deserialize_eventgrid_events(eg_storage_dict)
print(deserialized_dict_event)

deserialized_str_event = client.decode_eventgrid_event(eg_storage_string)
deserialized_str_event = client.deserialize_eventgrid_events(eg_storage_string)
print(deserialized_str_event)

deserialized_bytes_event = client.decode_eventgrid_event(eg_storage_bytes)
deserialized_bytes_event = client.deserialize_eventgrid_events(eg_storage_bytes)
print(deserialized_bytes_event)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
def on_event(partition_context, event):

dict_event = event.body_as_json()[0]
deserialized_event = eg_consumer.decode_eventgrid_event(dict_event)
deserialized_event = eg_consumer.deserialize_eventgrid_events(dict_event)
if deserialized_event.model.__class__ == CloudEvent:
dict_event = deserialized_event.to_json()
print("event.type: {}\n".format(dict_event["type"]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
for msg in msgs:
# receive single dict message
if 'specversion' in msg:
deserialized_event = consumer.decode_cloud_event(str(msg))
deserialized_event = consumer.deserialize_cloud_events(str(msg))
dict_event = deserialized_event.to_json()
print("event.to_json(): {}\n".format(dict_event))
print("model: {}\n".format(deserialized_event.model))
print("model.data: {}\n".format(deserialized_event.model.data))
else:
deserialized_event = consumer.decode_eventgrid_event(str(msg))
deserialized_event = consumer.deserialize_eventgrid_events(str(msg))
dict_event = deserialized_event.to_json()
print("event.to_json(): {}\n".format(dict_event))
print("model: {}\n".format(deserialized_event.model))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
for msg in msgs:
# receive single dict message
if 'specversion' in msg:
deserialized_event = consumer.decode_cloud_event(b64decode(msg.content))
deserialized_event = consumer.deserialize_cloud_events(b64decode(msg.content))
dict_event = deserialized_event.to_json()
print("event.type: {}\n".format(dict_event["type"]))
print("event.to_json(): {}\n".format(dict_event))
print("model: {}\n".format(deserialized_event.model))
print("model.data: {}\n".format(deserialized_event.model.data))
else:
deserialized_event = consumer.decode_eventgrid_event(b64decode(msg.content))
deserialized_event = consumer.deserialize_eventgrid_events(b64decode(msg.content))
dict_event = deserialized_event.to_json()
print("event.to_json(): {}\n".format(dict_event))
print("model: {}\n".format(deserialized_event.model))
Expand Down
24 changes: 12 additions & 12 deletions sdk/eventgrid/azure-eventgrid/tests/test_eg_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,82 +35,82 @@ class EventGridDeserializerTests(AzureMgmtTestCase):
# Cloud Event tests
def test_eg_consumer_cloud_storage_dict(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_cloud_event(cloud_storage_dict)
deserialized_event = client.deserialize_cloud_events(cloud_storage_dict)
assert deserialized_event.__class__ == CloudEvent
assert deserialized_event.data.__class__ == StorageBlobCreatedEventData

def test_eg_consumer_cloud_storage_string(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_cloud_event(cloud_storage_string)
deserialized_event = client.deserialize_cloud_events(cloud_storage_string)
assert deserialized_event.__class__ == CloudEvent
assert deserialized_event.data.__class__ == StorageBlobCreatedEventData

def test_eg_consumer_cloud_storage_bytes(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_cloud_event(cloud_storage_bytes)
deserialized_event = client.deserialize_cloud_events(cloud_storage_bytes)
assert deserialized_event.__class__ == CloudEvent
assert deserialized_event.data.__class__ == StorageBlobCreatedEventData


def test_eg_consumer_cloud_custom_dict(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_cloud_event(cloud_custom_dict)
deserialized_event = client.deserialize_cloud_events(cloud_custom_dict)
assert deserialized_event.__class__ == CloudEvent
assert deserialized_event.data is None


def test_eg_consumer_cloud_custom_string(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_cloud_event(cloud_custom_string)
deserialized_event = client.deserialize_cloud_events(cloud_custom_string)
assert deserialized_event.__class__ == CloudEvent
assert deserialized_event.data is None


def test_eg_consumer_cloud_custom_bytes(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_cloud_event(cloud_custom_bytes)
deserialized_event = client.deserialize_cloud_events(cloud_custom_bytes)
assert deserialized_event.__class__ == CloudEvent
assert deserialized_event.data is None

# EG Event tests

def test_eg_consumer_eg_storage_dict(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_eventgrid_event(eg_storage_dict)
deserialized_event = client.deserialize_eventgrid_events(eg_storage_dict)
assert deserialized_event.__class__ == EventGridEvent
assert deserialized_event.data.__class__ == StorageBlobCreatedEventData


def test_eg_consumer_eg_storage_string(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_eventgrid_event(eg_storage_string)
deserialized_event = client.deserialize_eventgrid_events(eg_storage_string)
assert deserialized_event.__class__ == EventGridEvent
assert deserialized_event.data.__class__ == StorageBlobCreatedEventData


def test_eg_consumer_eg_storage_bytes(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_eventgrid_event(eg_storage_bytes)
deserialized_event = client.deserialize_eventgrid_events(eg_storage_bytes)
assert deserialized_event.__class__ == EventGridEvent
assert deserialized_event.data.__class__ == StorageBlobCreatedEventData


def test_eg_consumer_eg_custom_dict(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_eventgrid_event(eg_custom_dict)
deserialized_event = client.deserialize_eventgrid_events(eg_custom_dict)
assert deserialized_event.__class__ == EventGridEvent
assert deserialized_event.data is None


def test_eg_consumer_eg_custom_string(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_eventgrid_event(eg_custom_string)
deserialized_event = client.deserialize_eventgrid_events(eg_custom_string)
assert deserialized_event.__class__ == EventGridEvent
assert deserialized_event.data is None


def test_eg_consumer_eg_custom_bytes(self, **kwargs):
client = EventGridDeserializer()
deserialized_event = client.decode_eventgrid_event(eg_custom_bytes)
deserialized_event = client.deserialize_eventgrid_events(eg_custom_bytes)
assert deserialized_event.__class__ == EventGridEvent
assert deserialized_event.data is None

0 comments on commit 7a4fdd3

Please sign in to comment.