Skip to content

Commit 5fc9644

Browse files
author
Mikhail Koviazin
authored
Merge pull request #206 from valkey-io/mkmkme/connection-parse_url
connection: reintroduce parse_url as a function in valkey.connection
2 parents 495bf7e + 7e19c35 commit 5fc9644

File tree

8 files changed

+46
-28
lines changed

8 files changed

+46
-28
lines changed

tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
import valkey
1212
from packaging.version import Version
1313
from valkey import Sentinel
14-
from valkey._parsers import parse_url
1514
from valkey.backoff import NoBackoff
16-
from valkey.connection import Connection
15+
from valkey.connection import Connection, parse_url
1716
from valkey.exceptions import ValkeyClusterException
1817
from valkey.retry import Retry
1918

@@ -286,7 +285,7 @@ def _get_client(
286285

287286
cluster_mode = VALKEY_INFO["cluster_enabled"]
288287
if not cluster_mode:
289-
url_options = parse_url(valkey_url, False)
288+
url_options = parse_url(valkey_url)
290289
url_options.update(kwargs)
291290
pool = valkey.ConnectionPool(**url_options)
292291
client = cls(connection_pool=pool)

tests/test_asyncio/conftest.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import pytest_asyncio
66
import valkey.asyncio as valkey
77
from tests.conftest import VALKEY_INFO
8-
from valkey._parsers import parse_url
98
from valkey.asyncio import Sentinel
109
from valkey.asyncio.client import Monitor
11-
from valkey.asyncio.connection import Connection
10+
from valkey.asyncio.connection import Connection, parse_url
1211
from valkey.asyncio.retry import Retry
1312
from valkey.backoff import NoBackoff
1413

@@ -55,7 +54,7 @@ async def client_factory(
5554
cluster_mode = VALKEY_INFO["cluster_enabled"]
5655
if not cluster_mode:
5756
single = kwargs.pop("single_connection_client", False) or single_connection
58-
url_options = parse_url(url, True)
57+
url_options = parse_url(url)
5958
url_options.update(kwargs)
6059
pool = valkey.ConnectionPool(**url_options)
6160
client = cls(connection_pool=pool)
@@ -270,4 +269,4 @@ def valkey_url(request):
270269
@pytest.fixture()
271270
def connect_args(request):
272271
url = request.config.getoption("--valkey-url")
273-
return parse_url(url, True)
272+
return parse_url(url)

tests/test_asyncio/test_connection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
_AsyncRESP2Parser,
1212
_AsyncRESP3Parser,
1313
_AsyncRESPBase,
14-
parse_url,
1514
)
1615
from valkey.asyncio import ConnectionPool, Valkey
1716
from valkey.asyncio.connection import (
1817
Connection,
1918
SSLConnection,
2019
UnixDomainSocketConnection,
20+
parse_url,
2121
)
2222
from valkey.asyncio.retry import Retry
2323
from valkey.backoff import NoBackoff
@@ -305,7 +305,7 @@ async def test_pool_auto_close(request, from_url):
305305
"""Verify that basic Valkey instances have auto_close_connection_pool set to True"""
306306

307307
url: str = request.config.getoption("--valkey-url")
308-
url_args = parse_url(url, True)
308+
url_args = parse_url(url)
309309

310310
async def get_valkey_connection():
311311
if from_url:
@@ -347,7 +347,7 @@ async def test_pool_auto_close_disable(request):
347347
"""Verify that auto_close_connection_pool can be disabled (deprecated)"""
348348

349349
url: str = request.config.getoption("--valkey-url")
350-
url_args = parse_url(url, True)
350+
url_args = parse_url(url)
351351

352352
async def get_valkey_connection():
353353
url_args["auto_close_connection_pool"] = False
@@ -366,7 +366,7 @@ async def test_valkey_connection_pool(request, from_url):
366366
have auto_close_connection_pool set to False"""
367367

368368
url: str = request.config.getoption("--valkey-url")
369-
url_args = parse_url(url, True)
369+
url_args = parse_url(url)
370370

371371
pool = None
372372

@@ -398,7 +398,7 @@ async def test_valkey_from_pool(request, from_url):
398398
have auto_close_connection_pool set to True"""
399399

400400
url: str = request.config.getoption("--valkey-url")
401-
url_args = parse_url(url, True)
401+
url_args = parse_url(url)
402402

403403
pool = None
404404

tests/test_connection.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
import pytest
77
import valkey
88
from valkey import ConnectionPool, Valkey
9-
from valkey._parsers import _LibvalkeyParser, _RESP2Parser, _RESP3Parser, parse_url
9+
from valkey._parsers import _LibvalkeyParser, _RESP2Parser, _RESP3Parser
1010
from valkey.backoff import NoBackoff
11-
from valkey.connection import Connection, SSLConnection, UnixDomainSocketConnection
11+
from valkey.connection import (
12+
Connection,
13+
SSLConnection,
14+
UnixDomainSocketConnection,
15+
parse_url,
16+
)
1217
from valkey.exceptions import ConnectionError, InvalidResponse, TimeoutError
1318
from valkey.retry import Retry
1419
from valkey.utils import LIBVALKEY_AVAILABLE
@@ -217,7 +222,7 @@ def test_pool_auto_close(request, from_url):
217222
"""Verify that basic Valkey instances have auto_close_connection_pool set to True"""
218223

219224
url: str = request.config.getoption("--valkey-url")
220-
url_args = parse_url(url, False)
225+
url_args = parse_url(url)
221226

222227
def get_valkey_connection():
223228
if from_url:
@@ -235,7 +240,7 @@ def test_valkey_connection_pool(request, from_url):
235240
have auto_close_connection_pool set to False"""
236241

237242
url: str = request.config.getoption("--valkey-url")
238-
url_args = parse_url(url, True)
243+
url_args = parse_url(url)
239244

240245
pool = None
241246

@@ -267,7 +272,7 @@ def test_valkey_from_pool(request, from_url):
267272
have auto_close_connection_pool set to True"""
268273

269274
url: str = request.config.getoption("--valkey-url")
270-
url_args = parse_url(url, True)
275+
url_args = parse_url(url)
271276

272277
pool = None
273278

@@ -344,7 +349,7 @@ def test_unix_socket_connection_failure():
344349

345350

346351
def test_parsing_unix_socket_relative_path():
347-
parsed = parse_url("unix:./valkey.sock", False)
352+
parsed = parse_url("unix:./valkey.sock")
348353
assert parsed["path"] == "./valkey.sock"
349354
assert parsed["connection_class"] is UnixDomainSocketConnection
350355
assert len(parsed) == 2

valkey/asyncio/cluster.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@
2525
DEFAULT_EVICTION_POLICY,
2626
AbstractCache,
2727
)
28-
from valkey._parsers import AsyncCommandsParser, Encoder, parse_url
28+
from valkey._parsers import AsyncCommandsParser, Encoder
2929
from valkey._parsers.helpers import (
3030
_ValkeyCallbacks,
3131
_ValkeyCallbacksRESP2,
3232
_ValkeyCallbacksRESP3,
3333
)
3434
from valkey.asyncio.client import ResponseCallbackT
35-
from valkey.asyncio.connection import Connection, DefaultParser, SSLConnection
35+
from valkey.asyncio.connection import (
36+
Connection,
37+
DefaultParser,
38+
SSLConnection,
39+
parse_url,
40+
)
3641
from valkey.asyncio.lock import Lock
3742
from valkey.asyncio.retry import Retry
3843
from valkey.backoff import default_backoff
@@ -214,7 +219,7 @@ def from_url(cls, url: str, **kwargs: Any) -> "ValkeyCluster":
214219
:class:`~valkey.asyncio.connection.Connection` when created.
215220
In the case of conflicting arguments, querystring arguments are used.
216221
"""
217-
kwargs.update(parse_url(url, True))
222+
kwargs.update(parse_url(url))
218223
if kwargs.pop("connection_class", None) is SSLConnection:
219224
kwargs["ssl"] = True
220225
return cls(**kwargs)

valkey/asyncio/connection.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@
7171
SYM_EMPTY = b""
7272

7373

74+
def parse_url(url: str):
75+
from .._parsers.url_parser import parse_url
76+
77+
return parse_url(url, async_connection=True)
78+
79+
7480
class _Sentinel(enum.Enum):
7581
sentinel = object()
7682

@@ -1021,9 +1027,8 @@ def from_url(cls: Type[_CP], url: str, **kwargs) -> _CP:
10211027
class initializer. In the case of conflicting arguments, querystring
10221028
arguments always win.
10231029
"""
1024-
from .._parsers.url_parser import parse_url
10251030

1026-
url_options = parse_url(url, True)
1031+
url_options = parse_url(url)
10271032
kwargs.update(url_options)
10281033
return cls(**kwargs)
10291034

valkey/cluster.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from collections import OrderedDict
77
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
88

9-
from valkey._parsers import CommandsParser, Encoder, parse_url
9+
from valkey._parsers import CommandsParser, Encoder
1010
from valkey._parsers.helpers import parse_scan
1111
from valkey.backoff import default_backoff
1212
from valkey.client import CaseInsensitiveDict, PubSub, Valkey
1313
from valkey.commands import READ_COMMANDS, ValkeyClusterCommands
1414
from valkey.commands.helpers import list_or_args
15-
from valkey.connection import ConnectionPool, DefaultParser
15+
from valkey.connection import ConnectionPool, DefaultParser, parse_url
1616
from valkey.crc import VALKEY_CLUSTER_HASH_SLOTS, key_slot
1717
from valkey.exceptions import (
1818
AskError,
@@ -580,7 +580,7 @@ def __init__(
580580
from_url = False
581581
if url is not None:
582582
from_url = True
583-
url_options = parse_url(url, False)
583+
url_options = parse_url(url)
584584
if "path" in url_options:
585585
raise ValkeyClusterException(
586586
"ValkeyCluster does not currently support Unix Domain "

valkey/connection.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
DefaultParser = _RESP2Parser
7575

7676

77+
def parse_url(url: str):
78+
from ._parsers.url_parser import parse_url
79+
80+
return parse_url(url, async_connection=False)
81+
82+
7783
class LibvalkeyRespSerializer:
7884
def pack(self, *args) -> list[bytes]:
7985
"""Pack a series of arguments into the Valkey protocol"""
@@ -1001,9 +1007,8 @@ def from_url(cls, url, **kwargs):
10011007
class initializer. In the case of conflicting arguments, querystring
10021008
arguments always win.
10031009
"""
1004-
from ._parsers.url_parser import parse_url
10051010

1006-
url_options = parse_url(url, False)
1011+
url_options = parse_url(url)
10071012
if "connection_class" in kwargs:
10081013
url_options["connection_class"] = kwargs["connection_class"]
10091014

0 commit comments

Comments
 (0)