forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_functions.py
147 lines (118 loc) · 3.67 KB
/
shared_functions.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from __future__ import annotations
import base64
from typing import Any, Dict, overload
def base64_decode(value: str) -> str:
"""
Decodes a Base64-encoded string and returns the decoded value.
Parameters
----------
value: str
The Base64-encoded string to decode.
Returns
-------
str
The decoded string value.
"""
return base64.b64decode(value).decode("UTF-8")
@overload
def get_header_value(
headers: dict[str, Any],
name: str,
default_value: str,
case_sensitive: bool = False,
) -> str: ...
@overload
def get_header_value(
headers: dict[str, Any],
name: str,
default_value: str | None = None,
case_sensitive: bool = False,
) -> str | None: ...
def get_header_value(
headers: dict[str, Any],
name: str,
default_value: str | None = None,
case_sensitive: bool = False,
) -> str | None:
"""
Get the value of a header by its name.
Parameters
----------
headers: Dict[str, str]
The dictionary of headers.
name: str
The name of the header to retrieve.
default_value: str, optional
The default value to return if the header is not found. Default is None.
case_sensitive: bool, optional
Indicates whether the header name should be case-sensitive. Default is False.
Returns
-------
str, optional
The value of the header if found, otherwise the default value or None.
"""
# If headers is NoneType, return default value
if not headers:
return default_value
if case_sensitive:
return headers.get(name, default_value)
name_lower = name.lower()
return next(
# Iterate over the dict and do a case-insensitive key comparison
(value for key, value in headers.items() if key.lower() == name_lower),
# Default value is returned if no matches was found
default_value,
)
@overload
def get_query_string_value(
query_string_parameters: Dict[str, str] | None,
name: str,
default_value: str,
) -> str: ...
@overload
def get_query_string_value(
query_string_parameters: Dict[str, str] | None,
name: str,
default_value: str | None = None,
) -> str | None: ...
def get_query_string_value(
query_string_parameters: Dict[str, str] | None,
name: str,
default_value: str | None = None,
) -> str | None:
"""
Retrieves the value of a query string parameter specified by the given name.
Parameters
----------
name: str
The name of the query string parameter to retrieve.
default_value: str, optional
The default value to return if the parameter is not found. Defaults to None.
Returns
-------
str. optional
The value of the query string parameter if found, or the default value if not found.
"""
params = query_string_parameters
return default_value if params is None else params.get(name, default_value)
def get_multi_value_query_string_values(
multi_value_query_string_parameters: Dict[str, list[str]] | None,
name: str,
default_values: list[str] | None = None,
) -> list[str]:
"""
Retrieves the values of a multi-value string parameters specified by the given name.
Parameters
----------
name: str
The name of the query string parameter to retrieve.
default_value: list[str], optional
The default value to return if the parameter is not found. Defaults to None.
Returns
-------
List[str]. optional
The values of the query string parameter if found, or the default values if not found.
"""
default = default_values or []
params = multi_value_query_string_parameters or {}
return params.get(name) or default