Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions elastic_transport/client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,15 @@ def to_bytes(
return value


# Python 3.7 added '~' to the safe list for urllib.parse.quote()
_QUOTE_ALWAYS_SAFE = frozenset(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~"
)


def percent_encode(
string: Union[bytes, str],
safe: str = "/",
encoding: Optional[str] = None,
errors: Optional[str] = None,
) -> str:
"""Percent-encodes a string so it can be used in an HTTP request target"""
# Redefines 'urllib.parse.quote()' to always have the '~' character
# within the 'ALWAYS_SAFE' list. The character was added in Python 3.7
safe = "".join(_QUOTE_ALWAYS_SAFE.union(set(safe)))
# This function used to add `~` to unreserverd characters, but this was fixed in Python 3.7.
# Keeping the function here as it is part of the public API.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the function be deprecated, though? My guess is that we'd want to steer people away from using a custom function now that it is simply a wrapper.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be deprecated, but is currently used in elasticsearch-py. I'll look into doing it, thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently blocked by elastic/elasticsearch-py#2268.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return _quote(string, safe, encoding=encoding, errors=errors) # type: ignore[arg-type]


Expand Down