|
23 | 23 | encode_header_value, |
24 | 24 | ) |
25 | 25 | from cloudevents.core.formats.base import Format |
| 26 | +from cloudevents.core.formats.json import JSONFormat |
| 27 | +from cloudevents.core.v1.event import CloudEvent |
26 | 28 |
|
27 | 29 | CE_PREFIX: Final[str] = "ce_" |
28 | 30 | PARTITIONKEY_ATTR: Final[str] = "partitionkey" |
@@ -320,3 +322,120 @@ def from_kafka( |
320 | 322 | return from_binary(message, event_format, event_factory) |
321 | 323 |
|
322 | 324 | return from_structured(message, event_format, event_factory) |
| 325 | + |
| 326 | + |
| 327 | +def to_binary_event( |
| 328 | + event: BaseCloudEvent, |
| 329 | + event_format: Format | None = None, |
| 330 | + key_mapper: KeyMapper | None = None, |
| 331 | +) -> KafkaMessage: |
| 332 | + """ |
| 333 | + Convenience wrapper for to_binary with JSON format and CloudEvent as defaults. |
| 334 | +
|
| 335 | + Example: |
| 336 | + >>> from cloudevents.core.v1.event import CloudEvent |
| 337 | + >>> from cloudevents.core.bindings import kafka |
| 338 | + >>> |
| 339 | + >>> event = CloudEvent( |
| 340 | + ... attributes={"type": "com.example.test", "source": "/test"}, |
| 341 | + ... data={"message": "Hello"} |
| 342 | + ... ) |
| 343 | + >>> message = kafka.to_binary_event(event) |
| 344 | +
|
| 345 | + :param event: The CloudEvent to convert |
| 346 | + :param event_format: Format implementation (defaults to JSONFormat) |
| 347 | + :param key_mapper: Optional function to extract message key from event |
| 348 | + :return: KafkaMessage with ce_-prefixed headers |
| 349 | + """ |
| 350 | + if event_format is None: |
| 351 | + event_format = JSONFormat() |
| 352 | + return to_binary(event, event_format, key_mapper) |
| 353 | + |
| 354 | + |
| 355 | +def from_binary_event( |
| 356 | + message: KafkaMessage, |
| 357 | + event_format: Format | None = None, |
| 358 | +) -> BaseCloudEvent: |
| 359 | + """ |
| 360 | + Convenience wrapper for from_binary with JSON format and CloudEvent as defaults. |
| 361 | +
|
| 362 | + Example: |
| 363 | + >>> from cloudevents.core.bindings import kafka |
| 364 | + >>> event = kafka.from_binary_event(message) |
| 365 | +
|
| 366 | + :param message: KafkaMessage to parse |
| 367 | + :param event_format: Format implementation (defaults to JSONFormat) |
| 368 | + :return: CloudEvent instance |
| 369 | + """ |
| 370 | + if event_format is None: |
| 371 | + event_format = JSONFormat() |
| 372 | + return from_binary(message, event_format, CloudEvent) |
| 373 | + |
| 374 | + |
| 375 | +def to_structured_event( |
| 376 | + event: BaseCloudEvent, |
| 377 | + event_format: Format | None = None, |
| 378 | + key_mapper: KeyMapper | None = None, |
| 379 | +) -> KafkaMessage: |
| 380 | + """ |
| 381 | + Convenience wrapper for to_structured with JSON format as default. |
| 382 | +
|
| 383 | + Example: |
| 384 | + >>> from cloudevents.core.v1.event import CloudEvent |
| 385 | + >>> from cloudevents.core.bindings import kafka |
| 386 | + >>> |
| 387 | + >>> event = CloudEvent( |
| 388 | + ... attributes={"type": "com.example.test", "source": "/test"}, |
| 389 | + ... data={"message": "Hello"} |
| 390 | + ... ) |
| 391 | + >>> message = kafka.to_structured_event(event) |
| 392 | +
|
| 393 | + :param event: The CloudEvent to convert |
| 394 | + :param event_format: Format implementation (defaults to JSONFormat) |
| 395 | + :param key_mapper: Optional function to extract message key from event |
| 396 | + :return: KafkaMessage with structured content |
| 397 | + """ |
| 398 | + if event_format is None: |
| 399 | + event_format = JSONFormat() |
| 400 | + return to_structured(event, event_format, key_mapper) |
| 401 | + |
| 402 | + |
| 403 | +def from_structured_event( |
| 404 | + message: KafkaMessage, |
| 405 | + event_format: Format | None = None, |
| 406 | +) -> BaseCloudEvent: |
| 407 | + """ |
| 408 | + Convenience wrapper for from_structured with JSON format and CloudEvent as defaults. |
| 409 | +
|
| 410 | + Example: |
| 411 | + >>> from cloudevents.core.bindings import kafka |
| 412 | + >>> event = kafka.from_structured_event(message) |
| 413 | +
|
| 414 | + :param message: KafkaMessage to parse |
| 415 | + :param event_format: Format implementation (defaults to JSONFormat) |
| 416 | + :return: CloudEvent instance |
| 417 | + """ |
| 418 | + if event_format is None: |
| 419 | + event_format = JSONFormat() |
| 420 | + return from_structured(message, event_format, CloudEvent) |
| 421 | + |
| 422 | + |
| 423 | +def from_kafka_event( |
| 424 | + message: KafkaMessage, |
| 425 | + event_format: Format | None = None, |
| 426 | +) -> BaseCloudEvent: |
| 427 | + """ |
| 428 | + Convenience wrapper for from_kafka with JSON format and CloudEvent as defaults. |
| 429 | + Auto-detects binary or structured mode. |
| 430 | +
|
| 431 | + Example: |
| 432 | + >>> from cloudevents.core.bindings import kafka |
| 433 | + >>> event = kafka.from_kafka_event(message) |
| 434 | +
|
| 435 | + :param message: KafkaMessage to parse |
| 436 | + :param event_format: Format implementation (defaults to JSONFormat) |
| 437 | + :return: CloudEvent instance |
| 438 | + """ |
| 439 | + if event_format is None: |
| 440 | + event_format = JSONFormat() |
| 441 | + return from_kafka(message, event_format, CloudEvent) |
0 commit comments