def test_eg_consumer_eg_custom_string(self, **kwargs):
     client = EventGridDeserializer()
     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_storage_bytes(self, **kwargs):
     client = EventGridDeserializer()
     deserialized_event = client.deserialize_eventgrid_events(
         eg_storage_bytes)
     assert deserialized_event.__class__ == EventGridEvent
     assert deserialized_event.data.__class__ == StorageBlobCreatedEventData
        "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 = str(eg_storage_string).encode("utf-8")

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

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

deserialized_bytes_event = client.deserialize_eventgrid_events(eg_storage_bytes)
print(deserialized_bytes_event)
Пример #4
0
 def test_eg_consumer_eg_storage_dict(self, **kwargs):
     client = EventGridDeserializer()
     deserialized_event = client.deserialize_eventgrid_events(
         eg_storage_dict)
     assert deserialized_event.__class__ == EventGridEvent
     assert deserialized_event.data.__class__ == dict
# --------------------------------------------------------------------------
# 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 EventGridDeserializer

consumer = EventGridDeserializer()
path = os.path.abspath(
    os.path.join(os.path.abspath(__file__), "..",
                 "./cs4_event_grid_event_custom_event.json"))

with open(path, 'r') as f:
    eg_event_received_message = json.loads(f.read())

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

# returns { "itemSku": "Contoso Item SKU #1" }
data_dict = event.data
print(data_dict)
Пример #6
0
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))