Skip to content

Commit d8cec17

Browse files
committed
Adding event_supported to converters
- this method validates whether a converter can process the event, in case of structured converter this method does nothing, in case of binary converter this method raise an exception if an event is not v0.2 - adding (un)marshaller validation on being a callable object - tests added Signed-off-by: Denis Makogon <denys.makogon@oracle.com>
1 parent 9357beb commit d8cec17

File tree

6 files changed

+59
-9
lines changed

6 files changed

+59
-9
lines changed

cloudevents/sdk/converters/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def read(self, event, headers: dict, body: typing.IO,
2525
data_unmarshaller: typing.Callable) -> base.BaseEvent:
2626
raise Exception("not implemented")
2727

28+
def event_supported(self, event):
29+
raise Exception("not implemented")
30+
2831
def can_read(self, content_type):
2932
raise Exception("not implemented")
3033

cloudevents/sdk/converters/binary.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class BinaryHTTPCloudEventConverter(base.Converter):
2828
def can_read(self, content_type):
2929
return True
3030

31+
def event_supported(self, event):
32+
if type(event) not in self.SUPPORTED_VERSIONS:
33+
raise exceptions.UnsupportedEvent(type(event))
34+
3135
def read(self,
3236
event: event_base.BaseEvent,
3337
headers: dict, body: typing.IO,
@@ -39,9 +43,6 @@ def read(self,
3943

4044
def write(self, event: event_base.BaseEvent,
4145
data_marshaller: typing.Callable) -> (dict, typing.IO):
42-
if not isinstance(data_marshaller, typing.Callable):
43-
raise exceptions.InvalidDataMarshaller()
44-
4546
return event.MarshalBinary(data_marshaller)
4647

4748

cloudevents/sdk/converters/structured.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import typing
1616

17-
from cloudevents.sdk import exceptions
1817
from cloudevents.sdk.converters import base
1918
from cloudevents.sdk.event import base as event_base
2019

@@ -26,6 +25,10 @@ class JSONHTTPCloudEventConverter(base.Converter):
2625
def can_read(self, content_type):
2726
return content_type == "application/cloudevents+json"
2827

28+
def event_supported(self, event):
29+
# structured format supported by both spec 0.1 and 0.2
30+
pass
31+
2932
def read(self, event: event_base.BaseEvent,
3033
headers: dict,
3134
body: typing.IO,
@@ -36,9 +39,6 @@ def read(self, event: event_base.BaseEvent,
3639
def write(self,
3740
event: event_base.BaseEvent,
3841
data_marshaller: typing.Callable) -> (dict, typing.IO):
39-
if not isinstance(data_marshaller, typing.Callable):
40-
raise exceptions.InvalidDataMarshaller()
41-
4242
return {}, event.MarshalJSON(data_marshaller)
4343

4444

cloudevents/sdk/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def __init__(self, event_class):
2020
"'{0}'".format(event_class))
2121

2222

23+
class InvalidDataUnmarshaller(Exception):
24+
25+
def __init__(self):
26+
super().__init__(
27+
"Invalid data unmarshaller, is not a callable")
28+
29+
2330
class InvalidDataMarshaller(Exception):
2431

2532
def __init__(self):

cloudevents/sdk/marshaller.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ def FromRequest(self, event: event_base.BaseEvent,
5555
:return: a CloudEvent
5656
:rtype: event_base.BaseEvent
5757
"""
58+
if not isinstance(data_unmarshaller, typing.Callable):
59+
raise exceptions.InvalidDataUnmarshaller()
60+
5861
content_type = headers.get(
5962
"content-type", headers.get("Content-Type"))
6063

6164
for _, cnvrtr in self.__converters.items():
6265
if cnvrtr.can_read(content_type):
66+
cnvrtr.event_supported(event)
6367
return cnvrtr.read(event, headers, body, data_unmarshaller)
6468

6569
raise exceptions.UnsupportedEventConverter(content_type)
@@ -78,6 +82,9 @@ def ToRequest(self, event: event_base.BaseEvent,
7882
:return: dict of HTTP headers and stream of HTTP request body
7983
:rtype: tuple
8084
"""
85+
if not isinstance(data_marshaller, typing.Callable):
86+
raise exceptions.InvalidDataMarshaller()
87+
8188
if converter_type in self.__converters:
8289
cnvrtr = self.__converters.get(converter_type)
8390
return cnvrtr.write(event, data_marshaller)

cloudevents/tests/test_event_from_request_converter.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_binary_converter_v01():
6868
pytest.raises(
6969
exceptions.UnsupportedEvent,
7070
m.FromRequest,
71-
v01.Event, {}, None, None)
71+
v01.Event, {}, None, lambda x: x)
7272

7373

7474
def test_unsupported_converter_v01():
@@ -81,7 +81,7 @@ def test_unsupported_converter_v01():
8181
pytest.raises(
8282
exceptions.UnsupportedEventConverter,
8383
m.FromRequest,
84-
v01.Event, {}, None, None)
84+
v01.Event, {}, None, lambda x: x)
8585

8686

8787
def test_structured_converter_v01():
@@ -114,3 +114,35 @@ def test_default_http_marshaller():
114114
assert event is not None
115115
assert event.Get("type") == (data.ce_type, True)
116116
assert event.Get("id") == (data.ce_id, True)
117+
118+
119+
def test_unsupported_event_configuration():
120+
m = marshaller.NewHTTPMarshaller(
121+
[
122+
binary.NewBinaryHTTPCloudEventConverter()
123+
]
124+
)
125+
pytest.raises(
126+
exceptions.UnsupportedEvent,
127+
m.FromRequest,
128+
v01.Event(),
129+
{"Content-Type": "application/cloudevents+json"},
130+
io.StringIO(json.dumps(data.ce)),
131+
lambda x: x.read()
132+
)
133+
134+
135+
def test_invalid_data_unmarshaller():
136+
m = marshaller.NewDefaultHTTPMarshaller()
137+
pytest.raises(
138+
exceptions.InvalidDataUnmarshaller,
139+
m.FromRequest,
140+
v01.Event(), {}, None, None)
141+
142+
143+
def test_invalid_data_marshaller():
144+
m = marshaller.NewDefaultHTTPMarshaller()
145+
pytest.raises(
146+
exceptions.InvalidDataMarshaller,
147+
m.ToRequest,
148+
v01.Event(), "blah", None)

0 commit comments

Comments
 (0)