Exemple #1
0
 def test_eg_consumer_eg_storage_string(self, **kwargs):
     client = EventGridConsumer()
     deserialized_event = client.deserialize_event(eg_storage_string)
     event_json = deserialized_event.to_json()
     assert deserialized_event.model.__class__ == EventGridEvent
     assert deserialized_event.model.data.__class__ == StorageBlobCreatedEventData
     assert event_json.__class__ == dict
Exemple #2
0
 def test_eg_consumer_cloud_custom_bytes(self, **kwargs):
     client = EventGridConsumer()
     deserialized_event = client.deserialize_event(cloud_custom_bytes)
     event_json = deserialized_event.to_json()
     assert deserialized_event.model.__class__ == CloudEvent
     assert deserialized_event.model.data is None
     assert event_json.__class__ == dict
Exemple #3
0
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.deserialize_event(cloud_custom_dict)
deserialized_str_event = client.deserialize_event(cloud_custom_string)
deserialized_bytes_event = client.deserialize_event(cloud_custom_bytes)

print(deserialized_bytes_event.model == deserialized_str_event.model)
print(deserialized_bytes_event.model == deserialized_dict_event.model)
Exemple #4
0
import os
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
    deserialized_event = consumer.deserialize_event(b64decode(msg.content))
    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))
Exemple #5
0
from azure.core.pipeline.policies import AzureKeyCredentialPolicy
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
            deserialized_event = consumer.deserialize_event(str(msg))
            if deserialized_event.model.__class__ == CloudEvent:
                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:
                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()
Exemple #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)