Skip to content

Commit

Permalink
[eventgrid] Fix Sample eh (#17064)
Browse files Browse the repository at this point in the history
* fix samples

* wrong import

* dedent in async
  • Loading branch information
Rakshith Bhyravabhotla authored Mar 3, 2021
1 parent 895ce54 commit 528df28
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,19 @@
3) EVENTHUB_NAME: The name of the eventhub account
"""
import os

from azure.eventgrid import CloudEvent, EventGridEvent
import json
from azure.core.messaging import CloudEvent
from azure.eventhub import EventHubConsumerClient

CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
EVENTHUB_NAME = os.environ["EVENTHUB_NAME"]


def on_event(partition_context, event):

dict_event = CloudEvent.from_dict(json.loads(event)[0])
print("data: {}\n".format(deserialized_event.data))

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()
consumer_client = EventHubConsumerClient.from_connection_string(
conn_str=CONNECTION_STR,
consumer_group='$Default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))

0 comments on commit 528df28

Please sign in to comment.