Ejemplo n.º 1
0
 def test_eg_consumer_eg_custom_dict(self, **kwargs):
     client = EventGridConsumer()
     deserialized_event = client.decode_eventgrid_event(eg_custom_dict)
     assert deserialized_event.__class__ == EventGridEvent
     assert deserialized_event.data is None
Ejemplo n.º 2
0
# --------------------------------------------------------------------------
# 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
from azure.eventgrid import EventGridConsumer

consumer = EventGridConsumer()

# returns List[DeserializedEvent]
deserialized_events = consumer.decode_eventgrid_event(
    service_bus_received_message)

# EventGridEvent schema, with custom event type
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
Ejemplo n.º 3
0
 def test_eg_consumer_cloud_custom_bytes(self, **kwargs):
     client = EventGridConsumer()
     deserialized_event = client.decode_cloud_event(cloud_custom_bytes)
     assert deserialized_event.__class__ == CloudEvent
     assert deserialized_event.data is None
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 def test_eg_consumer_cloud_storage_string(self, **kwargs):
     client = EventGridConsumer()
     deserialized_event = client.decode_cloud_event(cloud_storage_string)
     assert deserialized_event.__class__ == CloudEvent
     assert deserialized_event.data.__class__ == StorageBlobCreatedEventData
Ejemplo n.º 6
0
        "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.deserialize_event(eg_storage_dict)
deserialized_str_event = client.deserialize_event(eg_storage_string)
deserialized_bytes_event = client.deserialize_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)
Ejemplo n.º 7
0
def on_event(partition_context, event):

    dict_event = event.body_as_json()[0]
    deserialized_event = eg_consumer.deserialize_event(dict_event)
    if deserialized_event.model.__class__ == CloudEvent:
        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:
        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))


eg_consumer = EventGridConsumer()
consumer_client = EventHubConsumerClient.from_connection_string(
    conn_str=CONNECTION_STR,
    consumer_group='$Default',
    eventhub_name=EVENTHUB_NAME,
)

with consumer_client:
    event_list = consumer_client.receive(
        on_event=on_event,
        starting_position="-1",  # "-1" is from the beginning of the partition.
        prefetch=5)
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 EventGridConsumer, 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 = bytes(cloud_custom_string, "utf-8")

client = EventGridConsumer()
deserialized_dict_event = client.decode_cloud_event(cloud_custom_dict)
print(deserialized_dict_event)

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

deserialized_bytes_event = client.decode_cloud_event(cloud_custom_bytes)
print(deserialized_bytes_event)
Ejemplo n.º 9
0
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# 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
import json
from azure.eventgrid import EventGridConsumer

consumer = EventGridConsumer()
path = os.path.abspath(
    os.path.join(os.path.abspath(__file__), "..", "./cs3_system_event.json"))

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)

datetime_object = event.event_time
print(datetime_object)

storage_blobcreated_object = event.data
print(storage_blobcreated_object)