forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
29 lines (18 loc) · 763 Bytes
/
utils.py
File metadata and controls
29 lines (18 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import base64
import hashlib
import json
from pathlib import Path
from typing import Any
from aws_lambda_powertools.shared.json_encoder import Encoder
def load_event(file_name: str) -> Any:
path = Path(str(Path(__file__).parent.parent) + "/events/" + file_name)
return json.loads(path.read_text())
def str_to_b64(data: str) -> str:
return base64.b64encode(data.encode()).decode("utf-8")
def b64_to_str(data: str) -> str:
return base64.b64decode(data.encode()).decode("utf-8")
def json_serialize(data):
return json.dumps(data, sort_keys=True, cls=Encoder)
def hash_idempotency_key(data: Any):
"""Serialize data to JSON, encode, and hash it for idempotency key"""
return hashlib.md5(json_serialize(data).encode()).hexdigest()