11import json
22import typing
33
4+ from deprecation import deprecated
5+
46import cloudevents .exceptions as cloud_exceptions
57from cloudevents .http .event import CloudEvent
68from cloudevents .http .event_type import is_binary , is_structured
1012
1113
1214def from_http (
13- data : typing .Union [str , bytes ],
1415 headers : typing .Dict [str , str ],
16+ data : typing .Union [str , bytes , None ],
1517 data_unmarshaller : types .UnmarshallerType = None ,
1618):
1719 """
1820 Unwrap a CloudEvent (binary or structured) from an HTTP request.
19- :param data: the HTTP request body
20- :type data: typing.IO
2121 :param headers: the HTTP headers
2222 :type headers: typing.Dict[str, str]
23+ :param data: the HTTP request body
24+ :type data: typing.IO
2325 :param data_unmarshaller: Callable function to map data to a python object
2426 e.g. lambda x: x or lambda x: json.loads(x)
2527 :type data_unmarshaller: types.UnmarshallerType
2628 """
29+ if data is None :
30+ data = ""
31+
32+ if not isinstance (data , (str , bytes , bytearray )):
33+ raise cloud_exceptions .InvalidStructuredJSON (
34+ "Expected json of type (str, bytes, bytearray), "
35+ f"but instead found { type (data )} . "
36+ )
37+
38+ headers = {key .lower (): value for key , value in headers .items ()}
2739 if data_unmarshaller is None :
2840 data_unmarshaller = _json_or_string
2941
@@ -32,19 +44,25 @@ def from_http(
3244 if is_binary (headers ):
3345 specversion = headers .get ("ce-specversion" , None )
3446 else :
35- raw_ce = json .loads (data )
47+ try :
48+ raw_ce = json .loads (data )
49+ except json .decoder .JSONDecodeError :
50+ raise cloud_exceptions .InvalidStructuredJSON (
51+ "Failed to read fields from structured event. "
52+ f"The following can not be parsed as json: { data } . "
53+ )
3654 specversion = raw_ce .get ("specversion" , None )
3755
3856 if specversion is None :
39- raise cloud_exceptions .CloudEventMissingRequiredFields (
40- "could not find specversion in HTTP request"
57+ raise cloud_exceptions .MissingRequiredFields (
58+ "Failed to find specversion in HTTP request. "
4159 )
4260
4361 event_handler = _obj_by_version .get (specversion , None )
4462
4563 if event_handler is None :
46- raise cloud_exceptions .CloudEventTypeErrorRequiredFields (
47- f"found invalid specversion { specversion } "
64+ raise cloud_exceptions .InvalidRequiredFields (
65+ f"Found invalid specversion { specversion } . "
4866 )
4967
5068 event = marshall .FromRequest (
@@ -77,8 +95,8 @@ def _to_http(
7795 data_marshaller = _marshaller_by_format [format ]
7896
7997 if event ._attributes ["specversion" ] not in _obj_by_version :
80- raise cloud_exceptions .CloudEventTypeErrorRequiredFields (
81- f"Unsupported specversion: { event ._attributes ['specversion' ]} "
98+ raise cloud_exceptions .InvalidRequiredFields (
99+ f"Unsupported specversion: { event ._attributes ['specversion' ]} . "
82100 )
83101
84102 event_handler = _obj_by_version [event ._attributes ["specversion" ]]()
@@ -91,11 +109,13 @@ def _to_http(
91109 )
92110
93111
94- def to_structured_http (
112+ def to_structured (
95113 event : CloudEvent , data_marshaller : types .MarshallerType = None ,
96114) -> (dict , typing .Union [bytes , str ]):
97115 """
98- Returns a tuple of HTTP headers/body dicts representing this cloudevent
116+ Returns a tuple of HTTP headers/body dicts representing this cloudevent. If
117+ event.data is a byte object, body will have a data_base64 field instead of
118+ data.
99119
100120 :param event: CloudEvent to cast into http data
101121 :type event: CloudEvent
@@ -107,7 +127,7 @@ def to_structured_http(
107127 return _to_http (event = event , data_marshaller = data_marshaller )
108128
109129
110- def to_binary_http (
130+ def to_binary (
111131 event : CloudEvent , data_marshaller : types .MarshallerType = None ,
112132) -> (dict , typing .Union [bytes , str ]):
113133 """
@@ -125,3 +145,17 @@ def to_binary_http(
125145 format = converters .TypeBinary ,
126146 data_marshaller = data_marshaller ,
127147 )
148+
149+
150+ @deprecated (deprecated_in = "1.0.2" , details = "Use to_binary function instead" )
151+ def to_binary_http (
152+ event : CloudEvent , data_marshaller : types .MarshallerType = None ,
153+ ) -> (dict , typing .Union [bytes , str ]):
154+ return to_binary (event , data_marshaller )
155+
156+
157+ @deprecated (deprecated_in = "1.0.2" , details = "Use to_structured function instead" )
158+ def to_structured_http (
159+ event : CloudEvent , data_marshaller : types .MarshallerType = None ,
160+ ) -> (dict , typing .Union [bytes , str ]):
161+ return to_structured (event , data_marshaller )
0 commit comments