Skip to content

Commit a9956d2

Browse files
author
Rakshith Bhyravabhotla
authored
[EventGrid] Read me + improve docstrings (#13484)
* Read me + docstrings * Update sdk/eventgrid/azure-eventgrid/CHANGELOG.md * comments * comment * one more * add one more line * nti
1 parent 8f8574e commit a9956d2

File tree

5 files changed

+249
-22
lines changed

5 files changed

+249
-22
lines changed

sdk/eventgrid/azure-eventgrid/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Release History
22

3-
## 2.0.0b1 (Unreleased)
3+
## 2.0.0b1 (2020-09-08)
44

5-
- Placeholder - NEEDS TO BE CHANGED
5+
**Features**
6+
- Version (2.0.0b1) is the first preview of our efforts to create a user-friendly and Pythonic client library for Azure EventGrid.
7+
For more information about this, and preview releases of other Azure SDK libraries, please visit https://azure.github.io/azure-sdk/releases/latest/python.html.
8+
- Implements the `EventGridPublisherClient` for the publish flow for EventGrid Events, CloudEvents and CustomEvents.
9+
- Implements the `EventGridConsumer` for the consume flow of the events.
610

711
## 1.3.0 (2019-05-20)
812

Lines changed: 235 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,244 @@
1-
# Microsoft Azure SDK for Python
1+
# Azure EventGrid client library for Python
22

3-
This is the Microsoft Azure Event Grid Client Library.
4-
This package has been tested with Python 2.7, 3.5, 3.6, 3.7 and 3.8.
5-
For a more complete view of Azure libraries, see the [azure sdk python release](https://aka.ms/azsdk/python/all).
3+
Azure Event Grid is a fully-managed intelligent event routing service that allows for uniform event consumption using a publish-subscribe model.
64

5+
[Source code][python-eg-src] | [Package (PyPI)][python-eg-pypi] | [API reference documentation][python-eg-ref-docs]| [Product documentation][python-eg-product-docs] | [Samples][python-eg-samples]| [Changelog][python-eg-changelog]
76

8-
# Usage
7+
## Getting started
98

10-
For code examples, see [Event Grid](https://docs.microsoft.com/python/api/overview/azure/event-grid)
11-
on docs.microsoft.com.
9+
### Prerequisites
10+
* Python 2.7, or 3.5 or later is required to use this package.
11+
* You must have an [Azure subscription][azure_subscription] and an EventGrid Topic resource to use this package.
1212

13+
### Install the package
14+
Install the Azure EventGrid client library for Python with [pip][pip]:
1315

14-
# Provide Feedback
16+
```bash
17+
pip install azure-eventgrid
18+
```
1519

16-
If you encounter any bugs or have suggestions, please file an issue in the
17-
[Issues](https://github.com/Azure/azure-sdk-for-python/issues)
18-
section of the project.
20+
#### Create an EventGrid Topic
21+
You can create the resource using [Azure Portal][azure_portal_create_EG_resource]
1922

23+
### Authenticate the client
24+
In order to interact with the Eventgrid service, you will need to create an instance of a client.
25+
A **topic_hostname** and **credential** are necessary to instantiate the client object.
2026

21-
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-python%2Fazure-eventgrid%2FREADME.png)
27+
#### Looking up the endpoint
28+
You can find the endpoint and the hostname on the Azure portal.
29+
30+
#### Create the client with AzureKeyCredential
31+
32+
To use an API key as the `credential` parameter,
33+
pass the key as a string into an instance of [AzureKeyCredential][azure-key-credential].
34+
35+
```python
36+
from azure.core.credentials import AzureKeyCredential
37+
from azure.eventgrid import EventGridPublisherClient
38+
39+
topic_hostname = "https://<name>.<region>.eventgrid.azure.net"
40+
credential = AzureKeyCredential("<api_key>")
41+
eg_publisher_client = EventGridPublisherClient(topic_hostname, credential)
42+
```
43+
44+
## Key concepts
45+
46+
Information about the key concepts on EventGrid, see [Concepts in Azure Event Grid][publisher-service-doc]
47+
48+
### EventGridPublisherClient
49+
`EventGridPublisherClient` provides operations to send event data to topic hostname specified during client initialization.
50+
Either a list or a single instance of CloudEvent/EventGridEvent/CustomEvent can be sent.
51+
52+
### EventGridConsumer
53+
`EventGridConsumer` is used to desrialize an event received.
54+
55+
## Examples
56+
57+
The following sections provide several code snippets covering some of the most common EventGrid tasks, including:
58+
59+
* [Send an EventGrid Event](#send-an-eventgrid-event)
60+
* [Send a Cloud Event](#send-a-cloud-event)
61+
* [Consume an eventgrid Event](#consume-an-eventgrid-event)
62+
* [Consume a cloud Event](#consume-a-cloud-event)
63+
64+
### Send an EventGrid Event
65+
66+
This example publishes an EventGrid event.
67+
68+
```Python
69+
import os
70+
from azure.core.credentials import AzureKeyCredential
71+
from azure.eventgrid import EventGridPublisherClient, EventGridEvent
72+
73+
key = os.environ["EG_ACCESS_KEY"]
74+
topic_hostname = os.environ["EG_TOPIC_HOSTNAME"]
75+
76+
event = EventGridEvent(
77+
subject="Door1",
78+
data={"team": "azure-sdk"},
79+
event_type="Azure.Sdk.Demo",
80+
data_version="2.0"
81+
)
82+
83+
credential = AzureKeyCredential(key)
84+
client = EventGridPublisherClient(topic_hostname, credential)
85+
86+
client.send(event)
87+
```
88+
89+
### Send a Cloud Event
90+
91+
This example publishes a Cloud event.
92+
93+
```Python
94+
import os
95+
from azure.core.credentials import AzureKeyCredential
96+
from azure.eventgrid import EventGridPublisherClient, CloudEvent
97+
98+
key = os.environ["CLOUD_ACCESS_KEY"]
99+
topic_hostname = os.environ["CLOUD_TOPIC_HOSTNAME"]
100+
101+
event = CloudEvent(
102+
type="Azure.Sdk.Sample",
103+
source="https://egsample.dev/sampleevent",
104+
data={"team": "azure-sdk"}
105+
)
106+
107+
credential = AzureKeyCredential(key)
108+
client = EventGridPublisherClient(topic_hostname, credential)
109+
110+
client.send(event)
111+
```
112+
113+
### Consume an EventGrid Event
114+
115+
This example demonstrates consuming and deserializing an eventgrid event.
116+
117+
```Python
118+
import os
119+
from azure.eventgrid import EventGridConsumer
120+
121+
consumer = EventGridConsumer()
122+
123+
eg_storage_dict = {
124+
"id":"bbab625-dc56-4b22-abeb-afcc72e5290c",
125+
"subject":"/blobServices/default/containers/oc2d2817345i200097container/blobs/oc2d2817345i20002296blob",
126+
"data":{
127+
"api":"PutBlockList",
128+
},
129+
"eventType":"Microsoft.Storage.BlobCreated",
130+
"dataVersion":"2.0",
131+
"metadataVersion":"1",
132+
"eventTime":"2020-08-07T02:28:23.867525Z",
133+
"topic":"/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.EventGrid/topics/eventgridegsub"
134+
}
135+
136+
deserialized_event = consumer.decode_eventgrid_event(eg_storage_dict)
137+
138+
# both allow access to raw properties as strings
139+
time_string = deserialized_event.time
140+
time_string = deserialized_event["time"]
141+
```
142+
143+
### Consume a Cloud Event
144+
145+
This example demonstrates consuming and deserializing a cloud event.
146+
147+
```Python
148+
import os
149+
from azure.eventgrid import EventGridConsumer
150+
151+
consumer = EventGridConsumer()
152+
153+
cloud_storage_dict = {
154+
"id":"a0517898-9fa4-4e70-b4a3-afda1dd68672",
155+
"source":"/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-account}",
156+
"data":{
157+
"api":"PutBlockList",
158+
},
159+
"type":"Microsoft.Storage.BlobCreated",
160+
"time":"2020-08-07T01:11:49.765846Z",
161+
"specversion":"1.0"
162+
}
163+
164+
deserialized_event = consumer.decode_cloud_event(cloud_storage_dict)
165+
166+
# both allow access to raw properties as strings
167+
time_string = deserialized_event.time
168+
time_string = deserialized_event["time"]
169+
```
170+
171+
## Troubleshooting
172+
173+
- Enable `azure.eventgrid` logger to collect traces from the library.
174+
175+
### General
176+
Eventgrid client library will raise exceptions defined in [Azure Core][azure_core_exceptions].
177+
178+
### Logging
179+
This library uses the standard
180+
[logging][python_logging] library for logging.
181+
Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO
182+
level.
183+
184+
### Optional Configuration
185+
186+
Optional keyword arguments can be passed in at the client and per-operation level.
187+
The azure-core [reference documentation][azure_core_ref_docs]
188+
describes available configurations for retries, logging, transport protocols, and more.
189+
190+
## Next steps
191+
192+
The following section provides several code snippets illustrating common patterns used in the EventGrid Python API.
193+
194+
### More sample code
195+
196+
These code samples show common champion scenario operations with the Azure Eventgrid client library.
197+
198+
* Publish Custom Events to a topic: [cs1_publish_custom_events_to_a_topic.py][python-eg-sample-customevent]
199+
* Publish Custom events to a domain topic: [cs2_publish_custom_events_to_a_domain_topic.py][python-eg-sample-customevent-to-domain]
200+
* Deserialize a System Event: [cs3_consume_system_events.py][python-eg-sample-consume-systemevent]
201+
* Deserialize a Custom Event: [cs4_consume_custom_events.py][python-eg-sample-consume-customevent]
202+
* Deserialize a Cloud Event: [cs5_consume_events_using_cloud_events_1.0_schema.py][python-eg-sample-consume-cloudevent]
203+
* Publish a Cloud Event: [cs6_publish_events_using_cloud_events_1.0_schema.py][python-eg-sample-send-cloudevent]
204+
205+
More samples can be found [here][python-eg-samples].
206+
207+
### Additional documentation
208+
209+
For more extensive documentation on Azure EventGrid, see the [Eventgrid documentation][python-eg-product-docs] on docs.microsoft.com.
210+
211+
## Contributing
212+
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit [cla.microsoft.com][cla].
213+
214+
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
215+
216+
This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [[email protected]][coc_contact] with any additional questions or comments.
217+
218+
<!-- LINKS -->
219+
220+
[python-eg-src]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/
221+
[python-eg-pypi]: https://pypi.org/project/azure-eventgrid
222+
[python-eg-product-docs]: https://docs.microsoft.com/en-us/azure/event-grid/overview
223+
[python-eg-ref-docs]: https://aka.ms/azsdk/python/eventgrid/docs
224+
[python-eg-samples]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventgrid/azure-eventgrid/samples
225+
[python-eg-changelog]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventgrid/azure-eventgrid/CHANGELOG.md
226+
227+
[azure_portal_create_EG_resource]: https://ms.portal.azure.com/#blade/HubsExtension/BrowseResource/resourceType/Microsoft.EventGrid%2Ftopics
228+
[azure-key-credential]: https://aka.ms/azsdk/python/core/azurekeycredential
229+
[azure_core_exceptions]: https://aka.ms/azsdk/python/core/docs#module-azure.core.exceptions
230+
[python_logging]: https://docs.python.org/3/library/logging.html
231+
[azure_core_ref_docs]: https://aka.ms/azsdk/python/core/docs
232+
233+
[python-eg-sample-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs1_publish_custom_events_to_a_topic.py
234+
[python-eg-sample-customevent-to-domain]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs2_publish_custom_events_to_a_domain_topic.py
235+
[python-eg-sample-consume-systemevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs3_consume_system_events.py
236+
[python-eg-sample-consume-customevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs4_consume_custom_events.py
237+
[python-eg-sample-send-cloudevent]: 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
238+
[python-eg-sample-consume-cloudevent]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventgrid/azure-eventgrid/samples/champion_scenarios/cs6_consume_events_using_cloud_events_1.0_schema.py
239+
[publisher-service-doc]: https://docs.microsoft.com/en-us/azure/event-grid/concepts
240+
241+
[cla]: https://cla.microsoft.com
242+
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
243+
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/
244+
[coc_contact]: mailto:[email protected]

sdk/eventgrid/azure-eventgrid/azure/eventgrid/_consumer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def deserialize_event(self, event, **kwargs):
3232
"""Single event following CloudEvent/EventGridEvent schema will be parsed and returned as DeserializedEvent.
3333
:param event: The event to be deserialized.
3434
:type event: Union[str, dict, bytes]
35+
:keyword str encoding: The encoding that should be used. Defaults to 'utf-8'
3536
:rtype: models.DeserializedEvent
36-
37-
:raise: :class:`ValueError`, when events do not follow CloudEvent or EventGridEvent schema.
37+
:raises: :class:`ValueError`, when events do not follow CloudEvent or EventGridEvent schema.
3838
"""
3939
encode = kwargs.pop('encoding', 'utf-8')
4040
try:

sdk/eventgrid/azure-eventgrid/azure/eventgrid/_publisher_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def send(self, events, **kwargs):
5151
# type: (SendType, Any) -> None
5252
"""Sends event data to topic hostname specified during client initialization.
5353
54-
:param events: A list of CloudEvent/EventGridEvent/CustomEvent to be sent.
55-
:type events: Union[List[models.CloudEvent], List[models.EventGridEvent], List[models.CustomEvent]]
54+
:param events: A list or an instance of CloudEvent/EventGridEvent/CustomEvent to be sent.
55+
:type events: SendType
5656
:keyword str content_type: The type of content to be used to send the events.
5757
Has default value "application/json; charset=utf-8" for EventGridEvents, with "cloudevents-batch+json" for CloudEvents
5858
:rtype: None
59-
:raise: :class:`ValueError`, when events do not follow specified SendType.
59+
:raises: :class:`ValueError`, when events do not follow specified SendType.
6060
"""
6161
if not isinstance(events, list):
6262
events = [events]

sdk/eventgrid/azure-eventgrid/azure/eventgrid/aio/_publisher_client_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ async def send(self, events, **kwargs):
5252
# type: (SendType) -> None
5353
"""Sends event data to topic hostname specified during client initialization.
5454
55-
:param events: A list of CloudEvent/EventGridEvent/CustomEvent to be sent.
56-
:type events: Union[List[models.CloudEvent], List[models.EventGridEvent], List[models.CustomEvent]]
55+
:param events: A list or an instance of CloudEvent/EventGridEvent/CustomEvent to be sent.
56+
:type events: SendType
5757
:keyword str content_type: The type of content to be used to send the events.
5858
Has default value "application/json; charset=utf-8" for EventGridEvents, with "cloudevents-batch+json" for CloudEvents
5959
:rtype: None
60-
:raise: :class:`ValueError`, when events do not follow specified SendType.
60+
:raises: :class:`ValueError`, when events do not follow specified SendType.
6161
"""
6262
if not isinstance(events, list):
6363
events = [events]

0 commit comments

Comments
 (0)