Skip to content

Commit

Permalink
Added more volume to the python client, orgenaized the tests and src …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
barshaul committed Jul 21, 2022
1 parent 9d27ebb commit feb6f46
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 9 deletions.
2 changes: 2 additions & 0 deletions python/INSTALL
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Please use
python setup.py install
22 changes: 22 additions & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from setuptools import find_packages, setup

setup(
name="pybabushka",
description="Python client for Redis database and key-value store based on redis-rs",
long_description=open("README.md").read().strip(),
long_description_content_type="text/markdown",
keywords=["Redis", "Babushka", "key-value store", "database"],
license="",
version="1.0.0",
packages=find_packages(include=["src", "src.commands"]),
setup_requires=["pytest-runner"],
tests_require=["pytest"],
url="https://github.com/barshaul/babushka",
python_requires=">=3.6",
install_requires=[
"deprecated>=1.2.3",
"packaging>=20.4",
'typing-extensions; python_version<"3.8"',
"async-timeout>=4.0.2",
],
)
5 changes: 5 additions & 0 deletions python/src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from babushka import Client

from src.client import RedisAsyncClient, RedisClient

__all__ = ["RedisAsyncClient", "RedisClient", "Client"]
40 changes: 40 additions & 0 deletions python/src/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from babushka import Client

from src.commands.core import CoreCommands
from src.utils import to_url


class RedisClient(CoreCommands):
def __init__(
self, host="localhost", port=6379, tls_enabled=False, user="", password=""
):
self.host = host
self.port = port
self.user = user
self.password = password
self.tls_enabled = tls_enabled
self.connection = None

def create_multiplexed_conn(self):
raise NotImplementedError

def execute_command(self, command, *args, **kwargs):
conn_rust_func = getattr(self.connection, command.lower())
conn_rust_func(*args, **kwargs)


class RedisAsyncClient(RedisClient):
def __init__(
self, host="localhost", port=6379, tls_enabled=False, user="", password=""
):
super(RedisAsyncClient, self).__init__(host, port, tls_enabled, user, password)

async def create_multiplexed_conn(self):
self.connection = await Client.new(
to_url(self.host, self.port, self.user, self.password, self.tls_enabled)
)
return self.connection

async def execute_command(self, command, *args, **kwargs):
conn_rust_func = getattr(self.connection, command.lower())
return await conn_rust_func(*args, **kwargs)
6 changes: 6 additions & 0 deletions python/src/commands/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CoreCommands:
def get(self, key, *args, **kwargs):
return self.execute_command("GET", key, *args, **kwargs)

def set(self, key, value, *args, **kwargs):
return self.execute_command("SET", key, value, *args, **kwargs)
7 changes: 7 additions & 0 deletions python/src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def to_url(host, port=6379, user="", password="", tls=False):
protocol = "rediss" if tls else "redis"
auth = ""
if user or password:
auth = f"{user}{':' if user else ''}"
auth += f"{password}{'@' if password else ''}"
return f"{protocol}://{auth}{host}:{port}"
Empty file added python/tests/__init__.py
Empty file.
9 changes: 0 additions & 9 deletions python/tests/babushka_test.py

This file was deleted.

31 changes: 31 additions & 0 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from src.client import RedisAsyncClient

default_host = "localhost"
default_port = 6379


def pytest_addoption(parser):
parser.addoption(
"--host",
default=default_host,
action="store",
help="Redis host endpoint," " defaults to `%(default)s`",
)

parser.addoption(
"--port",
default=default_port,
action="store",
help="Redis port," " defaults to `%(default)s`",
)


@pytest.fixture()
async def async_client(request):
"Get async client for tests"
host = request.config.getoption("--host")
port = request.config.getoption("--port")
client = RedisAsyncClient(host, port)
return await client.create_multiplexed_conn()
12 changes: 12 additions & 0 deletions python/tests/test_babushka.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
from datetime import datetime

from src.client import RedisAsyncClient


@pytest.mark.asyncio
async def test_set_get(async_client):
time_str = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
await async_client.set("key", time_str)
result = await async_client.get("key")
assert result == time_str

0 comments on commit feb6f46

Please sign in to comment.