|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import base64 |
| 4 | +from functools import cached_property |
| 5 | +from typing import Any, Literal |
| 6 | + |
| 7 | +from aws_lambda_powertools.utilities.data_classes.common import DictWrapper |
| 8 | + |
| 9 | +OCTET_STREAM = "application/octet-stream" |
| 10 | + |
| 11 | + |
| 12 | +def _is_with_metadata(item: Any) -> bool: |
| 13 | + return isinstance(item, dict) and "Data" in item and "SystemMetadata" in item |
| 14 | + |
| 15 | + |
| 16 | +class EventBridgeV2SystemMetadata(DictWrapper): |
| 17 | + """Metadata added by the bus (`SystemMetadata`), only present with the WITH_METADATA transformer""" |
| 18 | + |
| 19 | + @property |
| 20 | + def event_id(self) -> str | None: |
| 21 | + """Unique event id assigned by the bus (`aws:EventId`). It is not the id of the classic envelope.""" |
| 22 | + return self.get("aws:EventId") |
| 23 | + |
| 24 | + @property |
| 25 | + def sequence_number(self) -> str | None: |
| 26 | + """Sequence number of the event within the bus (`aws:SequenceNumber`).""" |
| 27 | + return self.get("aws:SequenceNumber") |
| 28 | + |
| 29 | + @property |
| 30 | + def delivery_type(self) -> Literal["LIVE", "REPLAY"] | None: |
| 31 | + """Whether the event is delivered as it arrives (`LIVE`) or as part of a replay (`REPLAY`).""" |
| 32 | + return self.get("aws:DeliveryType") |
| 33 | + |
| 34 | + @property |
| 35 | + def ingestion_time(self) -> str | None: |
| 36 | + """Time the bus ingested the event (`aws:IngestionTime`).""" |
| 37 | + return self.get("aws:IngestionTime") |
| 38 | + |
| 39 | + @property |
| 40 | + def event_group_id(self) -> str | None: |
| 41 | + """FIFO group id the event belongs to (`EventGroupId`).""" |
| 42 | + return self.get("EventGroupId") |
| 43 | + |
| 44 | + @property |
| 45 | + def deduplication_id(self) -> str | None: |
| 46 | + """Deduplication id of the event (`DeduplicationId`).""" |
| 47 | + return self.get("DeduplicationId") |
| 48 | + |
| 49 | + @property |
| 50 | + def content_type(self) -> str | None: |
| 51 | + """Content type of the payload, e.g. `application/json` or `application/eventbridge+json` (`ContentType`).""" |
| 52 | + return self.get("ContentType") |
| 53 | + |
| 54 | + @property |
| 55 | + def source(self) -> str | None: |
| 56 | + """Event source (`aws:Source`), only present for events published with PutEvents.""" |
| 57 | + return self.get("aws:Source") |
| 58 | + |
| 59 | + @property |
| 60 | + def detail_type(self) -> str | None: |
| 61 | + """Event detail type (`aws:DetailType`), only present for events published with PutEvents.""" |
| 62 | + return self.get("aws:DetailType") |
| 63 | + |
| 64 | + |
| 65 | +class EventBridgeV2Record(DictWrapper): |
| 66 | + """A single event delivered by an Amazon EventBridge Custom Event Bus""" |
| 67 | + |
| 68 | + @property |
| 69 | + def data(self) -> Any: |
| 70 | + """Event payload: a dict for JSON, bytes for `application/octet-stream`.""" |
| 71 | + data = self["Data"] |
| 72 | + if self.system_metadata.content_type == OCTET_STREAM and isinstance(data, str): |
| 73 | + return base64.b64decode(data) |
| 74 | + return data |
| 75 | + |
| 76 | + @property |
| 77 | + def detail(self) -> dict[str, Any] | None: |
| 78 | + """Shortcut to `data["detail"]` when the event was published with PutEvents, otherwise None.""" |
| 79 | + data = self["Data"] |
| 80 | + if isinstance(data, dict) and "detail-type" in data and "detail" in data: |
| 81 | + return data["detail"] |
| 82 | + return None |
| 83 | + |
| 84 | + @property |
| 85 | + def metadata(self) -> dict[str, Any]: |
| 86 | + """Producer metadata keys sent with PutRawEvents.""" |
| 87 | + return self.get("Metadata") or {} |
| 88 | + |
| 89 | + @cached_property |
| 90 | + def system_metadata(self) -> EventBridgeV2SystemMetadata: |
| 91 | + """Metadata added by the bus; empty when the subscriber doesn't use the WITH_METADATA transformer.""" |
| 92 | + return EventBridgeV2SystemMetadata(self.get("SystemMetadata") or {}) |
| 93 | + |
| 94 | + |
| 95 | +class EventBridgeV2Event(DictWrapper): |
| 96 | + """Amazon EventBridge Custom Event Bus event |
| 97 | +
|
| 98 | + Accepts what a subscriber can deliver to Lambda: a single object or an array, using the |
| 99 | + WITH_METADATA transformer (`Data`, `Metadata`, `SystemMetadata`) or RAW (classic envelope, |
| 100 | + PutEvents array or the bare PutRawEvents payload). RAW deliveries carry no marker, so |
| 101 | + they are exposed with empty metadata. |
| 102 | +
|
| 103 | + Documentation: |
| 104 | + -------------- |
| 105 | + - https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-custom-bus.html |
| 106 | + """ |
| 107 | + |
| 108 | + @cached_property |
| 109 | + def records(self) -> list[EventBridgeV2Record]: |
| 110 | + """Events in the delivery. Always a list, even when the bus sent a single object.""" |
| 111 | + items = self._data if isinstance(self._data, list) else [self._data] |
| 112 | + return [ |
| 113 | + EventBridgeV2Record( |
| 114 | + item if _is_with_metadata(item) else {"Data": item, "Metadata": {}, "SystemMetadata": {}}, |
| 115 | + ) |
| 116 | + for item in items |
| 117 | + ] |
0 commit comments