-
Notifications
You must be signed in to change notification settings - Fork 432
refactor(event_source): centralizing helper functions for query, header and base64 #2496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leandrodamascena
merged 4 commits into
aws-powertools:develop
from
leandrodamascena:techdebt/dictwrapper
Jun 20, 2023
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f6eb2b1
tech-debt: centralizing functions
leandrodamascena 66d530d
addressing Heitor's feedback
leandrodamascena 8a257b0
Merge branch 'develop' into techdebt/dictwrapper
leandrodamascena 2dd8310
Merge branch 'develop' into techdebt/dictwrapper
leandrodamascena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
aws_lambda_powertools/utilities/data_classes/appsync_resolver_event.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
aws_lambda_powertools/utilities/data_classes/s3_object_event.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
aws_lambda_powertools/utilities/data_classes/shared_functions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from __future__ import annotations | ||
|
||
import base64 | ||
from typing import Any | ||
|
||
|
||
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") | ||
|
||
|
||
def get_header_value( | ||
headers: dict[str, Any], name: str, default_value: str | None, case_sensitive: bool | None | ||
) -> 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 None. | ||
|
||
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, | ||
) | ||
|
||
|
||
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.