-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Auto-format Bot <[email protected]>
- Loading branch information
1 parent
b79c307
commit 6b978ad
Showing
11 changed files
with
171 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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,45 @@ | ||
import base64 | ||
import json | ||
import sys | ||
from typing import Dict, Optional, Union | ||
|
||
|
||
def url_encode_dict(maybe_dict: Union[Dict, str], name: str, size_limit_bytes: Optional[int] = None) -> str: | ||
"""Encode a dictionary as a URL-safe, base64-encoded JSON string. | ||
:param maybe_dict: The dictionary or JSON string to encode. | ||
:type maybe_dict: dict or str | ||
:param name: The name of the dictionary, for use in the error message. | ||
:type name: str | ||
:param size_limit_bytes: The maximum size of the dictionary, in bytes. | ||
If `None`, no size limit is enforced. | ||
:type size_limit_bytes: int or None | ||
:raises TypeError: If `maybe_dict` is not a dictionary or JSON string. | ||
:raises ValueError: If `maybe_dict` is too large. | ||
:return: The URL-safe, base64-encoded JSON string. | ||
:rtype: str | ||
""" | ||
original_type = type(maybe_dict) | ||
if isinstance(maybe_dict, str): | ||
try: | ||
# It's a little inefficient to parse the JSON string, just to re-encode it later. But it | ||
# allows us to check that we get a valid dictionary, and we remove any whitespace. | ||
maybe_dict = json.loads(maybe_dict) | ||
except json.JSONDecodeError as e: | ||
raise TypeError(f"`{name}` must be a dictionary or JSON string: got {original_type}") from e | ||
|
||
if not isinstance(maybe_dict, dict): | ||
raise TypeError(f"`{name}` must be a dictionary or JSON string: got {original_type}") | ||
|
||
data_json = json.dumps(maybe_dict) | ||
|
||
if size_limit_bytes is not None: | ||
size_bytes = sys.getsizeof(data_json) | ||
if size_bytes > size_limit_bytes: | ||
raise ValueError(f"`{name}` is too large: {size_bytes} bytes > {size_limit_bytes} bytes limit.") | ||
|
||
return base64.urlsafe_b64encode(data_json.encode("utf-8")).decode("utf-8") |
Oops, something went wrong.