-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
|
@@ -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)| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...id/samples/async_samples/champion_scenarios/cs1_publish_custom_events_to_a_topic_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
46 changes: 46 additions & 0 deletions
46
..._samples/champion_scenarios/cs1b_publish_custom_events_to_a_topic_with_signature_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
54 changes: 54 additions & 0 deletions
54
...les/async_samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
44 changes: 44 additions & 0 deletions
44
...sync_samples/champion_scenarios/cs5_publish_events_using_cloud_events_1.0_schema_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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