Skip to content

Commit 72ced43

Browse files
committed
bump cloudevents to 2.*
1 parent cd63dbd commit 72ced43

6 files changed

Lines changed: 100 additions & 70 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1010

1111
### Added
1212
### Changed
13+
14+
- Bumping cloudevents dep to 2.0
15+
1316
### Deprecated
1417
### Removed
1518
### Fixed

‎cmd/fcloudevent/main.py‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import argparse
22
import logging
33
from func_python.cloudevent import serve
4-
from cloudevents.http import CloudEvent
4+
from cloudevents.core.v1.event import CloudEvent
55

66
# Set the default logging level to INFO
77
logging.basicConfig(level=logging.INFO)
@@ -30,30 +30,30 @@ async def handle(scope, receive, send):
3030
event = scope["event"]
3131
if not event:
3232
error_event = CloudEvent(
33-
{"type": "dev.functions.error", "source": "/fcloudevent/error"},
34-
{"message": "No CloudEvent found in scope"}
33+
attributes={"type": "dev.functions.error", "source": "/fcloudevent/error"},
34+
data={"message": "No CloudEvent found in scope"}
3535
)
3636
await send(error_event, 500)
3737
return
38-
39-
logging.info(f"Received CloudEvent: type={event['type']}, source={event['source']}")
40-
38+
39+
logging.info(f"Received CloudEvent: type={event.get_type()}, source={event.get_source()}")
40+
4141
# Handle event data - it might be bytes or dict
42-
event_data = event.data
42+
event_data = event.get_data()
4343
if isinstance(event_data, bytes):
4444
import json
4545
event_data = json.loads(event_data)
4646
logging.info(f"CloudEvent data: {event_data}")
47-
47+
4848
response_event = CloudEvent(
49-
{
49+
attributes={
5050
"type": "com.example.response.static",
5151
"source": "/fcloudevent/static",
5252
},
53-
{
53+
data={
5454
"message": "OK: static CloudEvent handler",
55-
"received_event_type": event['type'],
56-
"received_event_source": event['source'],
55+
"received_event_type": event.get_type(),
56+
"received_event_source": event.get_source(),
5757
"received_data": event_data
5858
}
5959
)
@@ -79,34 +79,34 @@ async def handle(self, scope, receive, send):
7979
if not event:
8080
# This shouldn't happen with our fix, but let's handle it gracefully
8181
error_event = CloudEvent(
82-
{"type": "dev.functions.error", "source": "/fcloudevent/error"},
83-
{"message": "No CloudEvent found in scope"}
82+
attributes={"type": "dev.functions.error", "source": "/fcloudevent/error"},
83+
data={"message": "No CloudEvent found in scope"}
8484
)
8585
await send(error_event, 500)
8686
return
87-
87+
8888
self.event_count += 1
89-
90-
logging.info(f"Received CloudEvent #{self.event_count}: type={event['type']}, source={event['source']}")
91-
89+
90+
logging.info(f"Received CloudEvent #{self.event_count}: type={event.get_type()}, source={event.get_source()}")
91+
9292
# Handle event data - it might be bytes or dict
93-
event_data = event.data
93+
event_data = event.get_data()
9494
if isinstance(event_data, bytes):
9595
import json
9696
event_data = json.loads(event_data)
9797
logging.info(f"CloudEvent data: {event_data}")
9898
logging.info(f"Total events processed: {self.event_count}")
99-
99+
100100
response_event = CloudEvent(
101-
{
101+
attributes={
102102
"type": "com.example.response.instanced",
103103
"source": "/fcloudevent/instanced",
104104
},
105-
{
105+
data={
106106
"message": "OK: instanced CloudEvent handler",
107107
"event_count": self.event_count,
108-
"received_event_type": event['type'],
109-
"received_event_source": event['source'],
108+
"received_event_type": event.get_type(),
109+
"received_event_source": event.get_source(),
110110
"received_data": event_data
111111
}
112112
)

‎poetry.lock‎

Lines changed: 42 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyproject.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ license = "Apache-2.0"
88
repository = "https://github.com/knative-extensions/func-python"
99

1010
[tool.poetry.dependencies]
11-
python = "^3.8"
11+
python = "^3.10"
1212
hypercorn = "^0.17.3"
13-
cloudevents = "^1.11.0"
13+
cloudevents = "^2.0.0"
1414

1515
[tool.pytest.ini_options]
1616
pythonpath = ["src"]

‎src/func_python/cloudevent.py‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import hypercorn.config
77
import hypercorn.asyncio
88

9-
from cloudevents.http import from_http, CloudEvent
10-
from cloudevents.conversion import to_structured, to_binary
11-
from cloudevents.exceptions import MissingRequiredFields, InvalidRequiredFields
9+
from cloudevents.core.v1.event import CloudEvent
10+
from cloudevents.core.bindings.http import (
11+
from_http_event, to_structured_event, to_binary_event, HTTPMessage,
12+
)
13+
from cloudevents.core.exceptions import CloudEventValidationError
1214

1315
import func_python.sock
1416

@@ -50,8 +52,8 @@ async def handle(self, scope, receive, send):
5052

5153

5254
class ASGIApplication():
53-
""" ASGIApplication is a wrapper around a Function instance which
54-
exposes it as an ASGI Application.
55+
""" ASGIApplication is a wrapper around a Function instance which
56+
exposes it as an ASGI Application.
5557
"""
5658
def __init__(self, f):
5759
self.f = f
@@ -153,12 +155,12 @@ async def __call__(self, scope, receive, send):
153155
send = CloudEventSender(send)
154156
# Delegate processing to user's Function
155157
await self.f.handle(scope, receive, send)
156-
except (MissingRequiredFields, InvalidRequiredFields) as e:
158+
except (CloudEventValidationError, ValueError) as e:
157159
# Log the non-CloudEvent request for debugging
158160
logging.warning(f"Received non-CloudEvent request: {scope['method']} {scope['path']}")
159161
headers_dict = {k.decode('utf-8'): v.decode('utf-8') for k, v in scope.get('headers', [])}
160162
logging.debug(f"Request headers: {headers_dict}")
161-
163+
162164
# Return 400 Bad Request for non-CloudEvent requests
163165
await send({
164166
'type': 'http.response.start',
@@ -231,7 +233,7 @@ async def decode_event(scope, receive):
231233
k.decode("utf-8").lower(): v.decode("utf-8")
232234
for k, v in scope.get("headers", [])
233235
}
234-
return from_http(headers, body)
236+
return from_http_event(HTTPMessage(headers=headers, body=body))
235237

236238

237239
async def receive_body(receive):
@@ -264,7 +266,7 @@ async def send_exception_cloudevent(send, status, message):
264266

265267
# Check if send is a CloudEventSender with structured method
266268
if hasattr(send, 'structured'):
267-
await send.structured(CloudEvent(attributes, data), status)
269+
await send.structured(CloudEvent(attributes=attributes, data=data), status)
268270
else:
269271
# Fallback to plain HTTP error response if send is not a CloudEventSender
270272
logging.warning("send_exception_cloudevent called with non-CloudEventSender, falling back to HTTP response")
@@ -293,13 +295,13 @@ async def __call__(self, event, status: int = 200):
293295

294296
async def structured(self, event, status=200):
295297
"""send as a structured cloudevent"""
296-
headers, body = to_structured(event)
297-
await self._send_encoded_cloudevent(headers, body, status)
298+
msg = to_structured_event(event)
299+
await self._send_encoded_cloudevent(msg.headers, msg.body, status)
298300

299301
async def binary(self, event, status=200):
300302
"""send as a binary cloudevent"""
301-
headers, body = to_binary(event)
302-
await self._send_encoded_cloudevent(headers, body, status)
303+
msg = to_binary_event(event)
304+
await self._send_encoded_cloudevent(msg.headers, msg.body, status)
303305

304306
async def http(self, message):
305307
"""Send a raw http response, bypassing the automatic cloudevent

0 commit comments

Comments
 (0)