-
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.
- Loading branch information
1 parent
fb0b895
commit 8587728
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...e-eventgrid/samples/sync_samples/eventgrid_client_samples/sample_publish_receive_renew.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,43 @@ | ||
# -------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
import os | ||
from azure.core.credentials import AzureKeyCredential | ||
from azure.eventgrid import EventGridClient | ||
from azure.eventgrid.models import * | ||
from azure.core.messaging import CloudEvent | ||
from azure.core.exceptions import HttpResponseError | ||
|
||
|
||
EVENTGRID_KEY: str = os.environ["EVENTGRID_KEY"] | ||
EVENTGRID_ENDPOINT: str = os.environ["EVENTGRID_ENDPOINT"] | ||
TOPIC_NAME: str = os.environ["EVENTGRID_TOPIC_NAME"] | ||
EVENT_SUBSCRIPTION_NAME: str = os.environ["EVENTGRID_EVENT_SUBSCRIPTION_NAME"] | ||
|
||
# Create a client | ||
client = EventGridClient(EVENTGRID_ENDPOINT, AzureKeyCredential(EVENTGRID_KEY)) | ||
|
||
|
||
# Publish a CloudEvent | ||
try: | ||
cloud_event = CloudEvent(data="hello", source="https://example.com", type="example") | ||
client.publish_cloud_events(topic_name=TOPIC_NAME, body=cloud_event) | ||
|
||
receive_result = client.receive_cloud_events(topic_name=TOPIC_NAME, event_subscription_name=EVENT_SUBSCRIPTION_NAME, max_events=10, max_wait_time=10) | ||
lock_tokens_to_release = [] | ||
for item in receive_result.value: | ||
lock_tokens_to_release.append(item.broker_properties.lock_token) | ||
|
||
|
||
# Renew a lock token | ||
lock_tokens = RenewLockOptions(lock_tokens=lock_tokens_to_release) | ||
renew_events = client.renew_cloud_event_locks( | ||
topic_name=TOPIC_NAME, | ||
event_subscription_name=EVENT_SUBSCRIPTION_NAME, | ||
renew_lock_options=lock_tokens, | ||
) | ||
print("Renewed Event:", renew_events) | ||
except HttpResponseError: | ||
raise |