18
18
19
19
20
20
class EventGetterSetter (object ):
21
-
22
21
def CloudEventVersion (self ) -> str :
23
22
raise Exception ("not implemented" )
24
23
@@ -76,18 +75,13 @@ def SetContentType(self, contentType: str) -> object:
76
75
77
76
78
77
class BaseEvent (EventGetterSetter ):
79
-
80
78
def Properties (self , with_nullable = False ) -> dict :
81
79
props = dict ()
82
80
for name , value in self .__dict__ .items ():
83
81
if str (name ).startswith ("ce__" ):
84
82
v = value .get ()
85
83
if v is not None or with_nullable :
86
- props .update (
87
- {
88
- str (name ).replace ("ce__" , "" ): value .get ()
89
- }
90
- )
84
+ props .update ({str (name ).replace ("ce__" , "" ): value .get ()})
91
85
92
86
return props
93
87
@@ -119,33 +113,38 @@ def MarshalJSON(self, data_marshaller: typing.Callable) -> typing.IO:
119
113
props ["data" ] = data_marshaller (props .get ("data" ))
120
114
return io .BytesIO (json .dumps (props ).encode ("utf-8" ))
121
115
122
- def UnmarshalJSON (self , b : typing .IO ,
123
- data_unmarshaller : typing .Callable ):
116
+ def UnmarshalJSON (self , b : typing .IO , data_unmarshaller : typing .Callable ):
124
117
raw_ce = json .load (b )
125
118
for name , value in raw_ce .items ():
126
119
if name == "data" :
127
120
value = data_unmarshaller (value )
128
121
self .Set (name , value )
129
122
130
- def UnmarshalBinary (self , headers : dict , body : typing .IO ,
131
- data_unmarshaller : typing .Callable ):
132
- BINARY_MAPPING = {
133
- 'content-type' : 'contenttype' ,
123
+ def UnmarshalBinary (
124
+ self ,
125
+ headers : dict ,
126
+ body : typing .IO ,
127
+ data_unmarshaller : typing .Callable
128
+ ):
129
+ binary_mapping = {
130
+ "content-type" : "contenttype" ,
134
131
# TODO(someone): add Distributed Tracing. It's not clear
135
132
# if this is one extension or two.
136
133
# https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md
137
134
}
138
135
for header , value in headers .items ():
139
136
header = header .lower ()
140
- if header in BINARY_MAPPING :
141
- self .Set (BINARY_MAPPING [header ], value )
137
+ if header in binary_mapping :
138
+ self .Set (binary_mapping [header ], value )
142
139
elif header .startswith ("ce-" ):
143
140
self .Set (header [3 :], value )
144
141
145
142
self .Set ("data" , data_unmarshaller (body ))
146
143
147
144
def MarshalBinary (
148
- self , data_marshaller : typing .Callable ) -> (dict , object ):
145
+ self ,
146
+ data_marshaller : typing .Callable
147
+ ) -> (dict , object ):
149
148
headers = {}
150
149
if self .ContentType ():
151
150
headers ["content-type" ] = self .ContentType ()
@@ -159,5 +158,4 @@ def MarshalBinary(
159
158
headers ["ce-{0}" .format (key )] = value
160
159
161
160
data , _ = self .Get ("data" )
162
- return headers , io .BytesIO (
163
- str (data_marshaller (data )).encode ("utf-8" ))
161
+ return headers , data_marshaller (data )
0 commit comments