-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathconversion.py
More file actions
266 lines (225 loc) · 9.28 KB
/
conversion.py
File metadata and controls
266 lines (225 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright 2018-Present The CloudEvents Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import base64
import json
import typing
from cloudevents_v1 import exceptions as cloud_exceptions
from cloudevents_v1 import http
from cloudevents_v1.abstract import AnyCloudEvent
from cloudevents_v1.kafka.exceptions import KeyMapperError
from cloudevents_v1.sdk import types
DEFAULT_MARSHALLER: types.MarshallerType = json.dumps
DEFAULT_UNMARSHALLER: types.MarshallerType = json.loads
DEFAULT_EMBEDDED_DATA_MARSHALLER: types.MarshallerType = lambda x: x
class KafkaMessage(typing.NamedTuple):
"""
Represents the elements of a message sent or received through the Kafka protocol.
Callers can map their client-specific message representation to and from this
type in order to use the cloudevents.kafka conversion functions.
"""
headers: typing.Dict[str, bytes]
"""
The dictionary of message headers key/values.
"""
key: typing.Optional[typing.Union[str, bytes]]
"""
The message key.
"""
value: typing.Union[str, bytes]
"""
The message value.
"""
KeyMapper = typing.Callable[[AnyCloudEvent], typing.AnyStr]
"""
A callable function that creates a Kafka message key, given a CloudEvent instance.
"""
DEFAULT_KEY_MAPPER: KeyMapper = lambda event: event.get("partitionkey")
"""
The default KeyMapper which maps the user provided `partitionkey` attribute value
to the `key` of the Kafka message as-is, if present.
"""
def to_binary(
event: AnyCloudEvent,
data_marshaller: typing.Optional[types.MarshallerType] = None,
key_mapper: typing.Optional[KeyMapper] = None,
) -> KafkaMessage:
"""
Returns a KafkaMessage in binary format representing this Cloud Event.
:param event: The event to be converted. To specify the Kafka messaage Key, set
the `partitionkey` attribute of the event, or provide a KeyMapper.
:param data_marshaller: Callable function to cast event.data into
either a string or bytes.
:param key_mapper: Callable function to get the Kafka message key.
:returns: KafkaMessage
"""
data_marshaller = data_marshaller or DEFAULT_MARSHALLER
key_mapper = key_mapper or DEFAULT_KEY_MAPPER
try:
message_key = key_mapper(event)
except Exception as e:
raise KeyMapperError(
f"Failed to map message key with error: {type(e).__name__}('{e}')"
)
headers = {}
if event["datacontenttype"]:
headers["content-type"] = event["datacontenttype"].encode("utf-8")
for attr, value in event.get_attributes().items():
if attr not in ["data", "partitionkey", "datacontenttype"]:
if value is not None:
headers["ce_{0}".format(attr)] = value.encode("utf-8")
try:
data = data_marshaller(event.get_data())
except Exception as e:
raise cloud_exceptions.DataMarshallerError(
f"Failed to marshall data with error: {type(e).__name__}('{e}')"
)
if isinstance(data, str):
data = data.encode("utf-8")
return KafkaMessage(headers, message_key, data)
def from_binary(
message: KafkaMessage,
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None,
data_unmarshaller: typing.Optional[types.MarshallerType] = None,
) -> AnyCloudEvent:
"""
Returns a CloudEvent from a KafkaMessage in binary format.
:param message: The KafkaMessage to be converted.
:param event_type: The type of CloudEvent to create. Defaults to http.CloudEvent.
:param data_unmarshaller: Callable function to map data to a python object
:returns: CloudEvent
"""
data_unmarshaller = data_unmarshaller or DEFAULT_UNMARSHALLER
attributes: typing.Dict[str, typing.Any] = {}
for header, value in message.headers.items():
header = header.lower()
if header == "content-type":
attributes["datacontenttype"] = value.decode()
elif header.startswith("ce_"):
attributes[header[3:]] = value.decode()
if message.key is not None:
attributes["partitionkey"] = message.key
try:
data = data_unmarshaller(message.value)
except Exception as e:
raise cloud_exceptions.DataUnmarshallerError(
f"Failed to unmarshall data with error: {type(e).__name__}('{e}')"
)
if event_type:
result = event_type.create(attributes, data)
else:
result = http.CloudEvent.create(attributes, data) # type: ignore
return result
def to_structured(
event: AnyCloudEvent,
data_marshaller: typing.Optional[types.MarshallerType] = None,
envelope_marshaller: typing.Optional[types.MarshallerType] = None,
key_mapper: typing.Optional[KeyMapper] = None,
) -> KafkaMessage:
"""
Returns a KafkaMessage in structured format representing this Cloud Event.
:param event: The event to be converted. To specify the Kafka message KEY, set
the `partitionkey` attribute of the event.
:param data_marshaller: Callable function to cast event.data into
either a string or bytes.
:param envelope_marshaller: Callable function to cast event envelope into
either a string or bytes.
:param key_mapper: Callable function to get the Kafka message key.
:returns: KafkaMessage
"""
data_marshaller = data_marshaller or DEFAULT_EMBEDDED_DATA_MARSHALLER
envelope_marshaller = envelope_marshaller or DEFAULT_MARSHALLER
key_mapper = key_mapper or DEFAULT_KEY_MAPPER
try:
message_key = key_mapper(event)
except Exception as e:
raise KeyMapperError(
f"Failed to map message key with error: {type(e).__name__}('{e}')"
)
attrs: typing.Dict[str, typing.Any] = dict(event.get_attributes())
try:
data = data_marshaller(event.get_data())
except Exception as e:
raise cloud_exceptions.DataMarshallerError(
f"Failed to marshall data with error: {type(e).__name__}('{e}')"
)
if isinstance(data, (bytes, bytes, memoryview)):
attrs["data_base64"] = base64.b64encode(data).decode("ascii")
else:
attrs["data"] = data
headers = {}
if "datacontenttype" in attrs:
headers["content-type"] = attrs.pop("datacontenttype").encode("utf-8")
try:
value = envelope_marshaller(attrs)
except Exception as e:
raise cloud_exceptions.DataMarshallerError(
f"Failed to marshall event with error: {type(e).__name__}('{e}')"
)
if isinstance(value, str):
value = value.encode("utf-8")
return KafkaMessage(headers, message_key, value)
def from_structured(
message: KafkaMessage,
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None,
data_unmarshaller: typing.Optional[types.MarshallerType] = None,
envelope_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
) -> AnyCloudEvent:
"""
Returns a CloudEvent from a KafkaMessage in structured format.
:param message: The KafkaMessage to be converted.
:param event_type: The type of CloudEvent to create. Defaults to http.CloudEvent.
:param data_unmarshaller: Callable function to map the data to a python object.
:param envelope_unmarshaller: Callable function to map the envelope to a python
object.
:returns: CloudEvent
"""
data_unmarshaller = data_unmarshaller or DEFAULT_EMBEDDED_DATA_MARSHALLER
envelope_unmarshaller = envelope_unmarshaller or DEFAULT_UNMARSHALLER
try:
structure = envelope_unmarshaller(message.value)
except Exception as e:
raise cloud_exceptions.DataUnmarshallerError(
f"Failed to unmarshall message with error: {type(e).__name__}('{e}')"
)
attributes: typing.Dict[str, typing.Any] = {}
if message.key is not None:
attributes["partitionkey"] = message.key
data: typing.Optional[typing.Any] = None
for name, value in structure.items():
try:
if name == "data":
decoded_value = data_unmarshaller(value)
elif name == "data_base64":
decoded_value = data_unmarshaller(base64.b64decode(value))
name = "data"
else:
decoded_value = value
except Exception as e:
raise cloud_exceptions.DataUnmarshallerError(
f"Failed to unmarshall data with error: {type(e).__name__}('{e}')"
)
if name == "data":
data = decoded_value
else:
attributes[name] = decoded_value
for header, val in message.headers.items():
if header.lower() == "content-type":
attributes["datacontenttype"] = val.decode()
else:
attributes[header.lower()] = val.decode()
if event_type:
result = event_type.create(attributes, data)
else:
result = http.CloudEvent.create(attributes, data) # type: ignore
return result