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

[Event Grid] created async samples + updated samples/README.md #16171

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 6 additions & 6 deletions sdk/eventgrid/azure-eventgrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
[azure_core_ref_docs]: https://aka.ms/azsdk/python/core/docs
[azure_subscription]: https://azure.microsoft.com/free/

[python-eg-sample-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py
Copy link
Contributor

@rakshith91 rakshith91 Jan 15, 2021

Choose a reason for hiding this comment

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

all the references in the readme mus not contain the async versions as well wherever applicable

[sample_name](--link to sync sample--)([async_version](link to corresponding async sample"))

[python-eg-sample-customevent-to-domain]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py
[python-eg-sample-consume-systemevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs3_consume_system_events.py
[python-eg-sample-consume-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs4_consume_custom_events.py
[python-eg-sample-send-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py
[python-eg-sample-consume-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py
[python-eg-sample-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py
[python-eg-sample-customevent-to-domain]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py
[python-eg-sample-consume-systemevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs3_consume_system_events.py
[python-eg-sample-consume-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs4_consume_custom_events.py
[python-eg-sample-send-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py
[python-eg-sample-consume-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py
[publisher-service-doc]: https://docs.microsoft.com/azure/event-grid/concepts

[cla]: https://cla.microsoft.com
Expand Down
6 changes: 3 additions & 3 deletions sdk/eventgrid/azure-eventgrid/migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ cloud_event = {

| In v1.3 | Equivalent in v2.0 | Sample |
|---|---|---|
|`EventGridClient(credentials)`|`EventGridPublisherClient(topic_hostname, credential)`|[Sample for client construction](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py)|
|`EventGridClient(credentials)`|`EventGridPublisherClient(topic_hostname, credential)`|[Sample for client construction](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py)|

* Additionally, we now have an `EventGridConsumer` that should be used to deserialize the events. This class is used only to decode data into a `CloudEvent` or an `EventGridEvent`. Hence, there are no credentials required to construct this as shown below.

Expand All @@ -79,11 +79,11 @@ The `publish_events` API is replaced with `send` in v2.0. Additionally, `send` A

| In v1.3 | Equivalent in v2.0 | Sample |
|---|---|---|
|`EventGridClient(credentials).publish_events(topic_hostname, events)`|`EventGridPublisherClient(topic_hostname, credential).send(events)`|[Sample for client construction](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py)|
|`EventGridClient(credentials).publish_events(topic_hostname, events)`|`EventGridPublisherClient(topic_hostname, credential).send(events)`|[Sample for client construction](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py)|
Copy link
Contributor

Choose a reason for hiding this comment

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

please add the async versions as well here


### Consuming Events

In v2.0, `EventGridConsumer` can be used to decode both Cloud Events and EventGrid Events. Please find the samples [here](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventgrid/azure-eventgrid/samples/consume_samples) to see detailed examples of the consumer.
In v2.0, `EventGridConsumer` can be used to decode both Cloud Events and EventGrid Events. Please find the samples [here](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/consume_samples) to see detailed examples of the consumer.

```Python
EventGridConsumer().decode_cloud_event(cloud_event_dict)
Expand Down
31 changes: 19 additions & 12 deletions sdk/eventgrid/azure-eventgrid/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ urlFragment: azure-eventgrid-samples
# Azure Event Grid Client Library Python Samples

These code samples show common champion scenario operations with the Azure Event Grid client library.
Both [sync version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventhub/azure-eventhub/samples/sync_samples) and [async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventhub/azure-eventhub/samples/async_samples) of publish samples are provided, async samples require Python 3.5 or later. Only the sync versions of consume samples are provided, since the EventGridConsumer does not interact with the service and does not provide asynchronous support.

* Publish Custom Events to a topic: [cs1_publish_custom_events_to_a_topic.py][python-eg-sample-customevent]
* Publish Custom events to a domain topic: [cs2_publish_custom_events_to_a_domain_topic.py][python-eg-sample-customevent-to-domain]
* Deserialize a System Event: [cs3_consume_system_events.py][python-eg-sample-consume-systemevent]
* Deserialize a Custom Event: [cs4_consume_custom_events.py][python-eg-sample-consume-customevent]
* Deserialize a Cloud Event: [cs5_consume_events_using_cloud_events_1.0_schema.py][python-eg-sample-consume-cloudevent]
* Publish a Cloud Event: [cs6_publish_events_using_cloud_events_1.0_schema.py][python-eg-sample-send-cloudevent]
* [cs1_publish_custom_events_to_a_topic.py][python-eg-sample-customevent] ([async_version][python-eg-sample-customevent-async]) - Publishes Event Grid Events to a custom topic.
* [cs1b_publish_custom_events_to_a_topic_with_signature.py][python-eg-sample-customevent-signature] ([async_version][python-eg-sample-customevent-signature-async]) - Publishes Event Grid Events to a custom topic using a shared access signature for authentication.
* [cs2_publish_custom_events_to_a_domain_topic.py][python-eg-sample-customevent-to-domain] ([async_version][python-eg-sample-customevent-to-domain-async]) - Publishes Event Grid Events to domain topics.
* [cs3_consume_system_events.py][python-eg-sample-consume-systemevent] - Deserializes a System Event.
* [cs4_consume_custom_events.py][python-eg-sample-consume-customevent] - Deserializes a custom Event Grid Event.
* [cs5_publish_events_using_cloud_events_1.0_schema.py][python-eg-sample-send-cloudevent] ([async_version][python-eg-sample-send-cloudevent-async]) - Publishes Cloud Events to a custom topic.
* [cs6_consume_events_using_cloud_events_1.0_schema.py][python-eg-sample-consume-cloudevent] - Deserializes a Cloud Event.

[python-eg-sample-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py
[python-eg-sample-customevent-to-domain]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py
[python-eg-sample-consume-systemevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs3_consume_system_events.py
[python-eg-sample-consume-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs4_consume_custom_events.py
[python-eg-sample-send-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py
[python-eg-sample-consume-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py
[python-eg-sample-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py
[python-eg-sample-customevent-async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/async_samples/champion_scenarios/cs1_publish_custom_events_to_a_topic_async.py
[python-eg-sample-customevent-signature]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs1b_publish_custom_events_to_a_topic_with_signature.py
[python-eg-sample-customevent-signature-async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/async_samples/champion_scenarios/cs1b_publish_custom_events_to_a_topic_with_signature_async.py
[python-eg-sample-customevent-to-domain]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py
[python-eg-sample-customevent-to-domain-async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/async_samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic_async.py
[python-eg-sample-consume-systemevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs3_consume_system_events.py
[python-eg-sample-consume-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs4_consume_custom_events.py
[python-eg-sample-send-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema.py
[python-eg-sample-send-cloudevent-async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/async_samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema_async.py
[python-eg-sample-consume-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/sync_samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py
[publisher-service-doc]: https://docs.microsoft.com/azure/event-grid/concepts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# --------------------------------------------------------------------------
# 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_async.py
DESCRIPTION:
These samples demonstrate sending an EventGrid Event asynchronously.
USAGE:
python cs1_publish_custom_events_to_a_topic_async.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".
"""
import os
import asyncio

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

async def publish():
topic_key = os.environ["EG_ACCESS_KEY"]
topic_hostname = os.environ["EG_TOPIC_HOSTNAME"]

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

await client.send([
EventGridEvent(
event_type="Contoso.Items.ItemReceived",
data={
"itemSku": "Contoso Item SKU #1"
},
subject="Door1",
data_version="2.0"
)
])

if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(publish())
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# --------------------------------------------------------------------------
# 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_async.py
DESCRIPTION:
These samples demonstrate sending an EventGrid Event using a shared access signature for authentication asynchronously.
USAGE:
python cs1b_publish_custom_events_to_a_topic_with_signature_async.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".
"""
import os
import asyncio

from azure.eventgrid.aio import EventGridPublisherClient
from azure.eventgrid import EventGridEvent, generate_shared_access_signature, EventGridSharedAccessSignatureCredential
from azure.core.credentials import AzureKeyCredential
from datetime import datetime, timedelta

async def publish():
topic_key = os.environ["EG_ACCESS_KEY"]
topic_hostname = os.environ["EG_TOPIC_HOSTNAME"]
expiration_date_utc = datetime.utcnow() + timedelta(hours=1)

signature = generate_shared_access_signature(topic_hostname, topic_key, expiration_date_utc)
credential = EventGridSharedAccessSignatureCredential(signature)
client = EventGridPublisherClient(topic_hostname, credential)
await client.send([
EventGridEvent(
event_type="Contoso.Items.ItemReceived",
data={
"itemSku": "Contoso Item SKU #1"
},
subject="Door1",
data_version="2.0"
)
])

if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(publish())
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# --------------------------------------------------------------------------
# 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_async.py
DESCRIPTION:
These samples demonstrate creating a list of EventGrid Events and sending them as a list asynchronously.
USAGE:
python cs2_publish_custom_events_to_a_domain_topic_async.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".
"""
import os
import asyncio

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

async def publish():
domain_key = os.environ["EG_DOMAIN_ACCESS_KEY"]
domain_hostname = os.environ["EG_DOMAIN_HOSTNAME"]

credential = AzureKeyCredential(domain_key)
client = EventGridPublisherClient(domain_hostname, credential)

await client.send([
EventGridEvent(
topic="MyCustomDomainTopic1",
event_type="Contoso.Items.ItemReceived",
data={
"itemSku": "Contoso Item SKU #1"
},
subject="Door1",
data_version="2.0"
),
EventGridEvent(
topic="MyCustomDomainTopic2",
event_type="Contoso.Items.ItemReceived",
data={
"itemSku": "Contoso Item SKU #2"
},
subject="Door1",
data_version="2.0"
)
])

if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(publish())
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# --------------------------------------------------------------------------
# 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_async.py
DESCRIPTION:
These samples demonstrate creating a list of CloudEvents and sending them as a list asynchronously.
USAGE:
python cs5_publish_events_using_cloud_events_1.0_schema_async.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".
"""
import os
import asyncio

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

async def publish():
topic_key = os.environ["CLOUD_ACCESS_KEY"]
topic_hostname = os.environ["CLOUD_TOPIC_HOSTNAME"]

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

await client.send([
CloudEvent(
type="Contoso.Items.ItemReceived",
source="/contoso/items",
data={
"itemSku": "Contoso Item SKU #1"
},
subject="Door1"
)
])

if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(publish())
Loading