-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more volume to the python client, orgenaized the tests and src …
…code
- Loading branch information
Showing
10 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Please use | ||
python setup.py install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |