From 90166ee9953d1985d59eea8af0047cfb789a5c9e Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 3 Mar 2021 10:43:35 -0800 Subject: [PATCH 1/4] fix samples --- .../consume_cloud_events_from_eventhub.py | 20 +++++-------------- .../EventGridTrigger1/__init__.py | 5 ++--- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py index c831c53f6a16..9f60057fdf6b 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py @@ -15,7 +15,7 @@ """ import os -from azure.eventgrid import CloudEvent, EventGridEvent +from azure.eventgrid import EventGridEvent from azure.eventhub import EventHubConsumerClient CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"] @@ -25,20 +25,10 @@ def on_event(partition_context, event): dict_event = event.body_as_json()[0] - deserialized_event = eg_consumer.deserialize_eventgrid_events(dict_event) - if deserialized_event.model.__class__ == CloudEvent: - 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: - 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)) - -eg_consumer = EventGridDeserializer() + + dict_event = CloudEvent.from_dict(dict_event) + print("data: {}\n".format(deserialized_event.data)) + consumer_client = EventHubConsumerClient.from_connection_string( conn_str=CONNECTION_STR, consumer_group='$Default', diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/functionsapp/EventGridTrigger1/__init__.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/functionsapp/EventGridTrigger1/__init__.py index 36abf8806643..460aa49b171e 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/functionsapp/EventGridTrigger1/__init__.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/functionsapp/EventGridTrigger1/__init__.py @@ -3,7 +3,7 @@ import sys import azure.functions as func -from azure.eventgrid import EventGridDeserializer +from azure.eventgrid import EventGridEvent def main(event: func.EventGridEvent): logging.info(sys.version) @@ -16,7 +16,6 @@ def main(event: func.EventGridEvent): 'event_type': event.event_type }) logging.info(result) - consumer = EventGridDeserializer() - deserialized_event = consumer.deserialize_events(result) + deserialized_event = EventGridEvent.from_dict(json.loads(result)) ## can only be EventGridEvent print("model: {}".format(event.model)) From 2b369e1c618f99dd7a6f9be2e150d0889aef5341 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 3 Mar 2021 10:44:13 -0800 Subject: [PATCH 2/4] wrong import --- .../consume_samples/consume_cloud_events_from_eventhub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py index 9f60057fdf6b..9e0ac7ef6c99 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py @@ -15,7 +15,7 @@ """ import os -from azure.eventgrid import EventGridEvent +from azure.core.messaging import CloudEvent from azure.eventhub import EventHubConsumerClient CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"] From 07ccfb1de94250dc02c4c7b173e0388d913e9fdf Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 3 Mar 2021 10:48:31 -0800 Subject: [PATCH 3/4] dedent in async --- .../azure/eventgrid/aio/_publisher_client_async.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/aio/_publisher_client_async.py b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/aio/_publisher_client_async.py index 5029e9a5f963..b6abc8d3f1a8 100644 --- a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/aio/_publisher_client_async.py +++ b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/aio/_publisher_client_async.py @@ -134,14 +134,14 @@ async def send(self, events: SendType, **kwargs: Any) -> None: :start-after: [START publish_eg_event_dict_async] :end-before: [END publish_eg_event_dict_async] :language: python - :dedent: 0 + :dedent: 4 :caption: Publishing an EventGridEvent using a dict-like representation. .. literalinclude:: ../samples/async_samples/sample_publish_cloud_event_using_dict_async.py :start-after: [START publish_cloud_event_dict_async] :end-before: [END publish_cloud_event_dict_async] :language: python - :dedent: 0 + :dedent: 4 :caption: Publishing a CloudEvent using a dict-like representation. When publishing a Custom Schema Event(s), dict-like representation is accepted. @@ -153,7 +153,7 @@ async def send(self, events: SendType, **kwargs: Any) -> None: :start-after: [START publish_custom_schema_async] :end-before: [END publish_custom_schema_async] :language: python - :dedent: 0 + :dedent: 4 :caption: Publishing a Custom Schema event. **WARNING**: To gain the best performance when sending multiple events at one time, From 3cb6e96817358f7c17dad729869725065bb9a767 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 3 Mar 2021 11:13:15 -0800 Subject: [PATCH 4/4] fix --- .../consume_samples/consume_cloud_events_from_eventhub.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py index 9e0ac7ef6c99..27f56126da72 100644 --- a/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py +++ b/sdk/eventgrid/azure-eventgrid/samples/consume_samples/consume_cloud_events_from_eventhub.py @@ -14,7 +14,7 @@ 3) EVENTHUB_NAME: The name of the eventhub account """ import os - +import json from azure.core.messaging import CloudEvent from azure.eventhub import EventHubConsumerClient @@ -23,10 +23,8 @@ def on_event(partition_context, event): - - dict_event = event.body_as_json()[0] - - dict_event = CloudEvent.from_dict(dict_event) + + dict_event = CloudEvent.from_dict(json.loads(event)[0]) print("data: {}\n".format(deserialized_event.data)) consumer_client = EventHubConsumerClient.from_connection_string(