|
6 | 6 | import hypercorn.config |
7 | 7 | import hypercorn.asyncio |
8 | 8 |
|
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 |
12 | 14 |
|
13 | 15 | import func_python.sock |
14 | 16 |
|
@@ -50,8 +52,8 @@ async def handle(self, scope, receive, send): |
50 | 52 |
|
51 | 53 |
|
52 | 54 | 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. |
55 | 57 | """ |
56 | 58 | def __init__(self, f): |
57 | 59 | self.f = f |
@@ -153,12 +155,12 @@ async def __call__(self, scope, receive, send): |
153 | 155 | send = CloudEventSender(send) |
154 | 156 | # Delegate processing to user's Function |
155 | 157 | await self.f.handle(scope, receive, send) |
156 | | - except (MissingRequiredFields, InvalidRequiredFields) as e: |
| 158 | + except (CloudEventValidationError, ValueError) as e: |
157 | 159 | # Log the non-CloudEvent request for debugging |
158 | 160 | logging.warning(f"Received non-CloudEvent request: {scope['method']} {scope['path']}") |
159 | 161 | headers_dict = {k.decode('utf-8'): v.decode('utf-8') for k, v in scope.get('headers', [])} |
160 | 162 | logging.debug(f"Request headers: {headers_dict}") |
161 | | - |
| 163 | + |
162 | 164 | # Return 400 Bad Request for non-CloudEvent requests |
163 | 165 | await send({ |
164 | 166 | 'type': 'http.response.start', |
@@ -231,7 +233,7 @@ async def decode_event(scope, receive): |
231 | 233 | k.decode("utf-8").lower(): v.decode("utf-8") |
232 | 234 | for k, v in scope.get("headers", []) |
233 | 235 | } |
234 | | - return from_http(headers, body) |
| 236 | + return from_http_event(HTTPMessage(headers=headers, body=body)) |
235 | 237 |
|
236 | 238 |
|
237 | 239 | async def receive_body(receive): |
@@ -264,7 +266,7 @@ async def send_exception_cloudevent(send, status, message): |
264 | 266 |
|
265 | 267 | # Check if send is a CloudEventSender with structured method |
266 | 268 | if hasattr(send, 'structured'): |
267 | | - await send.structured(CloudEvent(attributes, data), status) |
| 269 | + await send.structured(CloudEvent(attributes=attributes, data=data), status) |
268 | 270 | else: |
269 | 271 | # Fallback to plain HTTP error response if send is not a CloudEventSender |
270 | 272 | 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): |
293 | 295 |
|
294 | 296 | async def structured(self, event, status=200): |
295 | 297 | """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) |
298 | 300 |
|
299 | 301 | async def binary(self, event, status=200): |
300 | 302 | """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) |
303 | 305 |
|
304 | 306 | async def http(self, message): |
305 | 307 | """Send a raw http response, bypassing the automatic cloudevent |
|
0 commit comments