Skip to content

Commit

Permalink
Added factory method to create a new client initilized with connection
Browse files Browse the repository at this point in the history
  • Loading branch information
barshaul committed Jul 26, 2022
1 parent e32382d commit 635b11a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 49 deletions.
9 changes: 2 additions & 7 deletions python/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
from babushka import Client

from src.async_client import RedisAsyncClient
from src.config import ClientConfiguration, ClusterClientConfiguration
from src.config import ClientConfiguration

__all__ = [
"RedisAsyncClient",
"Client",
"ClientConfiguration",
"ClusterClientConfiguration",
]
__all__ = ["RedisAsyncClient", "Client", "ClientConfiguration"]
15 changes: 8 additions & 7 deletions python/src/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@


class RedisAsyncClient(CoreCommands):
def __init__(
self,
config: ClientConfiguration = ClientConfiguration.get_default_config(),
@classmethod
async def create(
cls, config: ClientConfiguration = ClientConfiguration.get_default_config()
):
self = RedisAsyncClient()
self.config = config
self.connection = None
self.connection = await self._create_multiplexed_conn()
return self

async def create_multiplexed_conn(self):
self.connection = await Client.new(to_url(**self.config.config_args))
return self.connection
async def _create_multiplexed_conn(self):
return await Client.new(to_url(**self.config.config_args))

async def execute_command(self, command, *args, **kwargs):
conn_rust_func = getattr(self.connection, command)
Expand Down
21 changes: 0 additions & 21 deletions python/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,3 @@ def get_default_config():
return ClientConfiguration(
host=DEFAULT_HOST, port=DEFAULT_PORT, db=0, tls_enabled=False
)


class ClusterClientConfiguration(BaseClientConfiguration):
CLUSTER_SPECIFIC_KEYS = {"read_from_replicas"}

def __init__(self, **kwargs):
allowed_keys = ClusterClientConfiguration.CLUSTER_SPECIFIC_KEYS.copy().union(
BASE_ALLOWED_KEYS
)
super(ClusterClientConfiguration, self).__init__(
allowed_keys=allowed_keys, **kwargs
)

@staticmethod
def get_default_config():
return ClusterClientConfiguration(
host=DEFAULT_HOST,
port=DEFAULT_PORT,
tls_enabled=False,
read_from_replicas=False,
)
8 changes: 3 additions & 5 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from src.async_client import RedisAsyncClient
from src.config import ClusterClientConfiguration
from src.config import ClientConfiguration

default_host = "localhost"
default_port = 6379
Expand All @@ -28,7 +28,5 @@ async def async_client(request):
"Get async client for tests"
host = request.config.getoption("--host")
port = request.config.getoption("--port")
config = ClusterClientConfiguration(host=host, port=port)
client = RedisAsyncClient(config)
await client.create_multiplexed_conn()
return client
config = ClientConfiguration(host=host, port=port)
return await RedisAsyncClient.create(config)
10 changes: 1 addition & 9 deletions python/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
from src.config import (DEFAULT_HOST, DEFAULT_PORT, ClientConfiguration,
ClusterClientConfiguration)


def test_default_cluster_config():
config = ClusterClientConfiguration.get_default_config()
assert config.config_args["host"] == DEFAULT_HOST
assert config.config_args["port"] == DEFAULT_PORT
assert config.config_args["read_from_replicas"] is False
from src.config import DEFAULT_HOST, DEFAULT_PORT, ClientConfiguration


def test_default_client_config():
Expand Down

0 comments on commit 635b11a

Please sign in to comment.