Skip to content

Added Cloudevents V0.3 and V1 implementations #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Added Cloudevents V0.3 and V1 implementations ([#22])
- Add helpful text to README ([#23])
- Add link to email in README ([#27])

Expand Down Expand Up @@ -61,6 +62,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#15]: https://github.com/cloudevents/sdk-python/pull/15
[#17]: https://github.com/cloudevents/sdk-python/pull/17
[#21]: https://github.com/cloudevents/sdk-python/pull/21
[#22]: https://github.com/cloudevents/sdk-python/pull/22
[#23]: https://github.com/cloudevents/sdk-python/pull/23
[#25]: https://github.com/cloudevents/sdk-python/pull/25
[#27]: https://github.com/cloudevents/sdk-python/pull/27
4 changes: 2 additions & 2 deletions cloudevents/sdk/converters/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from cloudevents.sdk import exceptions
from cloudevents.sdk.converters import base
from cloudevents.sdk.event import base as event_base
from cloudevents.sdk.event import v02
from cloudevents.sdk.event import v02, v03, v1


class BinaryHTTPCloudEventConverter(base.Converter):

TYPE = "binary"
SUPPORTED_VERSIONS = [v02.Event]
SUPPORTED_VERSIONS = [v02.Event, v03.Event, v1.Event]

def can_read(self, content_type: str) -> bool:
return True
Expand Down
12 changes: 4 additions & 8 deletions cloudevents/sdk/event/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import typing


# TODO(slinkydeveloper) is this really needed?
class EventGetterSetter(object):

def CloudEventVersion(self) -> str:
raise Exception("not implemented")

Expand Down Expand Up @@ -126,16 +128,10 @@ def UnmarshalBinary(
body: typing.IO,
data_unmarshaller: typing.Callable
):
binary_mapping = {
"content-type": "contenttype",
# TODO(someone): add Distributed Tracing. It's not clear
# if this is one extension or two.
# https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md
}
for header, value in headers.items():
header = header.lower()
if header in binary_mapping:
self.Set(binary_mapping[header], value)
if header == "content-type":
self.SetContentType(value)
elif header.startswith("ce-"):
self.Set(header[3:], value)

Expand Down
109 changes: 109 additions & 0 deletions cloudevents/sdk/event/v03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from cloudevents.sdk.event import base
from cloudevents.sdk.event import opt


class Event(base.BaseEvent):
def __init__(self):
self.ce__specversion = opt.Option("specversion", "0.3", True)
self.ce__id = opt.Option("id", None, True)
self.ce__source = opt.Option("source", None, True)
self.ce__type = opt.Option("type", None, True)

self.ce__datacontenttype = opt.Option("datacontenttype", None, False)
self.ce__datacontentencoding = opt.Option(
"datacontentencoding",
None,
False
)
self.ce__subject = opt.Option("subject", None, False)
self.ce__time = opt.Option("time", None, False)
self.ce__schemaurl = opt.Option("schemaurl", None, False)
self.ce__data = opt.Option("data", None, False)
self.ce__extensions = opt.Option("extensions", dict(), False)

def CloudEventVersion(self) -> str:
return self.ce__specversion.get()

def EventType(self) -> str:
return self.ce__type.get()

def Source(self) -> str:
return self.ce__source.get()

def EventID(self) -> str:
return self.ce__id.get()

def EventTime(self) -> str:
return self.ce__time.get()

def Subject(self) -> str:
return self.ce__subject.get()

def SchemaURL(self) -> str:
return self.ce__schemaurl.get()

def Data(self) -> object:
return self.ce__data.get()

def Extensions(self) -> dict:
return self.ce__extensions.get()

def ContentType(self) -> str:
return self.ce__datacontenttype.get()

def ContentEncoding(self) -> str:
return self.ce__datacontentencoding.get()

def SetEventType(self, eventType: str) -> base.BaseEvent:
self.Set("type", eventType)
return self

def SetSource(self, source: str) -> base.BaseEvent:
self.Set("source", source)
return self

def SetEventID(self, eventID: str) -> base.BaseEvent:
self.Set("id", eventID)
return self

def SetEventTime(self, eventTime: str) -> base.BaseEvent:
self.Set("time", eventTime)
return self

def SetSubject(self, subject: str) -> base.BaseEvent:
self.Set("subject", subject)
return self

def SetSchemaURL(self, schemaURL: str) -> base.BaseEvent:
self.Set("schemaurl", schemaURL)
return self

def SetData(self, data: object) -> base.BaseEvent:
self.Set("data", data)
return self

def SetExtensions(self, extensions: dict) -> base.BaseEvent:
self.Set("extensions", extensions)
return self

def SetContentType(self, contentType: str) -> base.BaseEvent:
self.Set("datacontenttype", contentType)
return self

def SetContentEncoding(self, contentEncoding: str) -> base.BaseEvent:
self.Set("datacontentencoding", contentEncoding)
return self
97 changes: 97 additions & 0 deletions cloudevents/sdk/event/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from cloudevents.sdk.event import base
from cloudevents.sdk.event import opt


class Event(base.BaseEvent):
def __init__(self):
self.ce__specversion = opt.Option("specversion", "1.0", True)
self.ce__id = opt.Option("id", None, True)
self.ce__source = opt.Option("source", None, True)
self.ce__type = opt.Option("type", None, True)

self.ce__datacontenttype = opt.Option("datacontenttype", None, False)
self.ce__dataschema = opt.Option("dataschema", None, False)
self.ce__subject = opt.Option("subject", None, False)
self.ce__time = opt.Option("time", None, False)
self.ce__data = opt.Option("data", None, False)
self.ce__extensions = opt.Option("extensions", dict(), False)

def CloudEventVersion(self) -> str:
return self.ce__specversion.get()

def EventType(self) -> str:
return self.ce__type.get()

def Source(self) -> str:
return self.ce__source.get()

def EventID(self) -> str:
return self.ce__id.get()

def EventTime(self) -> str:
return self.ce__time.get()

def Subject(self) -> str:
return self.ce__subject.get()

def Schema(self) -> str:
return self.ce__dataschema.get()

def ContentType(self) -> str:
return self.ce__datacontenttype.get()

def Data(self) -> object:
return self.ce__data.get()

def Extensions(self) -> dict:
return self.ce__extensions.get()

def SetEventType(self, eventType: str) -> base.BaseEvent:
self.Set("type", eventType)
return self

def SetSource(self, source: str) -> base.BaseEvent:
self.Set("source", source)
return self

def SetEventID(self, eventID: str) -> base.BaseEvent:
self.Set("id", eventID)
return self

def SetEventTime(self, eventTime: str) -> base.BaseEvent:
self.Set("time", eventTime)
return self

def SetSubject(self, subject: str) -> base.BaseEvent:
self.Set("subject", subject)
return self

def SetSchema(self, schema: str) -> base.BaseEvent:
self.Set("dataschema", schema)
return self

def SetContentType(self, contentType: str) -> base.BaseEvent:
self.Set("datacontenttype", contentType)
return self

def SetData(self, data: object) -> base.BaseEvent:
self.Set("data", data)
return self

def SetExtensions(self, extensions: dict) -> base.BaseEvent:
self.Set("extensions", extensions)
return self
4 changes: 3 additions & 1 deletion cloudevents/sdk/marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def FromRequest(
if not isinstance(data_unmarshaller, typing.Callable):
raise exceptions.InvalidDataUnmarshaller()

content_type = headers.get("content-type", headers.get("Content-Type"))
# Lower all header keys
headers = {key.lower(): value for key, value in headers.items()}
content_type = headers.get("content-type", None)

for cnvrtr in self.__converters:
if cnvrtr.can_read(content_type) and cnvrtr.event_supported(event):
Expand Down
67 changes: 53 additions & 14 deletions cloudevents/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,65 @@
# License for the specific language governing permissions and limitations
# under the License.

from cloudevents.sdk.event import v02, v03, v1

contentType = "application/json"
ce_type = "word.found.exclamation"
ce_id = "16fb5f0b-211e-1102-3dfe-ea6e2806f124"
source = "pytest"
specversion = "0.2"
eventTime = "2018-10-23T12:28:23.3464579Z"
body = '{"name":"john"}'

headers = {
"ce-specversion": specversion,
"ce-type": ce_type,
"ce-id": ce_id,
"ce-time": eventTime,
"ce-source": source,
"Content-Type": contentType,
v02.Event: {
"ce-specversion": "0.2",
"ce-type": ce_type,
"ce-id": ce_id,
"ce-time": eventTime,
"ce-source": source,
"Content-Type": contentType,
},
v03.Event: {
"ce-specversion": "0.3",
"ce-type": ce_type,
"ce-id": ce_id,
"ce-time": eventTime,
"ce-source": source,
"Content-Type": contentType,
},
v1.Event: {
"ce-specversion": "1.0",
"ce-type": ce_type,
"ce-id": ce_id,
"ce-time": eventTime,
"ce-source": source,
"Content-Type": contentType,
},
}
ce = {
"specversion": specversion,
"type": ce_type,
"id": ce_id,
"time": eventTime,
"source": source,
"contenttype": contentType,

json_ce = {
v02.Event: {
"specversion": "0.2",
"type": ce_type,
"id": ce_id,
"time": eventTime,
"source": source,
"contenttype": contentType,
},
v03.Event: {
"specversion": "0.3",
"type": ce_type,
"id": ce_id,
"time": eventTime,
"source": source,
"datacontenttype": contentType,
},
v1.Event: {
"specversion": "1.0",
"type": ce_type,
"id": ce_id,
"time": eventTime,
"source": source,
"datacontenttype": contentType,
},
}
Loading