Skip to content

Commit db71318

Browse files
authored
feat: implement http bindings (#251)
* chore: fix uv lock warnings Signed-off-by: Tudor Plugaru <[email protected]> * feat: implement http bindings Signed-off-by: Tudor Plugaru <[email protected]> * chore: fix mypy in tests Signed-off-by: Tudor Plugaru <[email protected]> * chore: more mypy fixes Signed-off-by: Tudor Plugaru <[email protected]> * chore: another try Signed-off-by: Tudor Plugaru <[email protected]> * chore: switch from using `Dict` to `dict` for type hints Signed-off-by: Tudor Plugaru <[email protected]> * chore: switch type hints to use `|` rather than `Union` Signed-off-by: Tudor Plugaru <[email protected]> * refactor: simplify header normalization in from_http function Signed-off-by: Tudor Plugaru <[email protected]> * refactor: streamline header normalization in from_binary function Signed-off-by: Tudor Plugaru <[email protected]> * refactor: remove redundant comment in from_binary function Signed-off-by: Tudor Plugaru <[email protected]> --------- Signed-off-by: Tudor Plugaru <[email protected]>
1 parent 44b0496 commit db71318

File tree

11 files changed

+1633
-244
lines changed

11 files changed

+1633
-244
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
- id: mypy
1919
files: ^(src/cloudevents/|tests/)
2020
exclude: ^(src/cloudevents/v1/)
21-
types: [ python ]
22-
args: [
23-
"--config-file=pyproject.toml",
24-
]
21+
types: [python]
22+
args: ["--config-file=pyproject.toml"]
23+
additional_dependencies:
24+
- types-python-dateutil>=2.9.0.20241003

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ dependencies = [
4545
requires = ["hatchling"]
4646
build-backend = "hatchling.build"
4747

48-
[tool.uv]
49-
dev-dependencies = [
48+
[dependency-groups]
49+
dev = [
5050
"pytest>=8.3.3",
5151
"mypy>=1.11.2",
5252
"isort>=5.13.2",

src/cloudevents/core/base.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
1615
from datetime import datetime
17-
from typing import Any, Optional, Protocol, Union
16+
from typing import Any, Protocol
1817

1918

2019
class BaseCloudEvent(Protocol):
@@ -27,7 +26,9 @@ class BaseCloudEvent(Protocol):
2726
"""
2827

2928
def __init__(
30-
self, attributes: dict[str, Any], data: Optional[Union[dict, str, bytes]] = None
29+
self,
30+
attributes: dict[str, Any],
31+
data: dict[str, Any] | str | bytes | None = None,
3132
) -> None:
3233
"""
3334
Create a new CloudEvent instance.
@@ -72,31 +73,31 @@ def get_specversion(self) -> str:
7273
"""
7374
...
7475

75-
def get_datacontenttype(self) -> Optional[str]:
76+
def get_datacontenttype(self) -> str | None:
7677
"""
7778
Retrieve the datacontenttype of the event.
7879
7980
:return: The datacontenttype of the event.
8081
"""
8182
...
8283

83-
def get_dataschema(self) -> Optional[str]:
84+
def get_dataschema(self) -> str | None:
8485
"""
8586
Retrieve the dataschema of the event.
8687
8788
:return: The dataschema of the event.
8889
"""
8990
...
9091

91-
def get_subject(self) -> Optional[str]:
92+
def get_subject(self) -> str | None:
9293
"""
9394
Retrieve the subject of the event.
9495
9596
:return: The subject of the event.
9697
"""
9798
...
9899

99-
def get_time(self) -> Optional[datetime]:
100+
def get_time(self) -> datetime | None:
100101
"""
101102
Retrieve the time of the event.
102103
@@ -113,7 +114,7 @@ def get_extension(self, extension_name: str) -> Any:
113114
"""
114115
...
115116

116-
def get_data(self) -> Optional[Union[dict, str, bytes]]:
117+
def get_data(self) -> dict[str, Any] | str | bytes | None:
117118
"""
118119
Retrieve data of the event.
119120
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2018-Present The CloudEvents Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from cloudevents.core.bindings.http import (
16+
HTTPMessage,
17+
from_binary,
18+
from_http,
19+
from_structured,
20+
to_binary,
21+
to_structured,
22+
)
23+
24+
__all__ = [
25+
"HTTPMessage",
26+
"to_binary",
27+
"from_binary",
28+
"to_structured",
29+
"from_structured",
30+
"from_http",
31+
]

0 commit comments

Comments
 (0)