Skip to content
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

cloudevents version 1.0.1 release #102

Merged
merged 5 commits into from
Aug 13, 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
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1]
### Added
- CloudEvent exceptions and event type checking in http module ([#96])
- CloudEvent equality override ([#98])

## [1.0.0]
### Added
- Update types and handle data_base64 structured ([#34])
- Added a user friendly CloudEvent class with data validation ([#36])
- CloudEvent structured cloudevent support ([#47])
- Separated http methods into cloudevents.http module ([#60])
- Implemented to_json and from_json in http module ([#72])

### Fixed
- Fixed top level extensions bug ([#71])

### Removed
- Removed support for Cloudevents V0.2 and V0.1 ([#43])
Expand Down Expand Up @@ -74,6 +85,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#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
[#34]: https://github.com/cloudevents/sdk-python/pull/34
[#36]: https://github.com/cloudevents/sdk-python/pull/36
[#43]: https://github.com/cloudevents/sdk-python/pull/43
[#47]: https://github.com/cloudevents/sdk-python/pull/47
[#60]: https://github.com/cloudevents/sdk-python/pull/60
[#71]: https://github.com/cloudevents/sdk-python/pull/71
[#72]: https://github.com/cloudevents/sdk-python/pull/72
[#96]: https://github.com/cloudevents/sdk-python/pull/96
[#98]: https://github.com/cloudevents/sdk-python/pull/98
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ requests.post("<some-url>", data=body, headers=headers)

You can find a complete example of turning a CloudEvent into a HTTP request [in the samples directory](samples/http-json-cloudevents/client.py).

#### Request to CloudEvent
## Receiving CloudEvents

The code below shows how to consume a cloudevent using the popular python web framework
[flask](https://flask.palletsprojects.com/en/1.1.x/quickstart/):
Expand Down Expand Up @@ -120,6 +120,17 @@ the same API. It will use semantic versioning with following rules:
- Email: https://lists.cncf.io/g/cncf-cloudevents-sdk
- Contact for additional information: Denis Makogon (`@denysmakogon` on slack).

Each SDK may have its own unique processes, tooling and guidelines, common
governance related material can be found in the
[CloudEvents `community`](https://github.com/cloudevents/spec/tree/master/community)
directory. In particular, in there you will find information concerning
how SDK projects are
[managed](https://github.com/cloudevents/spec/blob/master/community/SDK-GOVERNANCE.md),
[guidelines](https://github.com/cloudevents/spec/blob/master/community/SDK-maintainer-guidelines.md)
for how PR reviews and approval, and our
[Code of Conduct](https://github.com/cloudevents/spec/blob/master/community/GOVERNANCE.md#additional-information)
information.

## Maintenance

We use black and isort for autoformatting. We setup a tox environment to reformat
Expand Down
2 changes: 1 addition & 1 deletion cloudevents/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.0"
__version__ = "1.0.1"
19 changes: 19 additions & 0 deletions cloudevents/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.
class CloudEventMissingRequiredFields(Exception):
pass


class CloudEventTypeErrorRequiredFields(Exception):
pass
1 change: 1 addition & 0 deletions cloudevents/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import typing

from cloudevents.http.event import CloudEvent
from cloudevents.http.event_type import is_binary, is_structured
from cloudevents.http.http_methods import (
from_http,
to_binary_http,
Expand Down
8 changes: 6 additions & 2 deletions cloudevents/http/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import typing
import uuid

import cloudevents.exceptions as cloud_exceptions
from cloudevents.http.mappings import _required_by_version


Expand Down Expand Up @@ -57,17 +58,20 @@ def __init__(
).isoformat()

if self._attributes["specversion"] not in _required_by_version:
raise ValueError(
raise cloud_exceptions.CloudEventMissingRequiredFields(
f"Invalid specversion: {self._attributes['specversion']}"
)
# There is no good way to default 'source' and 'type', so this
# checks for those (or any new required attributes).
required_set = _required_by_version[self._attributes["specversion"]]
if not required_set <= self._attributes.keys():
raise ValueError(
raise cloud_exceptions.CloudEventMissingRequiredFields(
f"Missing required keys: {required_set - attributes.keys()}"
)

def __eq__(self, other):
return self.data == other.data and self._attributes == other._attributes

# Data access is handled via `.data` member
# Attribute access is managed via Mapping type
def __getitem__(self, key):
Expand Down
29 changes: 29 additions & 0 deletions cloudevents/http/event_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import typing

from cloudevents.sdk.converters import binary, structured


def is_binary(headers: typing.Dict[str, str]) -> bool:
"""Uses internal marshallers to determine whether this event is binary
:param headers: the HTTP headers
:type headers: typing.Dict[str, str]
:returns bool: returns a bool indicating whether the headers indicate a binary event type
"""
headers = {key.lower(): value for key, value in headers.items()}
content_type = headers.get("content-type", "")
binary_parser = binary.BinaryHTTPCloudEventConverter()
return binary_parser.can_read(content_type=content_type, headers=headers)


def is_structured(headers: typing.Dict[str, str]) -> bool:
"""Uses internal marshallers to determine whether this event is structured
:param headers: the HTTP headers
:type headers: typing.Dict[str, str]
:returns bool: returns a bool indicating whether the headers indicate a structured event type
"""
headers = {key.lower(): value for key, value in headers.items()}
content_type = headers.get("content-type", "")
structured_parser = structured.JSONHTTPCloudEventConverter()
return structured_parser.can_read(
content_type=content_type, headers=headers
)
14 changes: 10 additions & 4 deletions cloudevents/http/http_methods.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import typing

import cloudevents.exceptions as cloud_exceptions
from cloudevents.http.event import CloudEvent
from cloudevents.http.event_type import is_binary, is_structured
from cloudevents.http.mappings import _marshaller_by_format, _obj_by_version
from cloudevents.http.util import _json_or_string
from cloudevents.sdk import converters, marshaller, types
Expand All @@ -27,19 +29,23 @@ def from_http(

marshall = marshaller.NewDefaultHTTPMarshaller()

if converters.is_binary(headers):
if is_binary(headers):
specversion = headers.get("ce-specversion", None)
else:
raw_ce = json.loads(data)
specversion = raw_ce.get("specversion", None)

if specversion is None:
raise ValueError("could not find specversion in HTTP request")
raise cloud_exceptions.CloudEventMissingRequiredFields(
"could not find specversion in HTTP request"
)

event_handler = _obj_by_version.get(specversion, None)

if event_handler is None:
raise ValueError(f"found invalid specversion {specversion}")
raise cloud_exceptions.CloudEventTypeErrorRequiredFields(
f"found invalid specversion {specversion}"
)

event = marshall.FromRequest(
event_handler(), headers, data, data_unmarshaller=data_unmarshaller
Expand Down Expand Up @@ -71,7 +77,7 @@ def _to_http(
data_marshaller = _marshaller_by_format[format]

if event._attributes["specversion"] not in _obj_by_version:
raise ValueError(
raise cloud_exceptions.CloudEventTypeErrorRequiredFields(
f"Unsupported specversion: {event._attributes['specversion']}"
)

Expand Down
29 changes: 0 additions & 29 deletions cloudevents/sdk/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import typing

from cloudevents.sdk.converters import binary, structured

TypeBinary = binary.BinaryHTTPCloudEventConverter.TYPE
TypeStructured = structured.JSONHTTPCloudEventConverter.TYPE


def is_binary(headers: typing.Dict[str, str]) -> bool:
"""Uses internal marshallers to determine whether this event is binary
:param headers: the HTTP headers
:type headers: typing.Dict[str, str]
:returns bool: returns a bool indicating whether the headers indicate a binary event type
"""
headers = {key.lower(): value for key, value in headers.items()}
content_type = headers.get("content-type", "")
binary_parser = binary.BinaryHTTPCloudEventConverter()
return binary_parser.can_read(content_type=content_type, headers=headers)


def is_structured(headers: typing.Dict[str, str]) -> bool:
"""Uses internal marshallers to determine whether this event is structured
:param headers: the HTTP headers
:type headers: typing.Dict[str, str]
:returns bool: returns a bool indicating whether the headers indicate a structured event type
"""
headers = {key.lower(): value for key, value in headers.items()}
content_type = headers.get("content-type", "")
structured_parser = structured.JSONHTTPCloudEventConverter()
return structured_parser.can_read(
content_type=content_type, headers=headers
)
10 changes: 4 additions & 6 deletions cloudevents/sdk/converters/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from cloudevents.sdk import exceptions, types
from cloudevents.sdk.converters import base
from cloudevents.sdk.converters.structured import JSONHTTPCloudEventConverter
from cloudevents.sdk.converters.util import has_binary_headers
from cloudevents.sdk.event import base as event_base
from cloudevents.sdk.event import v1, v03

Expand All @@ -28,13 +28,11 @@ class BinaryHTTPCloudEventConverter(base.Converter):

def can_read(
self,
content_type: str,
content_type: str = None,
headers: typing.Dict[str, str] = {"ce-specversion": None},
) -> bool:
return ("ce-specversion" in headers) and not (
isinstance(content_type, str)
and content_type.startswith(JSONHTTPCloudEventConverter.MIME_TYPE)
)

return has_binary_headers(headers)

def event_supported(self, event: object) -> bool:
return type(event) in self.SUPPORTED_VERSIONS
Expand Down
9 changes: 5 additions & 4 deletions cloudevents/sdk/converters/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@

from cloudevents.sdk import types
from cloudevents.sdk.converters import base
from cloudevents.sdk.converters.util import has_binary_headers
from cloudevents.sdk.event import base as event_base


# TODO: Singleton?
class JSONHTTPCloudEventConverter(base.Converter):

TYPE = "structured"
MIME_TYPE = "application/cloudevents+json"

def can_read(
self,
content_type: str,
headers: typing.Dict[str, str] = {"ce-specversion": None},
self, content_type: str, headers: typing.Dict[str, str] = {},
) -> bool:
return (
isinstance(content_type, str)
and content_type.startswith(self.MIME_TYPE)
) or ("ce-specversion" not in headers)
or not has_binary_headers(headers)
)

def event_supported(self, event: object) -> bool:
# structured format supported by both spec 0.1 and 0.2
Expand Down
10 changes: 10 additions & 0 deletions cloudevents/sdk/converters/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typing


def has_binary_headers(headers: typing.Dict[str, str]) -> bool:
return (
"ce-specversion" in headers
and "ce-source" in headers
and "ce-type" in headers
and "ce-id" in headers
)
26 changes: 19 additions & 7 deletions cloudevents/sdk/event/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import json
import typing

import cloudevents.exceptions as cloud_exceptions
from cloudevents.sdk import types


# TODO(slinkydeveloper) is this really needed?


class EventGetterSetter(object):

# ce-specversion
Expand Down Expand Up @@ -159,6 +161,9 @@ def content_type(self, value: str):


class BaseEvent(EventGetterSetter):
_ce_required_fields = set()
_ce_optional_fields = set()

def Properties(self, with_nullable=False) -> dict:
props = dict()
for name, value in self.__dict__.items():
Expand Down Expand Up @@ -215,7 +220,9 @@ def UnmarshalJSON(

missing_fields = self._ce_required_fields - raw_ce.keys()
if len(missing_fields) > 0:
raise ValueError(f"Missing required attributes: {missing_fields}")
raise cloud_exceptions.CloudEventMissingRequiredFields(
f"Missing required attributes: {missing_fields}"
)

for name, value in raw_ce.items():
if name == "data":
Expand All @@ -233,18 +240,23 @@ def UnmarshalBinary(
body: typing.Union[bytes, str],
data_unmarshaller: types.UnmarshallerType,
):
if "ce-specversion" not in headers:
raise ValueError("Missing required attribute: 'specversion'")
required_binary_fields = {
f"ce-{field}" for field in self._ce_required_fields
}
missing_fields = required_binary_fields - headers.keys()

if len(missing_fields) > 0:
raise cloud_exceptions.CloudEventMissingRequiredFields(
f"Missing required attributes: {missing_fields}"
)

for header, value in headers.items():
header = header.lower()
if header == "content-type":
self.SetContentType(value)
elif header.startswith("ce-"):
self.Set(header[3:], value)
self.Set("data", data_unmarshaller(body))
missing_attrs = self._ce_required_fields - self.Properties().keys()
if len(missing_attrs) > 0:
raise ValueError(f"Missing required attributes: {missing_attrs}")

def MarshalBinary(
self, data_marshaller: types.MarshallerType
Expand Down
Loading