Skip to content

Commit 6648eb5

Browse files
authored
Feat/expose event attributes (cloudevents#195)
* feat: Add an API to read all event attributes Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com> * deps: update black version Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com> * chore: update version to v1.6.2 Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com> * docs: update changelog Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com> * docs: fix the release number link Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com> Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>
1 parent 60f848a commit 6648eb5

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
- id: isort
1212
args: [ "--profile", "black", "--filter-files" ]
1313
- repo: https://github.com/psf/black
14-
rev: 22.8.0
14+
rev: 22.10.0
1515
hooks:
1616
- id: black
1717
language_version: python3.10

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.6.2] — 2022-10-18
10+
### Added
11+
- Added `get_attributes` API to the `CloudEvent` API. The method returns a read-only
12+
view on the event attributes. ([#195])
13+
914
## [1.6.1] — 2022-08-18
1015
### Fixed
1116
- Missing `to_json` import. ([#191])
@@ -146,6 +151,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
146151
## [0.0.1] - 2018-11-19
147152
### Added
148153
- Initial release
154+
155+
[1.6.2]: https://github.com/cloudevents/sdk-python/compare/1.6.1...1.6.2
149156
[1.6.1]: https://github.com/cloudevents/sdk-python/compare/1.6.0...1.6.1
150157
[1.6.0]: https://github.com/cloudevents/sdk-python/compare/1.5.0...1.6.0
151158
[1.5.0]: https://github.com/cloudevents/sdk-python/compare/1.4.0...1.5.0
@@ -210,3 +217,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
210217
[#186]: https://github.com/cloudevents/sdk-python/pull/186
211218
[#188]: https://github.com/cloudevents/sdk-python/pull/188
212219
[#191]: https://github.com/cloudevents/sdk-python/pull/191
220+
[#195]: https://github.com/cloudevents/sdk-python/pull/195

cloudevents/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
__version__ = "1.6.1"
15+
__version__ = "1.6.2"

cloudevents/abstract/event.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import typing
1616
from abc import abstractmethod
17+
from types import MappingProxyType
18+
from typing import Mapping
1719

1820

1921
class CloudEvent:
@@ -45,6 +47,14 @@ def create(
4547
"""
4648
raise NotImplementedError()
4749

50+
def get_attributes(self) -> Mapping[str, typing.Any]:
51+
"""
52+
Returns a read-only view on the attributes of the event.
53+
54+
:returns: Read-only view on the attributes of the event.
55+
"""
56+
return MappingProxyType(self._get_attributes())
57+
4858
@abstractmethod
4959
def _get_attributes(self) -> typing.Dict[str, typing.Any]:
5060
"""

cloudevents/tests/test_http_events.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import bz2
1616
import io
1717
import json
18+
import typing
1819

1920
import pytest
2021
from sanic import Sanic, response
@@ -83,7 +84,6 @@ async def echo(request):
8384
@pytest.mark.parametrize("body", invalid_cloudevent_request_body)
8485
def test_missing_required_fields_structured(body):
8586
with pytest.raises(cloud_exceptions.MissingRequiredFields):
86-
8787
_ = from_http(
8888
{"Content-Type": "application/cloudevents+json"}, json.dumps(body)
8989
)
@@ -188,7 +188,6 @@ def test_missing_ce_prefix_binary_event(specversion):
188188
"ce-specversion": specversion,
189189
}
190190
for key in headers:
191-
192191
# breaking prefix e.g. e-id instead of ce-id
193192
prefixed_headers[key[1:]] = headers[key]
194193

@@ -245,6 +244,25 @@ def test_structured_to_request(specversion):
245244
assert body["data"] == data, f"|{body_bytes}|| {body}"
246245

247246

247+
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
248+
def test_attributes_view_accessor(specversion: str):
249+
attributes: dict[str, typing.Any] = {
250+
"specversion": specversion,
251+
"type": "word.found.name",
252+
"id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
253+
"source": "pytest",
254+
}
255+
data = {"message": "Hello World!"}
256+
257+
event: CloudEvent = CloudEvent(attributes, data)
258+
event_attributes: typing.Mapping[str, typing.Any] = event.get_attributes()
259+
assert event_attributes["specversion"] == attributes["specversion"]
260+
assert event_attributes["type"] == attributes["type"]
261+
assert event_attributes["id"] == attributes["id"]
262+
assert event_attributes["source"] == attributes["source"]
263+
assert event_attributes["time"]
264+
265+
248266
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
249267
def test_binary_to_request(specversion):
250268
attributes = {

cloudevents/tests/test_pydantic_events.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import bz2
1616
import io
1717
import json
18+
import typing
1819

1920
import pytest
2021
from sanic import Sanic, response
@@ -242,6 +243,25 @@ def test_structured_to_request(specversion):
242243
assert body["data"] == data, f"|{body_bytes}|| {body}"
243244

244245

246+
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
247+
def test_attributes_view_accessor(specversion: str):
248+
attributes: dict[str, typing.Any] = {
249+
"specversion": specversion,
250+
"type": "word.found.name",
251+
"id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
252+
"source": "pytest",
253+
}
254+
data = {"message": "Hello World!"}
255+
256+
event: CloudEvent = CloudEvent(attributes, data)
257+
event_attributes: typing.Mapping[str, typing.Any] = event.get_attributes()
258+
assert event_attributes["specversion"] == attributes["specversion"]
259+
assert event_attributes["type"] == attributes["type"]
260+
assert event_attributes["id"] == attributes["id"]
261+
assert event_attributes["source"] == attributes["source"]
262+
assert event_attributes["time"]
263+
264+
245265
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
246266
def test_binary_to_request(specversion):
247267
attributes = {

0 commit comments

Comments
 (0)