2023-08-02 00:03:47 +03:00
|
|
|
from datetime import datetime
|
2023-12-04 01:32:37 +03:00
|
|
|
from typing import Optional
|
2023-08-21 15:05:13 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
2023-12-01 00:29:28 +03:00
|
|
|
from models.settings import get_supabase_client
|
2023-12-04 01:32:37 +03:00
|
|
|
from modules.api_key.entity.api_key import ApiKey
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.api_key.repository.api_key_interface import ApiKeysInterface
|
2023-08-02 00:03:47 +03:00
|
|
|
|
|
|
|
|
2023-12-01 00:29:28 +03:00
|
|
|
class ApiKeys(ApiKeysInterface):
|
|
|
|
def __init__(self):
|
|
|
|
supabase_client = get_supabase_client()
|
2023-09-14 18:42:15 +03:00
|
|
|
self.db = supabase_client # type: ignore
|
2023-08-02 00:03:47 +03:00
|
|
|
|
2023-12-04 01:32:37 +03:00
|
|
|
def create_api_key(
|
|
|
|
self, new_key_id, new_api_key, user_id, name, days=30, only_chat=False
|
|
|
|
) -> Optional[ApiKey]:
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.insert(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"key_id": str(new_key_id),
|
|
|
|
"user_id": str(user_id),
|
|
|
|
"api_key": str(new_api_key),
|
2023-12-04 01:32:37 +03:00
|
|
|
"name": str(name),
|
|
|
|
"days": int(days),
|
|
|
|
"only_chat": bool(only_chat),
|
2023-08-02 00:03:47 +03:00
|
|
|
"creation_time": datetime.utcnow().strftime(
|
|
|
|
"%Y-%m-%d %H:%M:%S"
|
|
|
|
),
|
|
|
|
"is_active": True,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.execute()
|
|
|
|
)
|
2023-12-04 01:32:37 +03:00
|
|
|
if len(response.data) == 0:
|
|
|
|
return None
|
|
|
|
return ApiKey(**response.data[0])
|
2023-08-02 00:03:47 +03:00
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def delete_api_key(self, key_id: str, user_id: UUID):
|
2023-08-02 00:03:47 +03:00
|
|
|
return (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.update(
|
|
|
|
{
|
|
|
|
"is_active": False,
|
|
|
|
"deleted_time": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.match({"key_id": key_id, "user_id": user_id})
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def get_active_api_key(self, api_key: str):
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.select("api_key", "creation_time")
|
|
|
|
.filter("api_key", "eq", api_key)
|
2023-12-04 01:32:37 +03:00
|
|
|
.filter("is_active", "eq", str(True))
|
2023-08-02 00:03:47 +03:00
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def get_user_id_by_api_key(self, api_key: str):
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.select("user_id")
|
|
|
|
.filter("api_key", "eq", api_key)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2023-12-01 00:29:28 +03:00
|
|
|
def get_user_api_keys(self, user_id):
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.select("key_id, creation_time")
|
|
|
|
.filter("user_id", "eq", user_id)
|
|
|
|
.filter("is_active", "eq", True)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response.data
|