Skip to content

Commit 5174dc6

Browse files
author
Rakshith Bhyravabhotla
authored
Add sample docstrings to eg samples (#13572)
* docstrings * Apply suggestions from code review * few more docstrings * decode * add licesnse
1 parent 0f9fb5b commit 5174dc6

17 files changed

+406
-161
lines changed

sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: cs1_publish_custom_events_to_a_topic.py
8+
DESCRIPTION:
9+
These samples demonstrate sending an EventGrid Event.
10+
USAGE:
11+
python cs1_publish_custom_events_to_a_topic.py
12+
Set the environment variables with your own values before running the sample:
13+
1) EG_ACCESS_KEY - The access key of your eventgrid account.
14+
2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format
15+
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
16+
"""
17+
118
from azure.eventgrid import EventGridPublisherClient, EventGridEvent, CloudEvent
219
from azure.core.credentials import AzureKeyCredential
320

4-
topic_hostname = "<YOUR-TOPIC-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
5-
topic_key = "<YOUR-TOPIC-KEY>"
21+
topic_key = os.environ["EG_ACCESS_KEY"]
22+
topic_hostname = os.environ["EG_TOPIC_HOSTNAME"]
623

724
credential = AzureKeyCredential(topic_key)
825
client = EventGridPublisherClient(topic_hostname, credential)

sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1b_publish_custom_events_to_a_topic_with_signature.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: cs1b_publish_custom_events_to_a_topic_with_signature.py
8+
DESCRIPTION:
9+
These samples demonstrate sending an EventGrid Event using a shared access signature for authentication.
10+
USAGE:
11+
python cs1b_publish_custom_events_to_a_topic_with_signature.py
12+
Set the environment variables with your own values before running the sample:
13+
1) EG_ACCESS_KEY - The access key of your eventgrid account.
14+
2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format
15+
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
16+
"""
17+
118
from azure.eventgrid import EventGridPublisherClient, EventGridEvent, CloudEvent, generate_shared_access_signature, EventGridSharedAccessSignatureCredential
219
from azure.core.credentials import AzureKeyCredential
320

4-
topic_hostname = "<YOUR-TOPIC-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
5-
topic_key = "<YOUR-TOPIC-KEY>"
21+
topic_key = os.environ["EG_ACCESS_KEY"]
22+
topic_hostname = os.environ["EG_TOPIC_HOSTNAME"]
623
expiration_date_utc = dt.datetime.now(tzutc()) + timedelta(hours=1)
724

825
signature = generate_shared_access_signature(topic_hostname, topic_key, expiration_date_utc)

sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: cs2_publish_custom_events_to_a_domain_topic.py
8+
DESCRIPTION:
9+
These samples demonstrate creating a list of EventGrid Events and sending them as a list.
10+
USAGE:
11+
python cs2_publish_custom_events_to_a_domain_topic.py
12+
Set the environment variables with your own values before running the sample:
13+
1) EG_ACCESS_KEY - The access key of your eventgrid account.
14+
2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format
15+
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
16+
"""
17+
118
from azure.eventgrid import EventGridPublisherClient, EventGridEvent
219
from azure.core.credentials import AzureKeyCredential
320

4-
domain_hostname = "<YOUR-DOMAIN-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
5-
domain_key = "<YOUR-DOMAIN-KEY>"
21+
domain_key = os.environ["EG_ACCESS_KEY"]
22+
domain_hostname = os.environ["EG_TOPIC_HOSTNAME"]
623

724
credential = AzureKeyCredential(domain_key)
825
client = EventGridPublisherClient(domain_hostname, credential)
@@ -26,4 +43,4 @@
2643
subject="Door1",
2744
data_version="2.0"
2845
)
29-
])
46+
])

sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs3_consume_system_events.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: cs3_consume_system_events.py
8+
DESCRIPTION:
9+
These samples demonstrate deserializing a message from system event.
10+
USAGE:
11+
python cs3_consume_system_events.py
12+
"""
113
import os
214
from azure.eventgrid import EventGridConsumer
315

416
consumer = EventGridConsumer()
517

618
# returns List[DeserializedEvent]
7-
deserialized_events = consumer.deserialize_events(service_bus_received_message)
19+
deserialized_events = consumer.decode_eventgrid_event(service_bus_received_message)
820

921
# EventGridEvent schema, Storage.BlobCreated event
1022
for event in deserialized_events:

sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs4_consume_custom_events.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: cs4_consume_custom_events.py
8+
DESCRIPTION:
9+
These samples demonstrate deserializing a custom event
10+
USAGE:
11+
python cs4_consume_custom_events.py
12+
"""
113
import os
214
from azure.eventgrid import EventGridConsumer
315

416
consumer = EventGridConsumer()
517

618
# returns List[DeserializedEvent]
7-
deserialized_events = consumer.deserialize_events(service_bus_received_message)
19+
deserialized_events = consumer.decode_eventgrid_event(service_bus_received_message)
820

921
# EventGridEvent schema, with custom event type
1022
for event in deserialized_events:

sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: cs5_publish_events_using_cloud_events_1.0_schema.py
8+
DESCRIPTION:
9+
These samples demonstrate creating a list of CloudEvents and sending then as a list.
10+
USAGE:
11+
python cs5_publish_events_using_cloud_events_1.0_schema.py
12+
Set the environment variables with your own values before running the sample:
13+
1) CLOUD_ACCESS_KEY - The access key of your eventgrid account.
14+
2) CLOUD_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format
15+
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
16+
"""
117
from azure.eventgrid import EventGridPublisherClient, CloudEvent
218
from azure.core.credentials import AzureKeyCredential
319

4-
topic_hostname = "<YOUR-TOPIC-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
5-
topic_key = "<YOUR-TOPIC-KEY>"
20+
topic_key = os.environ["CLOUD_ACCESS_KEY"]
21+
topic_hostname = os.environ["CLOUD_TOPIC_HOSTNAME"]
622

723
credential = AzureKeyCredential(topic_key)
824
client = EventGridPublisherClient(topic_hostname, credential)

sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: cs6_consume_events_using_cloud_events_1.0_schema.py
8+
DESCRIPTION:
9+
These samples demonstrate creating a list of CloudEvents and sending then as a list.
10+
USAGE:
11+
python cs6_consume_events_using_cloud_events_1.0_schema.py
12+
"""
113
import os
214
from azure.eventgrid import EventGridConsumer
315

416
consumer = EventGridConsumer()
517

618
# returns List[DeserializedEvent]
7-
deserialized_events = consumer.deserialize_events(service_bus_received_message)
19+
deserialized_events = consumer.decode_eventgrid_event(service_bus_received_message)
820

921
# CloudEvent schema
1022
for event in deserialized_events:

sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_custom_data_sample.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: consume_cloud_custom_data_sample.py
8+
DESCRIPTION:
9+
These samples demonstrate consuming custom cloud data
10+
USAGE:
11+
python consume_cloud_custom_data_sample.py
12+
Set the environment variables with your own values before running the sample:
13+
"""
114
import json
215
from azure.eventgrid import EventGridConsumer, CloudEvent
316

@@ -14,9 +27,9 @@
1427
cloud_custom_bytes = bytes(cloud_custom_string, "utf-8")
1528

1629
client = EventGridConsumer()
17-
deserialized_dict_event = client.deserialize_event(cloud_custom_dict)
18-
deserialized_str_event = client.deserialize_event(cloud_custom_string)
19-
deserialized_bytes_event = client.deserialize_event(cloud_custom_bytes)
30+
deserialized_dict_event = client.decode_cloud_event(cloud_custom_dict)
31+
deserialized_str_event = client.decode_cloud_event(cloud_custom_string)
32+
deserialized_bytes_event = client.decode_cloud_event(cloud_custom_bytes)
2033

2134
print(deserialized_bytes_event.model == deserialized_str_event.model)
2235
print(deserialized_bytes_event.model == deserialized_dict_event.model)

sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_eg_storage_blob_created_data_sample.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: consume_eg_storage_blob_created_data_sample.py
8+
DESCRIPTION:
9+
These samples demonstrate consuming custom eventgrid event data.
10+
USAGE:
11+
python consume_eg_storage_blob_created_data_sample.py
12+
Set the environment variables with your own values before running the sample:
13+
"""
114
import json
215
from azure.eventgrid import EventGridConsumer, EventGridEvent, StorageBlobCreatedEventData
316

@@ -28,9 +41,9 @@
2841
eg_storage_bytes = bytes(eg_storage_string, "utf-8")
2942

3043
client = EventGridConsumer()
31-
deserialized_dict_event = client.deserialize_event(eg_storage_dict)
32-
deserialized_str_event = client.deserialize_event(eg_storage_string)
33-
deserialized_bytes_event = client.deserialize_event(eg_storage_bytes)
44+
deserialized_dict_event = client.decode_eventgrid_event(eg_storage_dict)
45+
deserialized_str_event = client.decode_eventgrid_event(eg_storage_string)
46+
deserialized_bytes_event = client.decode_eventgrid_event(eg_storage_bytes)
3447

3548
print(deserialized_bytes_event.model == deserialized_str_event.model)
3649
print(deserialized_bytes_event.model == deserialized_dict_event.model)

sdk/eventgrid/azure-eventgrid/samples/consume_samples/e2e_samples/consume_cloud_events_from_eventhub.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
import sys
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""
7+
FILE: consume_cloud_events_from_eventhub.py
8+
DESCRIPTION:
9+
These samples demonstrate receiving events from an Event Hub.
10+
USAGE:
11+
python consume_cloud_events_from_eventhub.py
12+
Set the environment variables with your own values before running the sample:
13+
1) EVENT_HUB_CONN_STR: The connection string to the Event hub account
14+
3) EVENTHUB_NAME: The name of the eventhub account
15+
"""
216
import os
317

4-
PACKAGE_PARENT = '..'
5-
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
6-
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
7-
818
from azure.eventgrid import EventGridConsumer, CloudEvent, EventGridEvent
919
from azure.eventhub import EventHubConsumerClient
1020

11-
"""
12-
An example to show receiving events from an Event Hub.
13-
"""
14-
1521
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
1622
EVENTHUB_NAME = os.environ["EVENTHUB_NAME"]
1723

1824

1925
def on_event(partition_context, event):
2026

2127
dict_event = event.body_as_json()[0]
22-
deserialized_event = eg_consumer.deserialize_event(dict_event)
28+
deserialized_event = eg_consumer.decode_eventgrid_event(dict_event)
2329
if deserialized_event.model.__class__ == CloudEvent:
2430
dict_event = deserialized_event.to_json()
2531
print("event.type: {}\n".format(dict_event["type"]))

0 commit comments

Comments
 (0)