def test_eg_consumer_cloud_custom_bytes(self, **kwargs): client = EventGridDeserializer() deserialized_event = client.deserialize_cloud_events( cloud_custom_bytes) assert deserialized_event.__class__ == CloudEvent assert deserialized_event.data is None
def test_eg_consumer_cloud_storage_bytes(self, **kwargs): client = EventGridDeserializer() 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_storage_string(self, **kwargs): client = EventGridDeserializer() deserialized_event = client.deserialize_cloud_events( cloud_storage_string) assert deserialized_event.__class__ == CloudEvent assert deserialized_event.data.__class__ == dict
from azure.storage.queue import QueueServiceClient from azure.eventgrid import EventGridDeserializer, CloudEvent from base64 import b64decode connection_str = os.environ["STORAGE_QUEUE_CONN_STR"] queue_name = os.environ["STORAGE_QUEUE_NAME"] queue_service = QueueServiceClient.from_connection_string( conn_str=connection_str) queue_client = queue_service.get_queue_client(queue_name) consumer = EventGridDeserializer() msgs = queue_client.receive_messages() for msg in msgs: # receive single dict message if 'specversion' in msg: 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.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)) print("model.data: {}\n".format(deserialized_event.model.data))
# -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- """ FILE: cs6_consume_events_using_cloud_events_1.0_schema.py DESCRIPTION: These samples demonstrate creating a list of CloudEvents and sending then as a list. USAGE: python cs6_consume_events_using_cloud_events_1.0_schema.py """ import os import json from azure.eventgrid import EventGridDeserializer consumer = EventGridDeserializer() path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./cs6_cloud_event_system_event.json")) with open(path, 'r') as f: cloud_event_received_message = json.loads(f.read()) # returns List[DeserializedEvent] event = consumer.deserialize_cloud_events(cloud_event_received_message) datetime_object = event.time print(datetime_object) storage_blobcreated_object = event.data print(storage_blobcreated_object)
USAGE: python consume_cloud_custom_data_sample.py Set the environment variables with your own values before running the sample: """ import json from azure.eventgrid import EventGridDeserializer, CloudEvent # all types of CloudEvents below produce same DeserializedEvent cloud_custom_dict = { "id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "source": "https://egtest.dev/cloudcustomevent", "data": { "team": "event grid squad" }, "type": "Azure.Sdk.Sample", "time": "2020-08-07T02:06:08.11969Z", "specversion": "1.0" } cloud_custom_string = json.dumps(cloud_custom_dict) cloud_custom_bytes = str(cloud_custom_string).encode("utf-8") client = EventGridDeserializer() deserialized_dict_event = client.deserialize_cloud_events(cloud_custom_dict) print(deserialized_dict_event) deserialized_str_event = client.deserialize_cloud_events(cloud_custom_string) print(deserialized_str_event) deserialized_bytes_event = client.deserialize_cloud_events(cloud_custom_bytes) print(deserialized_bytes_event)