🔧 chore(crud.py): change return type of check_key function from bool to Optional[ApiKey] to provide more information about the API key

🔧 chore(crud.py): rename api_key variable to api_key_object in check_key function for better clarity
🔧 chore(crud.py): change return value of check_key function from True to api_key_object.user to provide the associated user with the API key
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-28 07:01:01 -03:00
commit 59a08cd645

View file

@ -2,7 +2,7 @@ import datetime
import secrets import secrets
import threading import threading
from uuid import UUID from uuid import UUID
from typing import List from typing import List, Optional
from sqlmodel import Session, select from sqlmodel import Session, select
from langflow.services.database.models.api_key import ( from langflow.services.database.models.api_key import (
ApiKey, ApiKey,
@ -46,21 +46,21 @@ def delete_api_key(session: Session, api_key_id: UUID) -> None:
session.commit() session.commit()
def check_key(session: Session, api_key: str) -> bool: def check_key(session: Session, api_key: str) -> Optional[ApiKey]:
"""Check if the API key is valid.""" """Check if the API key is valid."""
query = select(ApiKey).where(ApiKey.api_key == api_key) query = select(ApiKey).where(ApiKey.api_key == api_key)
api_key = session.exec(query).first() api_key_object: Optional[ApiKey] = session.exec(query).first()
if api_key is None: if api_key_object is None:
return False return api_key_object
threading.Thread( threading.Thread(
target=update_total_uses, target=update_total_uses,
args=( args=(
session, session,
api_key, api_key_object,
), ),
).start() ).start()
return True return api_key_object.user
def update_total_uses(session, api_key: ApiKey): def update_total_uses(session, api_key: ApiKey):