def test_data_and_base64_both_exist_raises(): with pytest.raises(ValueError): CloudEvent.from_dict({ "source": 'sample', "type": 'type', "data": 'data', "data_base64": 'Y2kQ==' })
def test_wrong_schema_raises_no_type(): cloud_custom_dict = { "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "data":{"team": "event grid squad"}, "source":"Azure/Sdk/Sample", "time":"2020-08-07T02:06:08.11969Z", "specversion":"1.0", } with pytest.raises(ValueError, match="The event does not conform to the cloud event spec https://github.com/cloudevents/spec. The `source` and `type` params are required."): CloudEvent.from_dict(cloud_custom_dict)
def test_eventgrid_event_schema_raises(): cloud_custom_dict = { "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "data":{"team": "event grid squad"}, "dataVersion": "1.0", "subject":"Azure.Sdk.Sample", "eventTime":"2020-08-07T02:06:08.11969Z", "eventType":"pull request", } with pytest.raises(ValueError, match="The event you are trying to parse follows the Eventgrid Schema. You can parse EventGrid events using EventGridEvent.from_dict method in the azure-eventgrid library."): CloudEvent.from_dict(cloud_custom_dict)
def test_cloud_custom_dict_no_data(): cloud_custom_dict_with_missing_data = { "id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "source": "https://egtest.dev/cloudcustomevent", "type": "Azure.Sdk.Sample", "time": "2021-02-18T20:18:10+00:00", "specversion": "1.0", } event = CloudEvent.from_dict(cloud_custom_dict_with_missing_data) assert event.__class__ == CloudEvent assert event.data is None
def test_cloud_custom_dict_both_data_and_base64(): cloud_custom_dict_with_data_and_base64 = { "id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "source": "https://egtest.dev/cloudcustomevent", "data": "abc", "data_base64": "Y2Wa==", "type": "Azure.Sdk.Sample", "time": "2021-02-18T20:18:10+00:00", "specversion": "1.0", } with pytest.raises(ValueError): event = CloudEvent.from_dict(cloud_custom_dict_with_data_and_base64)
def test_cloud_from_dict_with_invalid_extensions(): cloud_custom_dict_with_extensions = { "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", "ext1": "example", "BADext2": "example2" } with pytest.raises(ValueError): event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
def test_cloud_custom_dict_valid_optional_attrs(): cloud_custom_dict_with_none_data = { "id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "source": "https://egtest.dev/cloudcustomevent", "type": "Azure.Sdk.Sample", "data": None, "dataschema": "exists", "time": "2021-02-18T20:18:10+00:00", "specversion": "1.0", } event = CloudEvent.from_dict(cloud_custom_dict_with_none_data) assert event.__class__ == CloudEvent assert event.data is NULL assert event.dataschema == "exists"
def test_cloud_storage_dict(): cloud_storage_dict = { "id": "a0517898-9fa4-4e70-b4a3-afda1dd68672", "source": "/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-account}", "data": { "api": "PutBlockList", "client_request_id": "6d79dbfb-0e37-4fc4-981f-442c9ca65760", "request_id": "831e1650-001e-001b-66ab-eeb76e000000", "e_tag": "0x8D4BCC2E4835CD0", "content_type": "application/octet-stream", "content_length": 524288, "blob_type": "BlockBlob", "url": "https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob", "sequencer": "00000000000004420000000000028963", "storage_diagnostics": { "batchId": "b68529f3-68cd-4744-baa4-3c0498ec19f0" } }, "type": "Microsoft.Storage.BlobCreated", "time": "2021-02-18T20:18:10.581147898Z", "specversion": "1.0" } event = CloudEvent.from_dict(cloud_storage_dict) assert event.data == { "api": "PutBlockList", "client_request_id": "6d79dbfb-0e37-4fc4-981f-442c9ca65760", "request_id": "831e1650-001e-001b-66ab-eeb76e000000", "e_tag": "0x8D4BCC2E4835CD0", "content_type": "application/octet-stream", "content_length": 524288, "blob_type": "BlockBlob", "url": "https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob", "sequencer": "00000000000004420000000000028963", "storage_diagnostics": { "batchId": "b68529f3-68cd-4744-baa4-3c0498ec19f0" } } assert event.specversion == "1.0" assert event.time.__class__ == datetime.datetime assert event.time.month == 2 assert event.time.day == 18 assert event.time.hour == 20 assert event.time.microsecond == 581147 assert event.__class__ == CloudEvent assert "id" in cloud_storage_dict assert "data" in cloud_storage_dict
def test_cloud_custom_dict_ms_precision_is_eq_six_z_not(): cloud_custom_dict_with_extensions = { "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e034", "source":"https://egtest.dev/cloudcustomevent", "data":{"team": "event grid squad"}, "type":"Azure.Sdk.Sample", "time":"2021-02-18T20:18:10.123456Z", "specversion":"1.0", } event = CloudEvent.from_dict(cloud_custom_dict_with_extensions) assert event.data == {"team": "event grid squad"} assert event.__class__ == CloudEvent assert event.time.month == 2 assert event.time.day == 18 assert event.time.hour == 20 assert event.time.microsecond == 123456
def test_cloud_custom_dict_base64(): cloud_custom_dict_base64 = { "id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "source": "https://egtest.dev/cloudcustomevent", "data_base64": 'Y2xvdWRldmVudA==', "type": "Azure.Sdk.Sample", "time": "2021-02-23T17:11:13.308772-08:00", "specversion": "1.0" } event = CloudEvent.from_dict(cloud_custom_dict_base64) assert event.data == b'cloudevent' assert event.specversion == "1.0" assert event.time.hour == 17 assert event.time.minute == 11 assert event.time.day == 23 assert event.time.tzinfo is not None assert event.__class__ == CloudEvent
def test_cloud_custom_dict_with_extensions(): cloud_custom_dict_with_extensions = { "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "source":"https://egtest.dev/cloudcustomevent", "data":{"team": "event grid squad"}, "type":"Azure.Sdk.Sample", "time":"2021-02-18T20:18:10.539861122+00:00", "specversion":"1.0", "ext1": "example", "ext2": "example2" } event = CloudEvent.from_dict(cloud_custom_dict_with_extensions) assert event.data == {"team": "event grid squad"} assert event.__class__ == CloudEvent assert event.time.month == 2 assert event.time.day == 18 assert event.time.hour == 20 assert event.time.microsecond == 539861 assert event.extensions == {"ext1": "example", "ext2": "example2"}
""" FILE: consume_cloud_events_from_storage_queue.py DESCRIPTION: These samples demonstrate receiving events from a Storage Queue. USAGE: python consume_cloud_events_from_storage_queue.py Set the environment variables with your own values before running the sample: 1) STORAGE_QUEUE_CONN_STR: The connection string to the Storage account 3) STORAGE_QUEUE_NAME: The name of the storage queue. """ from azure.core.messaging import CloudEvent from azure.storage.queue import QueueServiceClient, BinaryBase64DecodePolicy import os import json # all types of CloudEvents below produce same DeserializedEvent connection_str = os.environ['AZURE_STORAGE_CONNECTION_STRING'] queue_name = os.environ['STORAGE_QUEUE_NAME'] with QueueServiceClient.from_connection_string(connection_str) as qsc: payload = qsc.get_queue_client( queue=queue_name, message_decode_policy=BinaryBase64DecodePolicy()).peek_messages() ## deserialize payload into a list of typed Events events = [CloudEvent.from_dict(json.loads(msg.content)) for msg in payload] for event in events: print(type(event)) ## CloudEvent
def on_event(partition_context, event): dict_event = CloudEvent.from_dict(json.loads(event)[0]) print("data: {}\n".format(deserialized_event.data))