|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +from typing import Iterator, Optional |
| 4 | + |
| 5 | +from aws_lambda_powertools.utilities.data_classes.common import DictWrapper |
| 6 | + |
| 7 | + |
| 8 | +class KinesisFirehoseRecordMetadata(DictWrapper): |
| 9 | + @property |
| 10 | + def _metadata(self) -> dict: |
| 11 | + """Optional: metadata associated with this record; present only when Kinesis Stream is source""" |
| 12 | + return self["kinesisRecordMetadata"] # could raise KeyError |
| 13 | + |
| 14 | + @property |
| 15 | + def shard_id(self) -> str: |
| 16 | + """Kinesis stream shard ID; present only when Kinesis Stream is source""" |
| 17 | + return self._metadata["shardId"] |
| 18 | + |
| 19 | + @property |
| 20 | + def partition_key(self) -> str: |
| 21 | + """Kinesis stream partition key; present only when Kinesis Stream is source""" |
| 22 | + return self._metadata["partitionKey"] |
| 23 | + |
| 24 | + @property |
| 25 | + def approximate_arrival_timestamp(self) -> int: |
| 26 | + """Kinesis stream approximate arrival ISO timestamp; present only when Kinesis Stream is source""" |
| 27 | + return self._metadata["approximateArrivalTimestamp"] |
| 28 | + |
| 29 | + @property |
| 30 | + def sequence_number(self) -> str: |
| 31 | + """Kinesis stream sequence number; present only when Kinesis Stream is source""" |
| 32 | + return self._metadata["sequenceNumber"] |
| 33 | + |
| 34 | + @property |
| 35 | + def subsequence_number(self) -> str: |
| 36 | + """Kinesis stream sub-sequence number; present only when Kinesis Stream is source |
| 37 | +
|
| 38 | + Note: this will only be present for Kinesis streams using record aggregation |
| 39 | + """ |
| 40 | + return self._metadata["subsequenceNumber"] |
| 41 | + |
| 42 | + |
| 43 | +class KinesisFirehoseRecord(DictWrapper): |
| 44 | + @property |
| 45 | + def approximate_arrival_timestamp(self) -> int: |
| 46 | + """The approximate time that the record was inserted into the delivery stream""" |
| 47 | + return self["approximateArrivalTimestamp"] |
| 48 | + |
| 49 | + @property |
| 50 | + def record_id(self) -> str: |
| 51 | + """Record ID; uniquely identifies this record within the current batch""" |
| 52 | + return self["recordId"] |
| 53 | + |
| 54 | + @property |
| 55 | + def data(self) -> str: |
| 56 | + """The data blob, base64-encoded""" |
| 57 | + return self["data"] |
| 58 | + |
| 59 | + @property |
| 60 | + def metadata(self) -> Optional[KinesisFirehoseRecordMetadata]: |
| 61 | + """Optional: metadata associated with this record; present only when Kinesis Stream is source""" |
| 62 | + return KinesisFirehoseRecordMetadata(self._data) if self.get("kinesisRecordMetadata") else None |
| 63 | + |
| 64 | + @property |
| 65 | + def data_as_bytes(self) -> bytes: |
| 66 | + """Decoded base64-encoded data as bytes""" |
| 67 | + return base64.b64decode(self.data) |
| 68 | + |
| 69 | + @property |
| 70 | + def data_as_text(self) -> str: |
| 71 | + """Decoded base64-encoded data as text""" |
| 72 | + return self.data_as_bytes.decode("utf-8") |
| 73 | + |
| 74 | + @property |
| 75 | + def data_as_json(self) -> dict: |
| 76 | + """Decoded base64-encoded data loaded to json""" |
| 77 | + if self._json_data is None: |
| 78 | + self._json_data = json.loads(self.data_as_text) |
| 79 | + return self._json_data |
| 80 | + |
| 81 | + |
| 82 | +class KinesisFirehoseEvent(DictWrapper): |
| 83 | + """Kinesis Data Firehose event |
| 84 | +
|
| 85 | + Documentation: |
| 86 | + -------------- |
| 87 | + - https://docs.aws.amazon.com/lambda/latest/dg/services-kinesisfirehose.html |
| 88 | + """ |
| 89 | + |
| 90 | + @property |
| 91 | + def invocation_id(self) -> str: |
| 92 | + """Unique ID for for Lambda invocation""" |
| 93 | + return self["invocationId"] |
| 94 | + |
| 95 | + @property |
| 96 | + def delivery_stream_arn(self) -> str: |
| 97 | + """ARN of the Firehose Data Firehose Delivery Stream""" |
| 98 | + return self["deliveryStreamArn"] |
| 99 | + |
| 100 | + @property |
| 101 | + def source_kinesis_stream_arn(self) -> Optional[str]: |
| 102 | + """ARN of the Kinesis Stream; present only when Kinesis Stream is source""" |
| 103 | + return self.get("sourceKinesisStreamArn") |
| 104 | + |
| 105 | + @property |
| 106 | + def region(self) -> str: |
| 107 | + """AWS region where the event originated eg: us-east-1""" |
| 108 | + return self["region"] |
| 109 | + |
| 110 | + @property |
| 111 | + def records(self) -> Iterator[KinesisFirehoseRecord]: |
| 112 | + for record in self["records"]: |
| 113 | + yield KinesisFirehoseRecord(record) |
0 commit comments