|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import base64 |
| 4 | +from typing import Callable, Iterator, List, Optional, Union |
| 5 | + |
| 6 | +from aws_lambda_powertools.utilities.data_classes.common import DictWrapper |
| 7 | + |
| 8 | +FirehoseStateOk = "Ok" |
| 9 | +FirehoseStateDropped = "Dropped" |
| 10 | +FirehoseStateFailed = "ProcessingFailed" |
| 11 | + |
| 12 | + |
| 13 | +class KinesisFirehoseResponseRecordMetadata(DictWrapper): |
| 14 | + """ |
| 15 | + Documentation: |
| 16 | + -------------- |
| 17 | + - https://docs.aws.amazon.com/firehose/latest/dev/dynamic-partitioning.html |
| 18 | + """ |
| 19 | + |
| 20 | + @property |
| 21 | + def _metadata(self) -> Optional[dict]: |
| 22 | + """Optional: metadata associated with this record; present only when Kinesis Stream is source""" |
| 23 | + return self["metadata"] # could raise KeyError |
| 24 | + |
| 25 | + @property |
| 26 | + def partition_keys(self) -> Optional[dict[str, str]]: |
| 27 | + """Kinesis stream partition key; present only when Kinesis Stream is source""" |
| 28 | + return self._metadata["partitionKeys"] |
| 29 | + |
| 30 | + |
| 31 | +def KinesisFirehoseResponseRecordMetadataFactory( |
| 32 | + partition_keys: dict[str, str], |
| 33 | + json_deserializer: Optional[Callable] = None, |
| 34 | +) -> KinesisFirehoseResponseRecordMetadata: |
| 35 | + data = { |
| 36 | + "metadata": { |
| 37 | + "partitionKeys": partition_keys, |
| 38 | + }, |
| 39 | + } |
| 40 | + return KinesisFirehoseResponseRecordMetadata(data=data, json_deserializer=json_deserializer) |
| 41 | + |
| 42 | + |
| 43 | +class KinesisFirehoseResponceRecord(DictWrapper): |
| 44 | + """Record in Kinesis Data Firehose event |
| 45 | +
|
| 46 | + Documentation: |
| 47 | + -------------- |
| 48 | + - https://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html |
| 49 | + """ |
| 50 | + |
| 51 | + @property |
| 52 | + def record_id(self) -> str: |
| 53 | + """Record ID; uniquely identifies this record within the current batch""" |
| 54 | + return self["recordId"] |
| 55 | + |
| 56 | + @property |
| 57 | + def result(self) -> Union[FirehoseStateOk, FirehoseStateDropped, FirehoseStateFailed]: |
| 58 | + """processing result, supported value: Ok, Dropped, ProcessingFailed""" |
| 59 | + return self["result"] |
| 60 | + |
| 61 | + @property |
| 62 | + def data(self) -> str: |
| 63 | + """The data blob, base64-encoded""" |
| 64 | + return self["data"] |
| 65 | + |
| 66 | + @property |
| 67 | + def metadata(self) -> Optional[KinesisFirehoseResponseRecordMetadata]: |
| 68 | + """Optional: metadata associated with this record; present only when Kinesis Stream is source""" |
| 69 | + return KinesisFirehoseResponseRecordMetadata(self._data) if self.get("metadata") else None |
| 70 | + |
| 71 | + @property |
| 72 | + def data_as_bytes(self) -> bytes: |
| 73 | + """Decoded base64-encoded data as bytes""" |
| 74 | + return base64.b64decode(self.data) |
| 75 | + |
| 76 | + @property |
| 77 | + def data_as_text(self) -> str: |
| 78 | + """Decoded base64-encoded data as text""" |
| 79 | + return self.data_as_bytes.decode("utf-8") |
| 80 | + |
| 81 | + @property |
| 82 | + def data_as_json(self) -> dict: |
| 83 | + """Decoded base64-encoded data loaded to json""" |
| 84 | + if self._json_data is None: |
| 85 | + self._json_data = self._json_deserializer(self.data_as_text) |
| 86 | + return self._json_data |
| 87 | + |
| 88 | + |
| 89 | +def KinesisFirehoseResponceRecordFactory( |
| 90 | + record_id: str, |
| 91 | + result: Union[FirehoseStateOk, FirehoseStateDropped, FirehoseStateFailed], |
| 92 | + data: str, |
| 93 | + metadata: Optional[KinesisFirehoseResponseRecordMetadata] = None, |
| 94 | + json_deserializer: Optional[Callable] = None, |
| 95 | +) -> KinesisFirehoseResponceRecord: |
| 96 | + pass_data = { |
| 97 | + "recordId": record_id, |
| 98 | + "result": result, |
| 99 | + "data": base64.b64encode(data.encode("utf-8")).decode("utf-8"), |
| 100 | + } |
| 101 | + if metadata: |
| 102 | + data["metadata"] = metadata |
| 103 | + return KinesisFirehoseResponceRecord(data=pass_data, json_deserializer=json_deserializer) |
| 104 | + |
| 105 | + |
| 106 | +class KinesisFirehoseResponce(DictWrapper): |
| 107 | + """Kinesis Data Firehose event |
| 108 | +
|
| 109 | + Documentation: |
| 110 | + -------------- |
| 111 | + - https://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html |
| 112 | + """ |
| 113 | + |
| 114 | + @property |
| 115 | + def records(self) -> Iterator[KinesisFirehoseResponceRecord]: |
| 116 | + for record in self["records"]: |
| 117 | + yield KinesisFirehoseResponceRecord(data=record, json_deserializer=self._json_deserializer) |
| 118 | + |
| 119 | + |
| 120 | +def KinesisFirehoseResponceFactory( |
| 121 | + records: List[KinesisFirehoseResponceRecord], |
| 122 | + json_deserializer: Optional[Callable] = None, |
| 123 | +) -> KinesisFirehoseResponce: |
| 124 | + pass_data = {"records": records} |
| 125 | + return KinesisFirehoseResponce(data=pass_data, json_deserializer=json_deserializer) |
0 commit comments