from azure.core.credentials import AzureKeyCredential from azure.eventgrid import EventGridConsumer, CloudEvent from azure.servicebus import ServiceBusClient connection_str = os.environ['SB_CONN_STR'] queue_name = os.environ['SERVICE_BUS_QUEUE_NAME'] sb_client = ServiceBusClient.from_connection_string(connection_str) consumer = EventGridConsumer() with sb_client: receiver = sb_client.get_queue_receiver(queue_name, prefetch=10) with receiver: msgs = receiver.receive(max_batch_size=10, max_wait_time=1) print("number of messages: {}".format(len(msgs))) for msg in msgs: # receive single dict message if 'specversion' in msg: deserialized_event = consumer.decode_cloud_event(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)) 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)) msg.complete()
"subject":"/blobServices/default/containers/oc2d2817345i200097container/blobs/oc2d2817345i20002296blob", "data":{ "api":"PutBlockList", "clientRequestId":"6d79dbfb-0e37-4fc4-981f-442c9ca65760", "requestId":"831e1650-001e-001b-66ab-eeb76e000000", "eTag":"0x8D4BCC2E4835CD0", "contentType":"application/octet-stream", "contentLength":524288, "blobType":"BlockBlob", "url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob", "sequencer":"00000000000004420000000000028963", "storageDiagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"} }, "eventType":"Microsoft.Storage.BlobCreated", "dataVersion":"2.0", "metadataVersion":"1", "eventTime":"2020-08-07T02:28:23.867525Z", "topic":"/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/t-swpill-test/providers/Microsoft.EventGrid/topics/eventgridegsub" } eg_storage_string = json.dumps(eg_storage_dict) eg_storage_bytes = bytes(eg_storage_string, "utf-8") client = EventGridConsumer() deserialized_dict_event = client.decode_eventgrid_event(eg_storage_dict) deserialized_str_event = client.decode_eventgrid_event(eg_storage_string) deserialized_bytes_event = client.decode_eventgrid_event(eg_storage_bytes) print(deserialized_bytes_event.model == deserialized_str_event.model) print(deserialized_bytes_event.model == deserialized_dict_event.model) print(deserialized_str_event.model.data.__class__ == StorageBlobCreatedEventData)
# -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- """ FILE: cs4_consume_custom_events.py DESCRIPTION: These samples demonstrate deserializing a custom event USAGE: python cs4_consume_custom_events.py """ import os import json from azure.eventgrid import EventGridConsumer consumer = EventGridConsumer() with open('./cs4_event_grid_event_custom_event.json', 'r') as f: eg_event_received_message = json.loads(f.read()) # returns List[DeserializedEvent] event = consumer.decode_eventgrid_event(eg_event_received_message) # returns { "itemSku": "Contoso Item SKU #1" } data_dict = event.data print(data_dict)
from azure.storage.queue import QueueServiceClient from azure.eventgrid import EventGridConsumer, 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 = EventGridConsumer() msgs = queue_client.receive_messages() for msg in msgs: # receive single dict message if 'specversion' in msg: deserialized_event = consumer.decode_cloud_event(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)) 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))
# license information. # -------------------------------------------------------------------------- """ FILE: cs3_consume_system_events.py DESCRIPTION: These samples demonstrate deserializing a message from system event. USAGE: python cs3_consume_system_events.py """ import os from azure.eventgrid import EventGridConsumer consumer = EventGridConsumer() # returns List[DeserializedEvent] deserialized_events = consumer.decode_eventgrid_event(service_bus_received_message) # EventGridEvent schema, Storage.BlobCreated event for event in deserialized_events: # both allow access to raw properties as strings time_string = event.event_time time_string = event["event_time"] # model returns EventGridEvent object event_grid_event = event.model # all model properties are strongly typed datetime_object = event.model.time storage_blobcreated_object = event.model.data
def test_eg_consumer_eg_custom_bytes(self, **kwargs): client = EventGridConsumer() deserialized_event = client.decode_eventgrid_event(eg_custom_bytes) assert deserialized_event.__class__ == EventGridEvent assert deserialized_event.data is None
def test_eg_consumer_eg_storage_bytes(self, **kwargs): client = EventGridConsumer() deserialized_event = client.decode_eventgrid_event(eg_storage_bytes) assert deserialized_event.__class__ == EventGridEvent assert deserialized_event.data.__class__ == StorageBlobCreatedEventData