-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add async samples * changes - 2 * update links * fix samples * add secret * links * fix * more links * more changes * read me link * Apply suggestions from code review Co-authored-by: swathipil <[email protected]> * chagens * delete md file * add more samples * ci * Apply suggestions from code review Co-authored-by: swathipil <[email protected]> Co-authored-by: swathipil <[email protected]>
- Loading branch information
Showing
44 changed files
with
704 additions
and
547 deletions.
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
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
47 changes: 47 additions & 0 deletions
47
...grid/azure-eventgrid/samples/async_samples/sample_publish_cloud_event_using_dict_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,47 @@ | ||
# -------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
""" | ||
FILE: sample_publish_cloud_event_using_dict_async.py | ||
DESCRIPTION: | ||
These samples demonstrate creating a list of CloudEvents using dict representations | ||
and sending then as a list. | ||
USAGE: | ||
python sample_publish_cloud_event_using_dict_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 import CloudEvent | ||
from azure.eventgrid.aio import EventGridPublisherClient | ||
from azure.core.credentials import AzureKeyCredential | ||
|
||
topic_key = os.environ["CLOUD_ACCESS_KEY"] | ||
endpoint = os.environ["CLOUD_TOPIC_HOSTNAME"] | ||
|
||
async def publish(): | ||
credential = AzureKeyCredential(topic_key) | ||
client = EventGridPublisherClient(endpoint, credential) | ||
|
||
async with client: | ||
client.send([ | ||
{ | ||
"type": "Contoso.Items.ItemReceived", | ||
"source": "/contoso/items", | ||
"data": { | ||
"itemSku": "Contoso Item SKU #1" | ||
}, | ||
"subject": "Door1", | ||
"specversion": "1.0", | ||
"id": "randomclouduuid11" | ||
} | ||
]) | ||
|
||
if __name__ == '__main__': | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(publish()) |
62 changes: 62 additions & 0 deletions
62
...id/azure-eventgrid/samples/async_samples/sample_publish_custom_schema_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,62 @@ | ||
# -------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
""" | ||
FILE: sample_publish_custom_schema_to_a_topic_async.py | ||
DESCRIPTION: | ||
These samples demonstrate creating a list of Custom Events and sending them as a list. | ||
USAGE: | ||
python sample_publish_custom_schema_to_a_topic_async.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 | ||
"<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net". | ||
""" | ||
import os | ||
import asyncio | ||
from random import randint, sample | ||
import time | ||
import uuid | ||
from msrest.serialization import UTC | ||
import datetime as dt | ||
|
||
from azure.core.credentials import AzureKeyCredential | ||
from azure.eventgrid.aio import EventGridPublisherClient | ||
|
||
key = os.environ["CUSTOM_SCHEMA_ACCESS_KEY"] | ||
endpoint = os.environ["CUSTOM_SCHEMA_TOPIC_HOSTNAME"] | ||
|
||
async def publish_event(): | ||
# authenticate client | ||
credential = AzureKeyCredential(key) | ||
client = EventGridPublisherClient(endpoint, 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(3): | ||
|
||
event_list = [] # list of events to publish | ||
# create events and append to list | ||
for j in range(randint(1, 3)): | ||
event_list.append(custom_schema_event) | ||
|
||
async with client: | ||
# 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__': | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(publish_event()) |
58 changes: 58 additions & 0 deletions
58
...entgrid/azure-eventgrid/samples/async_samples/sample_publish_eg_event_using_dict_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,58 @@ | ||
# -------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
""" | ||
FILE: sample_publish_eg_event_using_dict_async.py | ||
DESCRIPTION: | ||
These samples demonstrate sending an EventGrid Event as a dict representation | ||
directly to a topic. The dict representation should be that of the serialized | ||
model of EventGridEvent. | ||
USAGE: | ||
python sample_publish_eg_event_using_dict_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 datetime import datetime | ||
from azure.eventgrid import EventGridEvent | ||
from azure.eventgrid.aio import EventGridPublisherClient | ||
from azure.core.credentials import AzureKeyCredential | ||
|
||
topic_key = os.environ["EG_ACCESS_KEY"] | ||
endpoint = os.environ["EG_TOPIC_HOSTNAME"] | ||
|
||
async def publish(): | ||
credential = AzureKeyCredential(topic_key) | ||
client = EventGridPublisherClient(endpoint, credential) | ||
event0 = { | ||
"eventType": "Contoso.Items.ItemReceived", | ||
"data": { | ||
"itemSku": "Contoso Item SKU #1" | ||
}, | ||
"subject": "Door1", | ||
"dataVersion": "2.0", | ||
"id": "randomuuid11", | ||
"eventTime": datetime.utcnow() | ||
} | ||
event1 = { | ||
"eventType": "Contoso.Items.ItemReceived", | ||
"data": { | ||
"itemSku": "Contoso Item SKU #2" | ||
}, | ||
"subject": "Door1", | ||
"dataVersion": "2.0", | ||
"id": "randomuuid12", | ||
"eventTime": datetime.utcnow() | ||
} | ||
|
||
async with client: | ||
await client.send([event0, event1]) | ||
|
||
if __name__ == '__main__': | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(publish()) |
54 changes: 54 additions & 0 deletions
54
...tgrid/azure-eventgrid/samples/async_samples/sample_publish_eg_events_to_a_domain_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: sample_publish_eg_events_to_a_domain_async.py | ||
DESCRIPTION: | ||
These samples demonstrate creating a list of EventGrid Events and sending them as a list to a topic | ||
in a domain. | ||
USAGE: | ||
python sample_publish_eg_events_to_a_domain_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 import EventGridEvent | ||
from azure.eventgrid.aio import EventGridPublisherClient | ||
from azure.core.credentials import AzureKeyCredential | ||
|
||
domain_key = os.environ["EG_DOMAIN_ACCESS_KEY"] | ||
domain_hostname = os.environ["EG_DOMAIN_TOPIC_HOSTNAME"] | ||
|
||
async def publish(): | ||
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()) |
Oops, something went wrong.