Skip to content

Commit

Permalink
Naming feedback - part 1 (#16208)
Browse files Browse the repository at this point in the history
* EventGridConsumer

* decodes
  • Loading branch information
Rakshith Bhyravabhotla authored Jan 16, 2021
1 parent b3960b1 commit 989edaf
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 77 deletions.
3 changes: 3 additions & 0 deletions sdk/eventgrid/azure-eventgrid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

**Breaking Changes**
- `EventGridSharedAccessSignatureCredential` is deprecated in favor of `AzureSasCredential`.
- `EventGridConsumer` is now renamed to `EventGridDeserializer`.
- `decode_cloud_event` is renamed to `deserialize_cloud_events`.
- `decode_eventgrid_event` is renamed to `deserialize_eventgrid_events`.

**Bug Fixes**
- `EventGridEvent` has two additional required positional parameters namely, `data` and `data_version`.
Expand Down
16 changes: 8 additions & 8 deletions sdk/eventgrid/azure-eventgrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ Information about the key concepts on Event Grid, see [Concepts in Azure Event G
`EventGridPublisherClient` provides operations to send event data to topic hostname specified during client initialization.
Either a list or a single instance of CloudEvent/EventGridEvent/CustomEvent can be sent.

### EventGridConsumer
`EventGridConsumer` is used to desrialize an event received.
### EventGridDeserializer
`EventGridDeserializer` is used to desrialize an event received.

## Examples

Expand Down Expand Up @@ -129,9 +129,9 @@ This example demonstrates consuming and deserializing an eventgrid event.

```Python
import os
from azure.eventgrid import EventGridConsumer
from azure.eventgrid import EventGridDeserializer

consumer = EventGridConsumer()
consumer = EventGridDeserializer()

eg_storage_dict = {
"id":"bbab625-dc56-4b22-abeb-afcc72e5290c",
Expand All @@ -146,7 +146,7 @@ eg_storage_dict = {
"topic":"/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.EventGrid/topics/eventgridegsub"
}

deserialized_event = consumer.decode_eventgrid_event(eg_storage_dict)
deserialized_event = consumer.deserialize_eventgrid_events(eg_storage_dict)

# both allow access to raw properties as strings
time_string = deserialized_event.event_time
Expand All @@ -158,9 +158,9 @@ This example demonstrates consuming and deserializing a cloud event.

```Python
import os
from azure.eventgrid import EventGridConsumer
from azure.eventgrid import EventGridDeserializer

consumer = EventGridConsumer()
consumer = EventGridDeserializer()

cloud_storage_dict = {
"id":"a0517898-9fa4-4e70-b4a3-afda1dd68672",
Expand All @@ -173,7 +173,7 @@ cloud_storage_dict = {
"specversion":"1.0"
}

deserialized_event = consumer.decode_cloud_event(cloud_storage_dict)
deserialized_event = consumer.deserialize_cloud_events(cloud_storage_dict)

# both allow access to raw properties as strings
time_string = deserialized_event.time
Expand Down
4 changes: 2 additions & 2 deletions sdk/eventgrid/azure-eventgrid/azure/eventgrid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# --------------------------------------------------------------------------

from ._publisher_client import EventGridPublisherClient
from ._consumer import EventGridConsumer
from ._consumer import EventGridDeserializer
from ._helpers import generate_shared_access_signature
from ._models import CloudEvent, CustomEvent, EventGridEvent
from ._version import VERSION

__all__ = ['EventGridPublisherClient', 'EventGridConsumer',
__all__ = ['EventGridPublisherClient', 'EventGridDeserializer',
'CloudEvent', 'CustomEvent', 'EventGridEvent', 'generate_shared_access_signature'
]
__version__ = VERSION
6 changes: 3 additions & 3 deletions sdk/eventgrid/azure-eventgrid/azure/eventgrid/_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

_LOGGER = logging.getLogger(__name__)

class EventGridConsumer(object):
class EventGridDeserializer(object):
"""
A consumer responsible for deserializing event handler messages, to allow for
access to strongly typed Event objects.
"""
def decode_cloud_event(self, cloud_event, **kwargs): # pylint: disable=no-self-use
def deserialize_cloud_events(self, cloud_event, **kwargs): # pylint: disable=no-self-use
# type: (Union[str, dict, bytes], Any) -> CloudEvent
"""Single event following CloudEvent schema will be parsed and returned as Deserialized Event.
:param cloud_event: The event to be deserialized.
Expand All @@ -44,7 +44,7 @@ def decode_cloud_event(self, cloud_event, **kwargs): # pylint: disable=no-self-u
raise ValueError('Error: cannot deserialize event. Event does not have a valid format. \
Event must be a string, dict, or bytes following the CloudEvent schema.')

def decode_eventgrid_event(self, eventgrid_event, **kwargs): # pylint: disable=no-self-use
def deserialize_eventgrid_events(self, eventgrid_event, **kwargs): # pylint: disable=no-self-use
# type: (Union[str, dict, bytes], Any) -> EventGridEvent
"""Single event following EventGridEvent schema will be parsed and returned as Deserialized Event.
:param eventgrid_event: The event to be deserialized.
Expand Down
12 changes: 6 additions & 6 deletions sdk/eventgrid/azure-eventgrid/migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ cloud_event = {
|---|---|---|
|`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)|

* 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.
* Additionally, we now have an `EventGridDeserializer` 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.

```Python
from azure.eventgrid import EventGridConsumer
from azure.eventgrid import EventGridDeserializer

eg_consumer = EventGridConsumer()
eg_consumer = EventGridDeserializer()
```

### Publishing Events
Expand All @@ -83,12 +83,12 @@ The `publish_events` API is replaced with `send` in v2.0. Additionally, `send` A

### 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, `EventGridDeserializer` 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.

```Python
EventGridConsumer().decode_cloud_event(cloud_event_dict)
EventGridDeserializer().deserialize_cloud_events(cloud_event_dict)

EventGridConsumer().decode_eventgrid_event(eventgrid_event_dict)
EventGridDeserializer().deserialize_eventgrid_events(eventgrid_event_dict)
```

## Additional samples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"""
import os
import json
from azure.eventgrid import EventGridConsumer
from azure.eventgrid import EventGridDeserializer

consumer = EventGridConsumer()
consumer = EventGridDeserializer()
path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./cs3_system_event.json"))

with open(path, 'r') as f:
eg_event_received_message = json.loads(f.read())
# returns List[DeserializedEvent]
event = consumer.decode_eventgrid_event(eg_event_received_message)
event = consumer.deserialize_eventgrid_events(eg_event_received_message)

datetime_object = event.event_time
print(datetime_object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
"""
import os
import json
from azure.eventgrid import EventGridConsumer
from azure.eventgrid import EventGridDeserializer

consumer = EventGridConsumer()
consumer = EventGridDeserializer()
path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./cs4_event_grid_event_custom_event.json"))

with open(path, 'r') as f:
eg_event_received_message = json.loads(f.read())

# returns List[DeserializedEvent]
event = consumer.decode_eventgrid_event(eg_event_received_message)
event = consumer.deserialize_eventgrid_events(eg_event_received_message)

# returns { "itemSku": "Contoso Item SKU #1" }
data_dict = event.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
"""
import os
import json
from azure.eventgrid import EventGridConsumer
from azure.eventgrid import EventGridDeserializer

consumer = EventGridConsumer()
consumer = EventGridDeserializer()
path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./cs6_cloud_event_system_event.json"))

with open(path, 'r') as f:
cloud_event_received_message = json.loads(f.read())

# returns List[DeserializedEvent]
event = consumer.decode_cloud_event(cloud_event_received_message)
event = consumer.deserialize_cloud_events(cloud_event_received_message)

datetime_object = event.time
print(datetime_object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Set the environment variables with your own values before running the sample:
"""
import json
from azure.eventgrid import EventGridConsumer, CloudEvent
from azure.eventgrid import EventGridDeserializer, CloudEvent

# all types of CloudEvents below produce same DeserializedEvent
cloud_custom_dict = {
Expand All @@ -26,12 +26,12 @@
cloud_custom_string = json.dumps(cloud_custom_dict)
cloud_custom_bytes = str(cloud_custom_string).encode("utf-8")

client = EventGridConsumer()
deserialized_dict_event = client.decode_cloud_event(cloud_custom_dict)
client = EventGridDeserializer()
deserialized_dict_event = client.deserialize_cloud_events(cloud_custom_dict)
print(deserialized_dict_event)

deserialized_str_event = client.decode_cloud_event(cloud_custom_string)
deserialized_str_event = client.deserialize_cloud_events(cloud_custom_string)
print(deserialized_str_event)

deserialized_bytes_event = client.decode_cloud_event(cloud_custom_bytes)
deserialized_bytes_event = client.deserialize_cloud_events(cloud_custom_bytes)
print(deserialized_bytes_event)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Set the environment variables with your own values before running the sample:
"""
import json
from azure.eventgrid import EventGridConsumer, EventGridEvent
from azure.eventgrid import EventGridDeserializer, EventGridEvent
from azure.eventgrid.models import StorageBlobCreatedEventData

# all types of EventGridEvents below produce same DeserializedEvent
Expand Down Expand Up @@ -41,12 +41,12 @@
eg_storage_string = json.dumps(eg_storage_dict)
eg_storage_bytes = str(eg_storage_string).encode("utf-8")

client = EventGridConsumer()
deserialized_dict_event = client.decode_eventgrid_event(eg_storage_dict)
client = EventGridDeserializer()
deserialized_dict_event = client.deserialize_eventgrid_events(eg_storage_dict)
print(deserialized_dict_event)

deserialized_str_event = client.decode_eventgrid_event(eg_storage_string)
deserialized_str_event = client.deserialize_eventgrid_events(eg_storage_string)
print(deserialized_str_event)

deserialized_bytes_event = client.decode_eventgrid_event(eg_storage_bytes)
deserialized_bytes_event = client.deserialize_eventgrid_events(eg_storage_bytes)
print(deserialized_bytes_event)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""
import os

from azure.eventgrid import EventGridConsumer, CloudEvent, EventGridEvent
from azure.eventgrid import EventGridDeserializer, CloudEvent, EventGridEvent
from azure.eventhub import EventHubConsumerClient

CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
Expand All @@ -25,7 +25,7 @@
def on_event(partition_context, event):

dict_event = event.body_as_json()[0]
deserialized_event = eg_consumer.decode_eventgrid_event(dict_event)
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"]))
Expand All @@ -38,7 +38,7 @@ def on_event(partition_context, event):
print("model: {}\n".format(deserialized_event.model))
print("model.data: {}\n".format(deserialized_event.model.data))

eg_consumer = EventGridConsumer()
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 @@ -18,14 +18,14 @@
from azure.core.pipeline.policies import AzureKeyCredentialPolicy
from azure.core.credentials import AzureKeyCredential

from azure.eventgrid import EventGridConsumer, CloudEvent
from azure.eventgrid import EventGridDeserializer, CloudEvent
from azure.servicebus import ServiceBusClient

connection_str = os.environ['SB_CONN_STR']
queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']

sb_client = ServiceBusClient.from_connection_string(connection_str)
consumer = EventGridConsumer()
consumer = EventGridDeserializer()
with sb_client:
receiver = sb_client.get_queue_receiver(queue_name, prefetch=10)
with receiver:
Expand All @@ -34,13 +34,13 @@
for msg in msgs:
# receive single dict message
if 'specversion' in msg:
deserialized_event = consumer.decode_cloud_event(str(msg))
deserialized_event = consumer.deserialize_cloud_events(str(msg))
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))
else:
deserialized_event = consumer.decode_eventgrid_event(str(msg))
deserialized_event = consumer.deserialize_eventgrid_events(str(msg))
dict_event = deserialized_event.to_json()
print("event.to_json(): {}\n".format(dict_event))
print("model: {}\n".format(deserialized_event.model))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@
"""
import os
from azure.storage.queue import QueueServiceClient
from azure.eventgrid import EventGridConsumer, CloudEvent
from azure.eventgrid import EventGridDeserializer, CloudEvent
from base64 import b64decode

connection_str = os.environ["STORAGE_QUEUE_CONN_STR"]
queue_name = os.environ["STORAGE_QUEUE_NAME"]
queue_service = QueueServiceClient.from_connection_string(conn_str=connection_str)

queue_client = queue_service.get_queue_client(queue_name)
consumer = EventGridConsumer()
consumer = EventGridDeserializer()

msgs = queue_client.receive_messages()
for msg in msgs:
# receive single dict message
if 'specversion' in msg:
deserialized_event = consumer.decode_cloud_event(b64decode(msg.content))
deserialized_event = consumer.deserialize_cloud_events(b64decode(msg.content))
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:
deserialized_event = consumer.decode_eventgrid_event(b64decode(msg.content))
deserialized_event = consumer.deserialize_eventgrid_events(b64decode(msg.content))
dict_event = deserialized_event.to_json()
print("event.to_json(): {}\n".format(dict_event))
print("model: {}\n".format(deserialized_event.model))
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 EventGridConsumer
from azure.eventgrid import EventGridDeserializer

def main(event: func.EventGridEvent):
logging.info(sys.version)
Expand All @@ -16,7 +16,7 @@ def main(event: func.EventGridEvent):
'event_type': event.event_type
})
logging.info(result)
consumer = EventGridConsumer()
consumer = EventGridDeserializer()
deserialized_event = consumer.deserialize_events(result)
## can only be EventGridEvent
print("model: {}".format(event.model))
Loading

0 comments on commit 989edaf

Please sign in to comment.