Skip to content

Commit

Permalink
added support for redis and rediss
Browse files Browse the repository at this point in the history
Signed-off-by: ahmedsobeh <ahmed.sobeh@aiven.io>
  • Loading branch information
ahmedsobeh authored and aiven-sal committed Jul 3, 2024
1 parent e9bdc84 commit 0ca204b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
4 changes: 2 additions & 2 deletions tests/test_asyncio/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ def test_invalid_scheme_raises_error(self):
with pytest.raises(ValueError) as cm:
valkey.ConnectionPool.from_url("localhost")
assert str(cm.value) == (
"Valkey URL must specify one of the following schemes "
"(valkey://, valkeys://, unix://)"
"Valkey URL must specify one of the following schemes"
" ['valkey', 'valkeys', 'redis', 'rediss', 'unix']"
)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,15 @@ def test_invalid_scheme_raises_error(self):
valkey.ConnectionPool.from_url("localhost")
assert str(cm.value) == (
"Valkey URL must specify one of the following schemes "
"(valkey://, valkeys://, unix://)"
"['valkey', 'valkeys', 'redis', 'rediss', 'unix']"
)

def test_invalid_scheme_raises_error_when_double_slash_missing(self):
with pytest.raises(ValueError) as cm:
valkey.ConnectionPool.from_url("valkey:foo.bar.com:12345")
assert str(cm.value) == (
"Valkey URL must specify one of the following schemes "
"(valkey://, valkeys://, unix://)"
"['valkey', 'valkeys', 'redis', 'rediss', 'unix']"
)


Expand Down
29 changes: 13 additions & 16 deletions valkey/_parsers/url_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from types import MappingProxyType
from typing import Callable, Mapping, Optional
from urllib.parse import ParseResult, parse_qs, unquote, urlparse
Expand Down Expand Up @@ -36,19 +37,16 @@ def to_bool(value) -> Optional[bool]:


def parse_url(url: str, async_connection: bool):

if not (
url.startswith("valkey://")
or url.startswith("valkeys://")
or url.startswith("unix://")
):
raise ValueError(
"Valkey URL must specify one of the following "
"schemes (valkey://, valkeys://, unix://)"
)

supported_schemes = ["valkey", "valkeys", "redis", "rediss", "unix"]
parsed: ParseResult = urlparse(url)
kwargs: ConnectKwargs = {}
pattern = re.compile(
r"^(?:" + "|".join(map(re.escape, supported_schemes)) + r")://", re.IGNORECASE
)
if not pattern.match(url):
raise ValueError(
f"Valkey URL must specify one of the following schemes {supported_schemes}"
)

for name, value_list in parse_qs(parsed.query).items():
if value_list and len(value_list) > 0:
Expand All @@ -67,7 +65,7 @@ def parse_url(url: str, async_connection: bool):
if parsed.password:
kwargs["password"] = unquote(parsed.password)

# We only support valkey://, valkeys:// and unix:// schemes.
# We only support valkey://, valkeys://, redis://, rediss://, and unix:// schemes.
if parsed.scheme == "unix":
if parsed.path:
kwargs["path"] = unquote(parsed.path)
Expand All @@ -77,7 +75,7 @@ def parse_url(url: str, async_connection: bool):
else UnixDomainSocketConnection
)

elif parsed.scheme in ("valkey", "valkeys"):
elif parsed.scheme in supported_schemes:
if parsed.hostname:
kwargs["host"] = unquote(parsed.hostname)
if parsed.port:
Expand All @@ -91,14 +89,13 @@ def parse_url(url: str, async_connection: bool):
except (AttributeError, ValueError):
pass

if parsed.scheme == "valkeys":
if parsed.scheme in ("valkeys", "rediss"):
kwargs["connection_class"] = (
SSLConnectionAsync if async_connection else SSLConnection
)
else:
valid_schemes = "valkey://, valkeys://, unix://"
raise ValueError(
f"Valkey URL must specify one of the following schemes ({valid_schemes})"
f"Valkey URL must specify one of the following schemes {supported_schemes}"
)

return kwargs

0 comments on commit 0ca204b

Please sign in to comment.