Spaces:
Runtime error
Runtime error
default path to $ in redis fixed the issue
Browse files
db.py
CHANGED
@@ -3,7 +3,7 @@ from redis import Redis
|
|
3 |
from dotenv import load_dotenv
|
4 |
from uuid import uuid4
|
5 |
|
6 |
-
load_dotenv()
|
7 |
import os
|
8 |
import json
|
9 |
from redis.commands.json.path import Path
|
@@ -26,7 +26,8 @@ def get_client() -> Redis:
|
|
26 |
host=os.environ["REDIS_HOST"],
|
27 |
port=os.environ["REDIS_PORT"],
|
28 |
password=os.environ["REDIS_PASSWORD"],
|
29 |
-
|
|
|
30 |
)
|
31 |
return client
|
32 |
|
@@ -45,12 +46,12 @@ def create_user(client: Redis, user: User):
|
|
45 |
|
46 |
def get_user_by_username(client: Redis, username: str) -> Optional[User]:
|
47 |
uid = client.get(f"user:uuid:by-username:{username}")
|
48 |
-
user = client.json().get(f"user:by-uid:{uid}")
|
49 |
return user
|
50 |
|
51 |
|
52 |
def get_user_chat_by_uid(client: Redis, uid: str) -> List[Chat]:
|
53 |
-
chats = client.json().get(f"chats:by-user-uid:{uid}")
|
54 |
return chats
|
55 |
|
56 |
|
@@ -63,9 +64,9 @@ if __name__ == "__main__":
|
|
63 |
client = get_client()
|
64 |
user = User(username="foo", uid="afa03747-a9ce-4200-964c-7816ec381fa9")
|
65 |
chat = Chat(patient="foo", messages=[("a", "b"), ("c", "d")])
|
66 |
-
|
67 |
create_user(client, user)
|
68 |
user = get_user_by_username(client, user['username'])
|
|
|
69 |
add_chat_by_uid(client, chat, user['uid'])
|
70 |
chats = get_user_chat_by_uid(client, user['uid'])
|
71 |
print(chats)
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
from uuid import uuid4
|
5 |
|
6 |
+
load_dotenv(".env.local")
|
7 |
import os
|
8 |
import json
|
9 |
from redis.commands.json.path import Path
|
|
|
26 |
host=os.environ["REDIS_HOST"],
|
27 |
port=os.environ["REDIS_PORT"],
|
28 |
password=os.environ["REDIS_PASSWORD"],
|
29 |
+
ssl=os.environ.get("REDIS_USE_SSL", "True") == "True",
|
30 |
+
decode_responses=True
|
31 |
)
|
32 |
return client
|
33 |
|
|
|
46 |
|
47 |
def get_user_by_username(client: Redis, username: str) -> Optional[User]:
|
48 |
uid = client.get(f"user:uuid:by-username:{username}")
|
49 |
+
user = client.json().get(f"user:by-uid:{uid}", "$")[0]
|
50 |
return user
|
51 |
|
52 |
|
53 |
def get_user_chat_by_uid(client: Redis, uid: str) -> List[Chat]:
|
54 |
+
chats = client.json().get(f"chats:by-user-uid:{uid}", "$")[0]
|
55 |
return chats
|
56 |
|
57 |
|
|
|
64 |
client = get_client()
|
65 |
user = User(username="foo", uid="afa03747-a9ce-4200-964c-7816ec381fa9")
|
66 |
chat = Chat(patient="foo", messages=[("a", "b"), ("c", "d")])
|
|
|
67 |
create_user(client, user)
|
68 |
user = get_user_by_username(client, user['username'])
|
69 |
+
print("user", user)
|
70 |
add_chat_by_uid(client, chat, user['uid'])
|
71 |
chats = get_user_chat_by_uid(client, user['uid'])
|
72 |
print(chats)
|