2424
2525
2626class HTTPMarshaller (object ):
27+ """
28+ HTTP Marshaller class.
29+ API of this class designed to work with CloudEvent (upstream and v0.1)
30+ """
2731
2832 def __init__ (self , converters : typing .List [base .Converter ]):
29- self .__converters = converters
33+ """
34+ CloudEvent HTTP marshaller constructor
35+ :param converters: a list of HTTP-to-CloudEvent-to-HTTP constructors
36+ :type converters: typing.List[base.Converter]
37+ """
38+ self .__converters = {c .TYPE : c for c in converters }
3039
31- def FromRequest (self , headers : dict , body : typing .IO ):
40+ def FromRequest (self , headers : dict ,
41+ body : typing .IO ,
42+ data_unmarshaller :
43+ typing .Callable ) -> event_base .BaseEvent :
44+ """
45+ Reads a CloudEvent from an HTTP headers and request body
46+ :param headers: a dict-like HTTP headers
47+ :type headers: dict
48+ :param body: a stream-like HTTP request body
49+ :type body: typing.IO
50+ :param data_unmarshaller: a callable-like
51+ unmarshaller the CloudEvent data
52+ :return: a CloudEvent
53+ :rtype: event_base.BaseEvent
54+ """
3255 mimeType = headers .get ("Content-Type" )
33- for cnvrtr in self .__converters :
56+ for _ , cnvrtr in self .__converters . items () :
3457 if cnvrtr .can_read (mimeType ):
35- return cnvrtr .read (headers , body )
58+ return cnvrtr .read (headers , body , data_unmarshaller )
3659
37- raise exceptions .InvalidMimeType (mimeType )
60+ raise exceptions .InvalidMimeTypeFromRequest (mimeType )
3861
3962 def ToRequest (self , event : event_base .BaseEvent ,
4063 converter_type : str ,
4164 data_marshaller : typing .Callable ) -> (dict , typing .IO ):
42- for cnvrtv in self .__converters :
43- if converter_type == cnvrtv .TYPE :
44- return cnvrtv .write (event , data_marshaller )
65+ """
66+ Writes a CloudEvent into a HTTP-ready form of headers and request body
67+ :param event: CloudEvent
68+ :type event: event_base.BaseEvent
69+ :param converter_type: a type of CloudEvent-to-HTTP converter
70+ :type converter_type: str
71+ :param data_marshaller: a callable-like marshaller CloudEvent data
72+ :type data_marshaller: typing.Callable
73+ :return: dict of HTTP headers and stream of HTTP request body
74+ :rtype: tuple
75+ """
76+ if converter_type in self .__converters :
77+ cnvrtr = self .__converters .get (converter_type )
78+ return cnvrtr .write (event , data_marshaller )
79+
80+ raise exceptions .NoSuchConverter (converter_type )
4581
4682
4783def NewDefaultHTTPMarshaller (
4884 event_class : event_base .BaseEvent ) -> HTTPMarshaller :
85+ """
86+ Creates the default HTTP marshaller with both structured
87+ and binary converters
88+ :param event_class: CloudEvent spec class
89+ :type event_class: event_base.BaseEvent
90+ :return: an instance of HTTP marshaller
91+ :rtype: cloudevents.sdk.marshaller.HTTPMarshaller
92+ """
4993 return HTTPMarshaller ([
5094 structured .NewJSONHTTPCloudEventConverter (event_class ),
5195 binary .NewBinaryHTTPCloudEventConverter (event_class ),
@@ -54,4 +98,12 @@ def NewDefaultHTTPMarshaller(
5498
5599def NewHTTPMarshaller (
56100 converters : typing .List [base .Converter ]) -> HTTPMarshaller :
101+ """
102+ Creates the default HTTP marshaller with both
103+ structured and binary converters
104+ :param converters: a list of CloudEvent-to-HTTP-to-CloudEvent converters
105+ :type converters: typing.List[base.Converter]
106+ :return: an instance of HTTP marshaller
107+ :rtype: cloudevents.sdk.marshaller.HTTPMarshaller
108+ """
57109 return HTTPMarshaller (converters )
0 commit comments