Skip to content

Commit 5483b6a

Browse files
Rakshith Bhyravabhotlayunhaoling
andauthored
Add data as positional for cloud event (Azure#16288)
* Add data as positional for cloud event * lint * Update sdk/eventgrid/azure-eventgrid/azure/eventgrid/_models.py * Apply suggestions from code review Co-authored-by: Adam Ling (MSFT) <adam_ling@outlook.com> * docs * Update sdk/eventgrid/azure-eventgrid/azure/eventgrid/_models.py * Update sdk/eventgrid/azure-eventgrid/azure/eventgrid/_models.py Co-authored-by: Adam Ling (MSFT) <adam_ling@outlook.com> Co-authored-by: Adam Ling (MSFT) <adam_ling@outlook.com>
1 parent 95b40c2 commit 5483b6a

File tree

7 files changed

+156
-18
lines changed

7 files changed

+156
-18
lines changed

sdk/eventgrid/azure-eventgrid/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- The system events now exist in the `azure.eventgrid.systemevents` namespace instead of `azure.eventgrid.models` namespace.
1111
- The `send` method in the `EventGridPubliserClient` is now replaced by the `send_events`.
1212
- `topic_hostname` is renamed to `endpoint` in the `EventGridPublisherClient`.
13+
- `data` is now a required param for `CloudEvent`.
1314

1415
**Bug Fixes**
1516
- `EventGridEvent` has two additional required positional parameters namely, `data` and `data_version`.

sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def generate_shared_access_signature(endpoint, shared_access_key, expiration_dat
2727
Similar to <YOUR-TOPIC-NAME>.<YOUR-REGION-NAME>-1.eventgrid.azure.net
2828
:param str shared_access_key: The shared access key to be used for generating the token
2929
:param datetime.datetime expiration_date_utc: The expiration datetime in UTC for the signature.
30-
:param str api_version: The API Version to include in the signature.
30+
:keyword str api_version: The API Version to include in the signature.
3131
If not provided, the default API version will be used.
3232
:rtype: str
3333
"""

sdk/eventgrid/azure-eventgrid/azure/eventgrid/_models.py

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,54 @@ class CloudEvent(EventMixin): #pylint:disable=too-many-instance-attributes
5555
:param source: Required. Identifies the context in which an event happened. The combination of id and source must
5656
be unique for each distinct event. If publishing to a domain topic, source must be the domain name.
5757
:type source: str
58-
:param data: Event data specific to the event type.
59-
:type data: object
6058
:param type: Required. Type of event related to the originating occurrence.
6159
:type type: str
62-
:param time: The time (in UTC) the event was generated, in RFC3339 format.
60+
:param data: Required. Event data specific to the event type.
61+
:type data: object
62+
:keyword time: Optional. The time (in UTC) the event was generated, in RFC3339 format.
6363
:type time: ~datetime.datetime
64-
:param dataschema: Identifies the schema that data adheres to.
64+
:keyword dataschema: Optional. Identifies the schema that data adheres to.
6565
:type dataschema: str
66-
:param datacontenttype: Content type of data value.
66+
:keyword datacontenttype: Optional. Content type of data value.
6767
:type datacontenttype: str
68-
:param subject: This describes the subject of the event in the context of the event producer
68+
:keyword subject: Optional. This describes the subject of the event in the context of the event producer
6969
(identified by source).
7070
:type subject: str
71-
:param id: Optional. An identifier for the event. The combination of id and source must be
71+
:keyword specversion: Optional. The version of the CloudEvent spec. Defaults to "1.0"
72+
:type specversion: str
73+
:keyword id: Optional. An identifier for the event. The combination of id and source must be
7274
unique for each distinct event. If not provided, a random UUID will be generated and used.
7375
:type id: Optional[str]
76+
:ivar source: Identifies the context in which an event happened. The combination of id and source must
77+
be unique for each distinct event. If publishing to a domain topic, source must be the domain name.
78+
:vartype source: str
79+
:ivar data: Event data specific to the event type.
80+
:vartype data: object
81+
:ivar type: Type of event related to the originating occurrence.
82+
:vartype type: str
83+
:ivar time: The time (in UTC) the event was generated, in RFC3339 format.
84+
:vartype time: ~datetime.datetime
85+
:ivar dataschema: Identifies the schema that data adheres to.
86+
:vartype dataschema: str
87+
:ivar datacontenttype: Content type of data value.
88+
:vartype datacontenttype: str
89+
:ivar subject: This describes the subject of the event in the context of the event producer
90+
(identified by source).
91+
:vartype subject: str
92+
:ivar specversion: Optional. The version of the CloudEvent spec. Defaults to "1.0"
93+
:vartype specversion: str
94+
:ivar id: An identifier for the event. The combination of id and source must be
95+
unique for each distinct event. If not provided, a random UUID will be generated and used.
96+
:vartype id: Optional[str]
7497
"""
75-
def __init__(self, source, type, **kwargs): # pylint: disable=redefined-builtin
76-
# type: (str, str, Any) -> None
98+
def __init__(self, source, type, data, **kwargs): # pylint: disable=redefined-builtin
99+
# type: (str, str, object, Any) -> None
77100
self.source = source
78101
self.type = type
79102
self.specversion = kwargs.pop("specversion", "1.0")
80103
self.id = kwargs.pop("id", str(uuid.uuid4()))
81104
self.time = kwargs.pop("time", dt.datetime.now(UTC()).isoformat())
82-
self.data = kwargs.pop("data", None)
105+
self.data = data
83106
self.datacontenttype = kwargs.pop("datacontenttype", None)
84107
self.dataschema = kwargs.pop("dataschema", None)
85108
self.subject = kwargs.pop("subject", None)
@@ -145,18 +168,41 @@ class EventGridEvent(InternalEventGridEvent, EventMixin):
145168
:param data_version: Required. The schema version of the data object.
146169
If not provided, will be stamped with an empty value.
147170
:type data_version: str
148-
:param topic: The resource path of the event source. If not provided, Event Grid will stamp onto the event.
171+
:keyword topic: Optional. The resource path of the event source. If not provided, Event Grid will
172+
stamp onto the event.
149173
:type topic: str
150-
:ivar metadata_version: The schema version of the event metadata. If provided, must match Event Grid Schema exactly.
151-
If not provided, EventGrid will stamp onto event.
152-
:vartype metadata_version: str
153-
:param data_version: The schema version of the data object. If not provided, will be stamped with an empty value.
174+
:keyword metadata_version: Optional. The schema version of the event metadata. If provided,
175+
must match Event Grid Schema exactly. If not provided, EventGrid will stamp onto event.
176+
:type metadata_version: str
177+
:keyword data_version: Optional. The schema version of the data object. If not provided,
178+
will be stamped with an empty value.
154179
:type data_version: str
155-
:param id: Optional. An identifier for the event. In not provided, a random UUID will be generated and used.
180+
:keyword id: Optional. An identifier for the event. In not provided, a random UUID will be generated and used.
156181
:type id: Optional[str]
157-
:param event_time: Optional.The time (in UTC) of the event. If not provided,
182+
:keyword event_time: Optional.The time (in UTC) of the event. If not provided,
158183
it will be the time (in UTC) the event was generated.
159184
:type event_time: Optional[~datetime.datetime]
185+
:ivar subject: A resource path relative to the topic path.
186+
:vartype subject: str
187+
:ivar event_type: The type of the event that occurred.
188+
:vartype event_type: str
189+
:ivar data: Event data specific to the event type.
190+
:vartype data: object
191+
:ivar data_version: The schema version of the data object.
192+
If not provided, will be stamped with an empty value.
193+
:vartype data_version: str
194+
:ivar topic: The resource path of the event source. If not provided, Event Grid will stamp onto the event.
195+
:vartype topic: str
196+
:ivar metadata_version: The schema version of the event metadata. If provided, must match Event Grid Schema exactly.
197+
If not provided, EventGrid will stamp onto event.
198+
:vartype metadata_version: str
199+
:ivar data_version: The schema version of the data object. If not provided, will be stamped with an empty value.
200+
:vartype data_version: str
201+
:ivar id: An identifier for the event. In not provided, a random UUID will be generated and used.
202+
:vartype id: Optional[str]
203+
:ivar event_time: The time (in UTC) of the event. If not provided,
204+
it will be the time (in UTC) the event was generated.
205+
:vartype event_time: Optional[~datetime.datetime]
160206
"""
161207

162208
_validation = {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
interactions:
2+
- request:
3+
body: '[{"id": "880b26eb-96c6-4a14-b373-c3be6634cd21", "source": "http://samplesource.dev",
4+
"type": "Sample.Cloud.Event", "time": "2021-01-21T20:04:19.0468Z", "specversion":
5+
"1.0"}]'
6+
headers:
7+
Accept:
8+
- '*/*'
9+
Accept-Encoding:
10+
- gzip, deflate
11+
Connection:
12+
- keep-alive
13+
Content-Length:
14+
- '174'
15+
Content-Type:
16+
- application/cloudevents-batch+json; charset=utf-8
17+
User-Agent:
18+
- azsdk-python-eventgrid/2.0.0b5 Python/3.7.3 (Windows-10-10.0.18362-SP0)
19+
method: POST
20+
uri: https://cloudeventgridtestegtopic.westus-1.eventgrid.azure.net/api/events?api-version=2018-01-01
21+
response:
22+
body:
23+
string: ''
24+
headers:
25+
api-supported-versions:
26+
- '2018-01-01'
27+
content-length:
28+
- '0'
29+
date:
30+
- Thu, 21 Jan 2021 20:04:21 GMT
31+
server:
32+
- Microsoft-HTTPAPI/2.0
33+
strict-transport-security:
34+
- max-age=31536000; includeSubDomains
35+
status:
36+
code: 200
37+
message: OK
38+
version: 1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
interactions:
2+
- request:
3+
body: '[{"id": "e4d278ac-c129-433d-a9f7-10af74a41b44", "source": "http://samplesource.dev",
4+
"type": "Sample.Cloud.Event", "time": "2021-01-21T20:04:44.736922Z", "specversion":
5+
"1.0"}]'
6+
headers:
7+
Content-Length:
8+
- '176'
9+
Content-Type:
10+
- application/cloudevents-batch+json; charset=utf-8
11+
User-Agent:
12+
- azsdk-python-eventgridpublisherclient/2.0.0b5 Python/3.7.3 (Windows-10-10.0.18362-SP0)
13+
method: POST
14+
uri: https://cloudeventgridtestegtopic.westus-1.eventgrid.azure.net/api/events?api-version=2018-01-01
15+
response:
16+
body:
17+
string: ''
18+
headers:
19+
api-supported-versions: '2018-01-01'
20+
content-length: '0'
21+
date: Thu, 21 Jan 2021 20:04:46 GMT
22+
server: Microsoft-HTTPAPI/2.0
23+
strict-transport-security: max-age=31536000; includeSubDomains
24+
status:
25+
code: 200
26+
message: OK
27+
url: https://cloudeventgridtestmehspu.westus-1.eventgrid.azure.net/api/events?api-version=2018-01-01
28+
version: 1

sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ def test_send_cloud_event_data_dict(self, resource_group, eventgrid_topic, event
110110
)
111111
client.send_events(cloud_event)
112112

113+
114+
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
115+
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
116+
def test_send_cloud_event_data_none(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
117+
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
118+
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
119+
cloud_event = CloudEvent(
120+
source = "http://samplesource.dev",
121+
data = None,
122+
type="Sample.Cloud.Event"
123+
)
124+
client.send_events(cloud_event)
125+
113126
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
114127
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
115128
def test_send_cloud_event_data_str(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):

sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client_async.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ async def test_send_cloud_event_dict(self, resource_group, eventgrid_topic, even
198198
}
199199
await client.send_events(cloud_event1)
200200

201+
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
202+
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
203+
@pytest.mark.asyncio
204+
async def test_send_cloud_event_data_none(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
205+
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
206+
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
207+
cloud_event = CloudEvent(
208+
source = "http://samplesource.dev",
209+
data = None,
210+
type="Sample.Cloud.Event"
211+
)
212+
await client.send_events(cloud_event)
201213

202214
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
203215
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')

0 commit comments

Comments
 (0)