Esempio n. 1
0
# --------------------------------------------------------------------------
# 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 EventGridConsumer

consumer = EventGridConsumer()

with open('./cs6_cloud_event_system_event.json', 'r') as f:
    cloud_event_received_message = json.loads(f.read())

# returns List[DeserializedEvent]
event = consumer.decode_cloud_event(cloud_event_received_message)

datetime_object = event.time
print(datetime_object)

storage_blobcreated_object = event.data
print(storage_blobcreated_object)
Esempio n. 2
0
# --------------------------------------------------------------------------
"""
FILE: consume_cloud_custom_data_sample.py
DESCRIPTION:
    These samples demonstrate consuming custom cloud data
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)
deserialized_str_event = client.decode_cloud_event(cloud_custom_string)
deserialized_bytes_event = client.decode_cloud_event(cloud_custom_bytes)

print(deserialized_bytes_event.model == deserialized_str_event.model)
print(deserialized_bytes_event.model == deserialized_dict_event.model)
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()
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))
 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
 def test_eg_consumer_cloud_storage_bytes(self, **kwargs):
     client = EventGridConsumer()
     deserialized_event = client.decode_cloud_event(cloud_storage_bytes)
     assert deserialized_event.__class__ == CloudEvent
     assert deserialized_event.data.__class__ == StorageBlobCreatedEventData