Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample docstrings to eg samples #13572

Merged
merged 5 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
KieranBrantnerMagee marked this conversation as resolved.
Show resolved Hide resolved
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
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
"""

from azure.eventgrid import EventGridPublisherClient, EventGridEvent, CloudEvent
from azure.core.credentials import AzureKeyCredential

topic_hostname = "<YOUR-TOPIC-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
topic_key = "<YOUR-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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we specify these for some but not for others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah - consumer samples don't have any env variables - these are file specific

2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
"""

from azure.eventgrid import EventGridPublisherClient, EventGridEvent, CloudEvent, generate_shared_access_signature, EventGridSharedAccessSignatureCredential
from azure.core.credentials import AzureKeyCredential

topic_hostname = "<YOUR-TOPIC-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
topic_key = "<YOUR-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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
"""

from azure.eventgrid import EventGridPublisherClient, EventGridEvent
from azure.core.credentials import AzureKeyCredential

domain_hostname = "<YOUR-DOMAIN-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
domain_key = "<YOUR-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)
Expand All @@ -26,4 +43,4 @@
subject="Door1",
data_version="2.0"
)
])
])
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
"""
from azure.eventgrid import EventGridPublisherClient, CloudEvent
from azure.core.credentials import AzureKeyCredential

topic_hostname = "<YOUR-TOPIC-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
topic_key = "<YOUR-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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
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"]


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"]))
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
Expand Down
Loading