Skip to content

Commit f39991c

Browse files
author
Rakshith Bhyravabhotla
authored
Cloud Event Abstraction (#13542)
* initial commit * oops * some changes * lint * test fix * sas key * some more cahgnes * test fix * 2.7 compat * comments * extensions * changes * Update sdk/eventgrid/azure-eventgrid/azure/eventgrid/_models.py * remove test
1 parent 67520e5 commit f39991c

File tree

31 files changed

+441
-141
lines changed

31 files changed

+441
-141
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def decode_cloud_event(self, cloud_event, **kwargs):
3838
encode = kwargs.pop('encoding', 'utf-8')
3939
try:
4040
cloud_event = CloudEvent._from_json(cloud_event, encode)
41-
deserialized_event = CloudEvent.deserialize(cloud_event)
41+
deserialized_event = CloudEvent._from_generated(cloud_event)
4242
CloudEvent._deserialize_data(deserialized_event, deserialized_event.type)
4343
return deserialized_event
4444
except Exception as err:

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

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _from_json(event, encode):
5050
return event
5151

5252

53-
class CloudEvent(InternalCloudEvent, EventMixin): #pylint:disable=too-many-instance-attributes
53+
class CloudEvent(EventMixin): #pylint:disable=too-many-instance-attributes
5454
"""Properties of an event published to an Event Grid topic using the CloudEvent 1.0 Schema.
5555
5656
All required parameters must be populated in order to send to Azure.
@@ -75,35 +75,60 @@ class CloudEvent(InternalCloudEvent, EventMixin): #pylint:disable=too-many-ins
7575
unique for each distinct event.
7676
:type id: Optional[str]
7777
"""
78-
79-
_validation = {
80-
'source': {'required': True},
81-
'type': {'required': True},
82-
}
83-
84-
_attribute_map = {
85-
'additional_properties': {'key': '', 'type': '{object}'},
86-
'id': {'key': 'id', 'type': 'str'},
87-
'source': {'key': 'source', 'type': 'str'},
88-
'data': {'key': 'data', 'type': 'object'},
89-
'data_base64': {'key': 'data_base64', 'type': 'bytearray'},
90-
'type': {'key': 'type', 'type': 'str'},
91-
'time': {'key': 'time', 'type': 'iso-8601'},
92-
'specversion': {'key': 'specversion', 'type': 'str'},
93-
'dataschema': {'key': 'dataschema', 'type': 'str'},
94-
'datacontenttype': {'key': 'datacontenttype', 'type': 'str'},
95-
'subject': {'key': 'subject', 'type': 'str'},
96-
}
97-
9878
def __init__(self, source, type, **kwargs):
9979
# type: (str, str, Any) -> None
100-
kwargs.setdefault('id', uuid.uuid4())
101-
kwargs.setdefault("source", source)
102-
kwargs.setdefault("type", type)
103-
kwargs.setdefault("time", dt.datetime.now(UTC()).isoformat())
104-
kwargs.setdefault("specversion", "1.0")
105-
106-
super(CloudEvent, self).__init__(**kwargs)
80+
self.source = source
81+
self.type = type
82+
self.specversion = kwargs.pop("specversion", "1.0")
83+
self.id = kwargs.pop("id", str(uuid.uuid4()))
84+
self.time = kwargs.pop("time", dt.datetime.now(UTC()).isoformat())
85+
self.data = kwargs.pop("data", None)
86+
self.datacontenttype = kwargs.pop("datacontenttype", None)
87+
self.dataschema = kwargs.pop("dataschema", None)
88+
self.subject = kwargs.pop("subject", None)
89+
self.extensions = {}
90+
self.extensions.update({k:v for k, v in kwargs.pop('extensions', {}).items()})
91+
92+
@classmethod
93+
def _from_generated(cls, cloud_event, **kwargs):
94+
generated = InternalCloudEvent.deserialize(cloud_event)
95+
if generated.additional_properties:
96+
extensions = {k:v for k, v in generated.additional_properties.items()}
97+
kwargs.setdefault('extensions', extensions)
98+
return cls(
99+
id=generated.id,
100+
source=generated.source,
101+
type=generated.type,
102+
specversion=generated.specversion,
103+
data=generated.data or generated.data_base64,
104+
time=generated.time,
105+
dataschema=generated.dataschema,
106+
datacontenttype=generated.datacontenttype,
107+
subject=generated.subject,
108+
**kwargs
109+
)
110+
111+
def _to_generated(self, **kwargs):
112+
if isinstance(self.data, six.binary_type):
113+
data_base64 = self.data
114+
data = None
115+
else:
116+
data_base64 = None
117+
data = self.data
118+
return InternalCloudEvent(
119+
id=self.id,
120+
source=self.source,
121+
type=self.type,
122+
specversion=self.specversion,
123+
data=data,
124+
data_base64=data_base64,
125+
time=self.time,
126+
dataschema=self.dataschema,
127+
datacontenttype=self.datacontenttype,
128+
subject=self.subject,
129+
additional_properties=self.extensions,
130+
**kwargs
131+
)
107132

108133

109134
class EventGridEvent(InternalEventGridEvent, EventMixin):

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def send(self, events, **kwargs):
6262
events = [events]
6363

6464
if all(isinstance(e, CloudEvent) for e in events) or all(_is_cloud_event(e) for e in events):
65+
try:
66+
events = [e._to_generated(**kwargs) for e in events]
67+
except AttributeError:
68+
pass # means it's a dictionary
6569
kwargs.setdefault("content_type", "application/cloudevents-batch+json; charset=utf-8")
6670
self._client.publish_cloud_event_events(self._topic_hostname, events, **kwargs)
6771
elif all(isinstance(e, EventGridEvent) for e in events) or all(isinstance(e, dict) for e in events):

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ async def send(self, events, **kwargs):
6363
events = [events]
6464

6565
if all(isinstance(e, CloudEvent) for e in events) or all(_is_cloud_event(e) for e in events):
66+
try:
67+
events = [e._to_generated(**kwargs) for e in events]
68+
except AttributeError:
69+
pass # means it's a dictionary
6670
kwargs.setdefault("content_type", "application/cloudevents-batch+json; charset=utf-8")
6771
await self._client.publish_cloud_event_events(self._topic_hostname, events, **kwargs)
6872
elif all(isinstance(e, EventGridEvent) for e in events) or all(isinstance(e, dict) for e in events):

sdk/eventgrid/azure-eventgrid/tests/recordings/test_eg_publisher_client.test_send_cloud_event_data_as_list.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
interactions:
22
- request:
3-
body: '[{"id": "3dc4b913-4bc2-41f8-be9b-bf1f67069806", "source": "http://samplesource.dev",
4-
"data": "cloudevent", "type": "Sample.Cloud.Event", "time": "2020-08-19T03:36:41.947462Z",
3+
body: '[{"id": "4e7a9df5-8e28-42c8-adca-4412c93e92d5", "source": "http://samplesource.dev",
4+
"data": "cloudevent", "type": "Sample.Cloud.Event", "time": "2020-09-03T21:37:13.810619Z",
55
"specversion": "1.0"}]'
66
headers:
77
Accept:
@@ -16,8 +16,6 @@ interactions:
1616
- application/cloudevents-batch+json; charset=utf-8
1717
User-Agent:
1818
- azsdk-python-eventgridpublisherclient/unknown Python/3.7.3 (Windows-10-10.0.18362-SP0)
19-
aeg-sas-key:
20-
- dHUaOOg5xRj+D7iH/AC92GyHweLx9ugrDuMDg4e5Xvw=
2119
method: POST
2220
uri: https://cloudeventgridtestegtopic.westus-1.eventgrid.azure.net/api/events?api-version=2018-01-01
2321
response:
@@ -29,7 +27,7 @@ interactions:
2927
content-length:
3028
- '0'
3129
date:
32-
- Wed, 19 Aug 2020 03:36:43 GMT
30+
- Thu, 03 Sep 2020 21:37:12 GMT
3331
server:
3432
- Microsoft-HTTPAPI/2.0
3533
strict-transport-security:
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": "a3f05c78-9a8d-44d8-a1a3-131b8da61192", "source": "http://samplesource.dev",
4+
"data_base64": "Y2xvdWRldmVudA==", "type": "Sample.Cloud.Event", "time": "2020-09-04T17:06:31.601865Z",
5+
"specversion": "1.0"}]'
6+
headers:
7+
Accept:
8+
- '*/*'
9+
Accept-Encoding:
10+
- gzip, deflate
11+
Connection:
12+
- keep-alive
13+
Content-Length:
14+
- '211'
15+
Content-Type:
16+
- application/cloudevents-batch+json; charset=utf-8
17+
User-Agent:
18+
- azsdk-python-eventgridpublisherclient/unknown 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+
- Fri, 04 Sep 2020 17:06:29 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

sdk/eventgrid/azure-eventgrid/tests/recordings/test_eg_publisher_client.test_send_cloud_event_data_dict.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
interactions:
22
- request:
3-
body: '[{"id": "51c18497-2a25-45f1-b9ba-fdaf08c00263", "source": "http://samplesource.dev",
4-
"data": {"sample": "cloudevent"}, "type": "Sample.Cloud.Event", "time": "2020-08-19T03:36:42.304479Z",
3+
body: '[{"id": "51c747e0-7e8f-467c-80e4-920fdf1d95d2", "source": "http://samplesource.dev",
4+
"data": {"sample": "cloudevent"}, "type": "Sample.Cloud.Event", "time": "2020-09-03T21:37:14.383108Z",
55
"specversion": "1.0"}]'
66
headers:
77
Accept:
@@ -16,8 +16,6 @@ interactions:
1616
- application/cloudevents-batch+json; charset=utf-8
1717
User-Agent:
1818
- azsdk-python-eventgridpublisherclient/unknown Python/3.7.3 (Windows-10-10.0.18362-SP0)
19-
aeg-sas-key:
20-
- dHUaOOg5xRj+D7iH/AC92GyHweLx9ugrDuMDg4e5Xvw=
2119
method: POST
2220
uri: https://cloudeventgridtestegtopic.westus-1.eventgrid.azure.net/api/events?api-version=2018-01-01
2321
response:
@@ -29,7 +27,7 @@ interactions:
2927
content-length:
3028
- '0'
3129
date:
32-
- Wed, 19 Aug 2020 03:36:43 GMT
30+
- Thu, 03 Sep 2020 21:37:12 GMT
3331
server:
3432
- Microsoft-HTTPAPI/2.0
3533
strict-transport-security:

sdk/eventgrid/azure-eventgrid/tests/recordings/test_eg_publisher_client.test_send_cloud_event_data_str.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
interactions:
22
- request:
3-
body: '[{"id": "6a315e93-a59c-4eca-b2f2-6bf3b8f27984", "source": "http://samplesource.dev",
4-
"data": "cloudevent", "type": "Sample.Cloud.Event", "time": "2020-08-19T03:36:42.629467Z",
3+
body: '[{"id": "eef0ee57-9833-44fd-9f52-d5f20b74267d", "source": "http://samplesource.dev",
4+
"data": "cloudevent", "type": "Sample.Cloud.Event", "time": "2020-09-03T21:37:14.803665Z",
55
"specversion": "1.0"}]'
66
headers:
77
Accept:
@@ -16,8 +16,6 @@ interactions:
1616
- application/cloudevents-batch+json; charset=utf-8
1717
User-Agent:
1818
- azsdk-python-eventgridpublisherclient/unknown Python/3.7.3 (Windows-10-10.0.18362-SP0)
19-
aeg-sas-key:
20-
- dHUaOOg5xRj+D7iH/AC92GyHweLx9ugrDuMDg4e5Xvw=
2119
method: POST
2220
uri: https://cloudeventgridtestegtopic.westus-1.eventgrid.azure.net/api/events?api-version=2018-01-01
2321
response:
@@ -29,7 +27,7 @@ interactions:
2927
content-length:
3028
- '0'
3129
date:
32-
- Wed, 19 Aug 2020 03:36:43 GMT
30+
- Thu, 03 Sep 2020 21:37:13 GMT
3331
server:
3432
- Microsoft-HTTPAPI/2.0
3533
strict-transport-security:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
interactions:
2+
- request:
3+
body: '[{"reason_code": 204, "extension": "extension", "id": "9da8e4ce-df2c-4480-b809-c7132fb3ee74",
4+
"source": "http://samplesource.dev", "data": "cloudevent", "type": "Sample.Cloud.Event",
5+
"time": "2020-09-03T21:37:15.165652Z", "specversion": "1.0"}]'
6+
headers:
7+
Accept:
8+
- '*/*'
9+
Accept-Encoding:
10+
- gzip, deflate
11+
Connection:
12+
- keep-alive
13+
Content-Length:
14+
- '244'
15+
Content-Type:
16+
- application/cloudevents-batch+json; charset=utf-8
17+
User-Agent:
18+
- azsdk-python-eventgridpublisherclient/unknown 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, 03 Sep 2020 21:37:13 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

sdk/eventgrid/azure-eventgrid/tests/recordings/test_eg_publisher_client.test_send_cloud_event_dict.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ interactions:
1515
- application/cloudevents-batch+json; charset=utf-8
1616
User-Agent:
1717
- azsdk-python-eventgridpublisherclient/unknown Python/3.7.3 (Windows-10-10.0.18362-SP0)
18-
aeg-sas-key:
19-
- dHUaOOg5xRj+D7iH/AC92GyHweLx9ugrDuMDg4e5Xvw=
2018
method: POST
2119
uri: https://cloudeventgridtestegtopic.westus-1.eventgrid.azure.net/api/events?api-version=2018-01-01
2220
response:
@@ -28,7 +26,7 @@ interactions:
2826
content-length:
2927
- '0'
3028
date:
31-
- Wed, 19 Aug 2020 03:36:44 GMT
29+
- Thu, 03 Sep 2020 21:37:13 GMT
3230
server:
3331
- Microsoft-HTTPAPI/2.0
3432
strict-transport-security:

0 commit comments

Comments
 (0)