diff --git a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py index d98de8ab82ed..9fb0cd9e559e 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py +++ b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py @@ -1,8 +1,25 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: cs1_publish_custom_events_to_a_topic.py +DESCRIPTION: + These samples demonstrate sending an EventGrid Event. +USAGE: + python cs1_publish_custom_events_to_a_topic.py + Set the environment variables with your own values before running the sample: + 1) EG_ACCESS_KEY - The access key of your eventgrid account. + 2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". +""" + from azure.eventgrid import EventGridPublisherClient, EventGridEvent, CloudEvent from azure.core.credentials import AzureKeyCredential -topic_hostname = ".-1.eventgrid.azure.net" -topic_key = "" +topic_key = os.environ["EG_ACCESS_KEY"] +topic_hostname = os.environ["EG_TOPIC_HOSTNAME"] credential = AzureKeyCredential(topic_key) client = EventGridPublisherClient(topic_hostname, credential) diff --git a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1b_publish_custom_events_to_a_topic_with_signature.py b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1b_publish_custom_events_to_a_topic_with_signature.py index b320f603ea65..fc28c92e0726 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1b_publish_custom_events_to_a_topic_with_signature.py +++ b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1b_publish_custom_events_to_a_topic_with_signature.py @@ -1,8 +1,25 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: cs1b_publish_custom_events_to_a_topic_with_signature.py +DESCRIPTION: + These samples demonstrate sending an EventGrid Event using a shared access signature for authentication. +USAGE: + python cs1b_publish_custom_events_to_a_topic_with_signature.py + Set the environment variables with your own values before running the sample: + 1) EG_ACCESS_KEY - The access key of your eventgrid account. + 2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". +""" + from azure.eventgrid import EventGridPublisherClient, EventGridEvent, CloudEvent, generate_shared_access_signature, EventGridSharedAccessSignatureCredential from azure.core.credentials import AzureKeyCredential -topic_hostname = ".-1.eventgrid.azure.net" -topic_key = "" +topic_key = os.environ["EG_ACCESS_KEY"] +topic_hostname = os.environ["EG_TOPIC_HOSTNAME"] expiration_date_utc = dt.datetime.now(tzutc()) + timedelta(hours=1) signature = generate_shared_access_signature(topic_hostname, topic_key, expiration_date_utc) diff --git a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py index 3221d4ad30d5..0cbb9397c0fc 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py +++ b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py @@ -1,8 +1,25 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: cs2_publish_custom_events_to_a_domain_topic.py +DESCRIPTION: + These samples demonstrate creating a list of EventGrid Events and sending them as a list. +USAGE: + python cs2_publish_custom_events_to_a_domain_topic.py + Set the environment variables with your own values before running the sample: + 1) EG_ACCESS_KEY - The access key of your eventgrid account. + 2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". +""" + from azure.eventgrid import EventGridPublisherClient, EventGridEvent from azure.core.credentials import AzureKeyCredential -domain_hostname = ".-1.eventgrid.azure.net" -domain_key = "" +domain_key = os.environ["EG_ACCESS_KEY"] +domain_hostname = os.environ["EG_TOPIC_HOSTNAME"] credential = AzureKeyCredential(domain_key) client = EventGridPublisherClient(domain_hostname, credential) @@ -26,4 +43,4 @@ subject="Door1", data_version="2.0" ) -]) \ No newline at end of file +]) diff --git a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs3_consume_system_events.py b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs3_consume_system_events.py index ef39037148d4..1e325bfc0666 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs3_consume_system_events.py +++ b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs3_consume_system_events.py @@ -1,10 +1,22 @@ +# -------------------------------------------------------------------------- +# 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 from azure.eventgrid import EventGridConsumer consumer = EventGridConsumer() # returns List[DeserializedEvent] -deserialized_events = consumer.deserialize_events(service_bus_received_message) +deserialized_events = consumer.decode_eventgrid_event(service_bus_received_message) # EventGridEvent schema, Storage.BlobCreated event for event in deserialized_events: diff --git a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs4_consume_custom_events.py b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs4_consume_custom_events.py index 13d41d0f09ea..ea2fed0e3976 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs4_consume_custom_events.py +++ b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs4_consume_custom_events.py @@ -1,10 +1,22 @@ +# -------------------------------------------------------------------------- +# 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.deserialize_events(service_bus_received_message) +deserialized_events = consumer.decode_eventgrid_event(service_bus_received_message) # EventGridEvent schema, with custom event type for event in deserialized_events: diff --git a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py index 4474b6ab40fb..674a3851b495 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py +++ b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py @@ -1,8 +1,24 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: cs5_publish_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 cs5_publish_events_using_cloud_events_1.0_schema.py + Set the environment variables with your own values before running the sample: + 1) CLOUD_ACCESS_KEY - The access key of your eventgrid account. + 2) CLOUD_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". +""" from azure.eventgrid import EventGridPublisherClient, CloudEvent from azure.core.credentials import AzureKeyCredential -topic_hostname = ".-1.eventgrid.azure.net" -topic_key = "" +topic_key = os.environ["CLOUD_ACCESS_KEY"] +topic_hostname = os.environ["CLOUD_TOPIC_HOSTNAME"] credential = AzureKeyCredential(topic_key) client = EventGridPublisherClient(topic_hostname, credential) diff --git a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py index 2001070fb68c..886b96d6b50e 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py +++ b/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py @@ -1,10 +1,22 @@ +# -------------------------------------------------------------------------- +# 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 from azure.eventgrid import EventGridConsumer consumer = EventGridConsumer() # returns List[DeserializedEvent] -deserialized_events = consumer.deserialize_events(service_bus_received_message) +deserialized_events = consumer.decode_eventgrid_event(service_bus_received_message) # CloudEvent schema for event in deserialized_events: diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_custom_data_sample.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_custom_data_sample.py index ce477ef69963..9553dce745f6 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_custom_data_sample.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_custom_data_sample.py @@ -1,3 +1,16 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +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 @@ -14,9 +27,9 @@ 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) +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) \ No newline at end of file diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_eg_storage_blob_created_data_sample.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_eg_storage_blob_created_data_sample.py index 21735940739f..5a57a4ce9a38 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_eg_storage_blob_created_data_sample.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_eg_storage_blob_created_data_sample.py @@ -1,3 +1,16 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: consume_eg_storage_blob_created_data_sample.py +DESCRIPTION: + These samples demonstrate consuming custom eventgrid event data. +USAGE: + python consume_eg_storage_blob_created_data_sample.py + Set the environment variables with your own values before running the sample: +""" import json from azure.eventgrid import EventGridConsumer, EventGridEvent, StorageBlobCreatedEventData @@ -28,9 +41,9 @@ 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) +deserialized_dict_event = client.decode_eventgrid_event(eg_storage_dict) +deserialized_str_event = client.decode_eventgrid_event(eg_storage_string) +deserialized_bytes_event = client.decode_eventgrid_event(eg_storage_bytes) print(deserialized_bytes_event.model == deserialized_str_event.model) print(deserialized_bytes_event.model == deserialized_dict_event.model) diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_eventhub.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_eventhub.py index b672777afc0b..556e3a6beb19 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_eventhub.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_eventhub.py @@ -1,17 +1,23 @@ -import sys +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: consume_cloud_events_from_eventhub.py +DESCRIPTION: + These samples demonstrate receiving events from an Event Hub. +USAGE: + python consume_cloud_events_from_eventhub.py + Set the environment variables with your own values before running the sample: + 1) EVENT_HUB_CONN_STR: The connection string to the Event hub account + 3) EVENTHUB_NAME: The name of the eventhub account +""" import os -PACKAGE_PARENT = '..' -SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) -sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) - from azure.eventgrid import EventGridConsumer, CloudEvent, EventGridEvent from azure.eventhub import EventHubConsumerClient -""" -An example to show receiving events from an Event Hub. -""" - CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"] EVENTHUB_NAME = os.environ["EVENTHUB_NAME"] @@ -19,7 +25,7 @@ def on_event(partition_context, event): dict_event = event.body_as_json()[0] - deserialized_event = eg_consumer.deserialize_event(dict_event) + deserialized_event = eg_consumer.decode_eventgrid_event(dict_event) if deserialized_event.model.__class__ == CloudEvent: dict_event = deserialized_event.to_json() print("event.type: {}\n".format(dict_event["type"])) diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_service_bus_queue.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_service_bus_queue.py index 3f1b61057d83..17e902ef0ebf 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_service_bus_queue.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_service_bus_queue.py @@ -1,10 +1,20 @@ -import sys +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: consume_cloud_events_from_eventhub.py +DESCRIPTION: + These samples demonstrate receiving events from Service Bus. +USAGE: + python consume_cloud_events_from_eventhub.py + Set the environment variables with your own values before running the sample: + 1) SB_CONN_STR: The connection string to the Service Bus account + 3) SERVICE_BUS_QUEUE_NAME: The name of the servicebus account +""" import os -PACKAGE_PARENT = '..' -SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) -sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) - from azure.core.pipeline.policies import AzureKeyCredentialPolicy from azure.core.credentials import AzureKeyCredential @@ -23,13 +33,14 @@ 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: + 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)) diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_storage_queue.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_storage_queue.py index 754c561bfbe1..29e6247ca044 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_storage_queue.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_storage_queue.py @@ -1,3 +1,18 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: consume_cloud_events_from_eventhub.py +DESCRIPTION: + These samples demonstrate receiving events from a Storage Queue. +USAGE: + python consume_cloud_events_from_eventhub.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. +""" import os from azure.storage.queue import QueueServiceClient from azure.eventgrid import EventGridConsumer, CloudEvent @@ -13,14 +28,15 @@ 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: + 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)) diff --git a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_cloud_events_to_custom_topic_sample.py b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_cloud_events_to_custom_topic_sample.py index cada0ae9e5e5..c538cd235d73 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_cloud_events_to_custom_topic_sample.py +++ b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_cloud_events_to_custom_topic_sample.py @@ -1,12 +1,24 @@ -import sys +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: publish_cloud_events_to_custom_topic_sample.py +DESCRIPTION: + These samples demonstrate creating a list of CloudEvents and sending them as a list + to a custom topic. +USAGE: + python publish_cloud_events_to_custom_topic_sample.py + Set the environment variables with your own values before running the sample: + 1) CLOUD_ACCESS_KEY - The access key of your eventgrid account. + 2) CLOUD_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". +""" import os from random import randint, sample import time -PACKAGE_PARENT = '..' -SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) -sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) - from azure.core.credentials import AzureKeyCredential from azure.eventgrid import EventGridPublisherClient, CloudEvent @@ -19,21 +31,25 @@ team_members = ["Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren", "Srikanta", "Swathi"] # possible values for data field -# publish events -while True: - event_list = [] # list of events to publish - # create events and append to list - for j in range(randint(1, 1)): - sample_members = sample(team_members, k=randint(1, 9)) # select random subset of team members - data_dict = {"team": sample_members} - event = CloudEvent( - type="Azure.Sdk.Sample", - source="https://egsample.dev/sampleevent", - data={"team": sample_members} - ) - event_list.append(event) +def publish_event(): + # publish events + for _ in range(10): + event_list = [] # list of events to publish + # create events and append to list + for j in range(randint(1, 1)): + sample_members = sample(team_members, k=randint(1, 9)) # select random subset of team members + data_dict = {"team": sample_members} + event = CloudEvent( + type="Azure.Sdk.Sample", + source="https://egsample.dev/sampleevent", + data={"team": sample_members} + ) + event_list.append(event) + + # publish list of events + client.send(event_list) + print("Batch of size {} published".format(len(event_list))) + time.sleep(randint(1, 5)) - # publish list of events - client.send(event_list) - print("Batch of size {} published".format(len(event_list))) - time.sleep(randint(1, 5)) +if __name__ == "__main__": + publish_event() diff --git a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_cloud_events_to_domain_topic_sample.py b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_cloud_events_to_domain_topic_sample.py index 528d57d85887..54b5d23b1c8b 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_cloud_events_to_domain_topic_sample.py +++ b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_cloud_events_to_domain_topic_sample.py @@ -1,12 +1,26 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: publish_cloud_events_to_domain_topic_sample.py +DESCRIPTION: + These samples demonstrate creating a list of CloudEvents and sending them as a list + to a domain topic. +USAGE: + python publish_cloud_events_to_domain_topic_sample.py + Set the environment variables with your own values before running the sample: + 1) DOMAIN_ACCESS_KEY - The access key of your eventgrid account. + 2) DOMAIN_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". + 3) DOMAIN_NAME - the name of the topic +""" import sys import os from random import randint, sample import time -PACKAGE_PARENT = '..' -SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) -sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) - from azure.core.credentials import AzureKeyCredential from azure.eventgrid import EventGridPublisherClient, CloudEvent @@ -14,27 +28,32 @@ domain_topic_hostname = os.environ["DOMAIN_TOPIC_HOSTNAME"] domain_name = os.environ["DOMAIN_NAME"] + # authenticate client credential = AzureKeyCredential(domain_key) client = EventGridPublisherClient(domain_topic_hostname, credential) -# publish events -while True: - - event_list = [] # list of events to publish - team_members = ["Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren", "Srikanta", "Swathi"] # possible values for data field - - # create events and append to list - for j in range(randint(1, 3)): - sample_members = sample(team_members, k=randint(1, 9)) # select random subset of team members - event = CloudEvent( - type="Azure.Sdk.Demo", - source=domain_name, - data={"team": sample_members} - ) - event_list.append(event) - - # publish list of events - client.send(event_list) - print("Batch of size {} published".format(len(event_list))) - time.sleep(randint(1, 5)) +def publish_event(): + # publish events + for _ in range(10): + + event_list = [] # list of events to publish + team_members = ["Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren", "Srikanta", "Swathi"] # possible values for data field + + # create events and append to list + for j in range(randint(1, 3)): + sample_members = sample(team_members, k=randint(1, 9)) # select random subset of team members + event = CloudEvent( + type="Azure.Sdk.Demo", + source=domain_name, + data={"team": sample_members} + ) + event_list.append(event) + + # publish list of events + client.send(event_list) + print("Batch of size {} published".format(len(event_list))) + time.sleep(randint(1, 5)) + +if __name__ == '__main__': + publish_event() diff --git a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_custom_schema_events_to_topic_sample.py b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_custom_schema_events_to_topic_sample.py index ecfbf4eb208d..4396938d8ee1 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_custom_schema_events_to_topic_sample.py +++ b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_custom_schema_events_to_topic_sample.py @@ -1,4 +1,19 @@ -import sys +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: publish_custom_schema_events_to_topic_sample.py +DESCRIPTION: + These samples demonstrate creating a list of Custom Events and sending them as a list. +USAGE: + python publish_custom_schema_events_to_topic_sample.py + Set the environment variables with your own values before running the sample: + 1) CUSTOM_SCHEMA_ACCESS_KEY - The access key of your eventgrid account. + 2) CUSTOM_SCHEMA_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". +""" import os from random import randint, sample import time @@ -6,39 +21,40 @@ from msrest.serialization import UTC import datetime as dt -PACKAGE_PARENT = '..' -SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) -sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) - from azure.core.credentials import AzureKeyCredential from azure.eventgrid import EventGridPublisherClient, CustomEvent key = os.environ["CUSTOM_SCHEMA_ACCESS_KEY"] topic_hostname = os.environ["CUSTOM_SCHEMA_TOPIC_HOSTNAME"] -# authenticate client -credential = AzureKeyCredential(key) -client = EventGridPublisherClient(topic_hostname, credential) - -custom_schema_event = { - "customSubject": "sample", - "customEventType": "sample.event", - "customDataVersion": "2.0", - "customId": uuid.uuid4(), - "customEventTime": dt.datetime.now(UTC()).isoformat(), - "customData": "sample data" -} - -# publish events -while True: - - event_list = [] # list of events to publish - # create events and append to list - for j in range(randint(1, 3)): - event = CustomEvent(custom_schema_event) - event_list.append(event) - - # publish list of events - client.send(event_list) - print("Batch of size {} published".format(len(event_list))) - time.sleep(randint(1, 5)) +def publish_event(): + # authenticate client + credential = AzureKeyCredential(key) + client = EventGridPublisherClient(topic_hostname, credential) + + custom_schema_event = { + "customSubject": "sample", + "customEventType": "sample.event", + "customDataVersion": "2.0", + "customId": uuid.uuid4(), + "customEventTime": dt.datetime.now(UTC()).isoformat(), + "customData": "sample data" + } + + # publish events + for _ in range(10): + + event_list = [] # list of events to publish + # create events and append to list + for j in range(randint(1, 3)): + event = CustomEvent(custom_schema_event) + event_list.append(event) + + # publish list of events + client.send(event_list) + print("Batch of size {} published".format(len(event_list))) + time.sleep(randint(1, 5)) + + +if __name__ == '__main__': + publish_event() diff --git a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_event_grid_events_to_custom_topic_sample.py b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_event_grid_events_to_custom_topic_sample.py index a8379f5cf84a..d10b5ca6e76e 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_event_grid_events_to_custom_topic_sample.py +++ b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_event_grid_events_to_custom_topic_sample.py @@ -1,12 +1,24 @@ -import sys +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: publish_event_grid_events_to_custom_topic_sample.py +DESCRIPTION: + These samples demonstrate creating a list of Eventgrid Events and sending them as a list + to custom topic. +USAGE: + python publish_event_grid_events_to_custom_topic_sample.py + Set the environment variables with your own values before running the sample: + 1) EG_ACCESS_KEY - The access key of your eventgrid account. + 2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". +""" import os from random import randint, sample import time -PACKAGE_PARENT = '..' -SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) -sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) - from azure.core.credentials import AzureKeyCredential from azure.eventgrid import EventGridPublisherClient, EventGridEvent @@ -19,22 +31,26 @@ team_members = ["Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren", "Srikanta", "Swathi"] # possible values for data field -# publish events -while True: - - event_list = [] # list of events to publish - # create events and append to list - for j in range(randint(1, 3)): - sample_members = sample(team_members, k=randint(1, 9)) # select random subset of team members - event = EventGridEvent( - subject="Door1", - data={"team": sample_members}, - event_type="Azure.Sdk.Demo", - data_version="2.0" - ) - event_list.append(event) - - # publish list of events - client.send(event_list) - print("Batch of size {} published".format(len(event_list))) - time.sleep(randint(1, 5)) +def publish_event(): + # publish events + while True: + + event_list = [] # list of events to publish + # create events and append to list + for j in range(randint(1, 3)): + sample_members = sample(team_members, k=randint(1, 9)) # select random subset of team members + event = EventGridEvent( + subject="Door1", + data={"team": sample_members}, + event_type="Azure.Sdk.Demo", + data_version="2.0" + ) + event_list.append(event) + + # publish list of events + client.send(event_list) + print("Batch of size {} published".format(len(event_list))) + time.sleep(randint(1, 5)) + +if __name__ == '__main__': + publish_event() diff --git a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_with_shared_access_signature_sample.py b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_with_shared_access_signature_sample.py index 80ba720ccfd6..ba88862b9629 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_with_shared_access_signature_sample.py +++ b/sdk/eventgrid/azure-eventgrid/samples/publish_samples/publish_with_shared_access_signature_sample.py @@ -1,12 +1,24 @@ -import sys +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +FILE: publish_with_shared_access_signature_sample.py +DESCRIPTION: + These samples demonstrate creating a list of CloudEvents and publish them + using the shared access signature for authentication. +USAGE: + python publish_with_shared_access_signature_sample.py + Set the environment variables with your own values before running the sample: + 1) CLOUD_ACCESS_KEY - The access key of your eventgrid account. + 2) CLOUD_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format + "..eventgrid.azure.net". +""" import os from random import randint, sample import time -PACKAGE_PARENT = '..' -SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) -sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) - from dateutil.tz import tzutc from datetime import timedelta import datetime as dt @@ -25,21 +37,25 @@ team_members = ["Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren", "Srikanta", "Swathi"] # possible values for data field -# publish events -while True: - - event_list = [] # list of events to publish - # create events and append to list - for j in range(randint(1, 3)): - sample_members = sample(team_members, k=randint(1, 9)) # select random subset of team members - event = CloudEvent( - type="Azure.Sdk.Demo", - source="https://egdemo.dev/demowithsignature", - data={"team": sample_members} - ) - event_list.append(event) - - # publish list of events - client.send(event_list) - print("Batch of size {} published".format(len(event_list))) - time.sleep(randint(1, 5)) +def publish_event(): + # publish events + for _ in range(10): + + event_list = [] # list of events to publish + # create events and append to list + for j in range(randint(1, 3)): + sample_members = sample(team_members, k=randint(1, 9)) # select random subset of team members + event = CloudEvent( + type="Azure.Sdk.Demo", + source="https://egdemo.dev/demowithsignature", + data={"team": sample_members} + ) + event_list.append(event) + + # publish list of events + client.send(event_list) + print("Batch of size {} published".format(len(event_list))) + time.sleep(randint(1, 5)) + +if __name__ == '__main__': + publish_event()