2323from cloudevents .sdk .event import v1 , v03
2424
2525
26- def _best_effort_serialize_to_json (
26+ def _best_effort_serialize_to_json ( # type: ignore[no-untyped-def]
2727 value : typing .Any , * args , ** kwargs
2828) -> typing .Optional [typing .Union [bytes , str , typing .Any ]]:
2929 """
@@ -43,18 +43,18 @@ def _best_effort_serialize_to_json(
4343 return value
4444
4545
46- _default_marshaller_by_format = {
46+ _default_marshaller_by_format : typing . Dict [ str , types . MarshallerType ] = {
4747 converters .TypeStructured : lambda x : x ,
4848 converters .TypeBinary : _best_effort_serialize_to_json ,
49- } # type: typing.Dict[str, types.MarshallerType]
49+ }
5050
5151_obj_by_version = {"1.0" : v1 .Event , "0.3" : v03 .Event }
5252
5353
5454def to_json (
5555 event : AnyCloudEvent ,
56- data_marshaller : types .MarshallerType = None ,
57- ) -> typing . Union [ str , bytes ] :
56+ data_marshaller : typing . Optional [ types .MarshallerType ] = None ,
57+ ) -> bytes :
5858 """
5959 Converts given `event` to a JSON string.
6060
@@ -69,7 +69,7 @@ def to_json(
6969def from_json (
7070 event_type : typing .Type [AnyCloudEvent ],
7171 data : typing .Union [str , bytes ],
72- data_unmarshaller : types .UnmarshallerType = None ,
72+ data_unmarshaller : typing . Optional [ types .UnmarshallerType ] = None ,
7373) -> AnyCloudEvent :
7474 """
7575 Parses JSON string `data` into a CloudEvent.
@@ -91,9 +91,9 @@ def from_json(
9191
9292def from_http (
9393 event_type : typing .Type [AnyCloudEvent ],
94- headers : typing .Dict [str , str ],
95- data : typing .Union [str , bytes , None ],
96- data_unmarshaller : types .UnmarshallerType = None ,
94+ headers : typing .Mapping [str , str ],
95+ data : typing .Optional [ typing . Union [str , bytes ] ],
96+ data_unmarshaller : typing . Optional [ types .UnmarshallerType ] = None ,
9797) -> AnyCloudEvent :
9898 """
9999 Parses CloudEvent `data` and `headers` into an instance of a given `event_type`.
@@ -133,14 +133,14 @@ def from_http(
133133 except json .decoder .JSONDecodeError :
134134 raise cloud_exceptions .MissingRequiredFields (
135135 "Failed to read specversion from both headers and data. "
136- f "The following can not be parsed as json: { data } "
136+ "The following can not be parsed as json: {!r}" . format ( data )
137137 )
138138 if hasattr (raw_ce , "get" ):
139139 specversion = raw_ce .get ("specversion" , None )
140140 else :
141141 raise cloud_exceptions .MissingRequiredFields (
142142 "Failed to read specversion from both headers and data. "
143- f "The following deserialized data has no 'get' method: { raw_ce } "
143+ "The following deserialized data has no 'get' method: {}" . format ( raw_ce )
144144 )
145145
146146 if specversion is None :
@@ -152,7 +152,7 @@ def from_http(
152152
153153 if event_handler is None :
154154 raise cloud_exceptions .InvalidRequiredFields (
155- f "Found invalid specversion { specversion } "
155+ "Found invalid specversion {}" . format ( specversion )
156156 )
157157
158158 event = marshall .FromRequest (
@@ -163,20 +163,19 @@ def from_http(
163163 attrs .pop ("extensions" , None )
164164 attrs .update (** event .extensions )
165165
166+ result_data : typing .Optional [typing .Any ] = event .data
166167 if event .data == "" or event .data == b"" :
167168 # TODO: Check binary unmarshallers to debug why setting data to ""
168- # returns an event with data set to None, but structured will return ""
169- data = None
170- else :
171- data = event .data
172- return event_type .create (attrs , data )
169+ # returns an event with data set to None, but structured will return ""
170+ result_data = None
171+ return event_type .create (attrs , result_data )
173172
174173
175174def _to_http (
176175 event : AnyCloudEvent ,
177176 format : str = converters .TypeStructured ,
178- data_marshaller : types .MarshallerType = None ,
179- ) -> typing .Tuple [dict , typing .Union [ bytes , str ]]:
177+ data_marshaller : typing . Optional [ types .MarshallerType ] = None ,
178+ ) -> typing .Tuple [typing .Dict [ str , str ], bytes ]:
180179 """
181180 Returns a tuple of HTTP headers/body dicts representing this Cloud Event.
182181
@@ -196,7 +195,7 @@ def _to_http(
196195 event_handler = _obj_by_version [event ["specversion" ]]()
197196 for attribute_name in event :
198197 event_handler .Set (attribute_name , event [attribute_name ])
199- event_handler .data = event .data
198+ event_handler .data = event .get_data ()
200199
201200 return marshaller .NewDefaultHTTPMarshaller ().ToRequest (
202201 event_handler , format , data_marshaller = data_marshaller
@@ -205,8 +204,8 @@ def _to_http(
205204
206205def to_structured (
207206 event : AnyCloudEvent ,
208- data_marshaller : types .MarshallerType = None ,
209- ) -> typing .Tuple [dict , typing .Union [ bytes , str ]]:
207+ data_marshaller : typing . Optional [ types .MarshallerType ] = None ,
208+ ) -> typing .Tuple [typing .Dict [ str , str ], bytes ]:
210209 """
211210 Returns a tuple of HTTP headers/body dicts representing this Cloud Event.
212211
@@ -222,8 +221,8 @@ def to_structured(
222221
223222
224223def to_binary (
225- event : AnyCloudEvent , data_marshaller : types .MarshallerType = None
226- ) -> typing .Tuple [dict , typing .Union [ bytes , str ]]:
224+ event : AnyCloudEvent , data_marshaller : typing . Optional [ types .MarshallerType ] = None
225+ ) -> typing .Tuple [typing .Dict [ str , str ], bytes ]:
227226 """
228227 Returns a tuple of HTTP headers/body dicts representing this Cloud Event.
229228
@@ -287,19 +286,13 @@ def to_dict(event: AnyCloudEvent) -> typing.Dict[str, typing.Any]:
287286 :returns: The canonical dict representation of the event.
288287 """
289288 result = {attribute_name : event .get (attribute_name ) for attribute_name in event }
290- result ["data" ] = event .data
289+ result ["data" ] = event .get_data ()
291290 return result
292291
293292
294293def _json_or_string (
295- content : typing .Optional [typing .AnyStr ],
296- ) -> typing .Optional [
297- typing .Union [
298- typing .Dict [typing .Any , typing .Any ],
299- typing .List [typing .Any ],
300- typing .AnyStr ,
301- ]
302- ]:
294+ content : typing .Optional [typing .Union [str , bytes ]],
295+ ) -> typing .Any :
303296 """
304297 Returns a JSON-decoded dictionary or a list of dictionaries if
305298 a valid JSON string is provided.
0 commit comments