🔧 fix(api_key.py): import missing dependencies and fix type annotations in api_key.py
🔧 fix(api_key.py): fix type annotations and import in api_key.py 🔧 fix(api_key.py): fix type annotations and
This commit is contained in:
parent
950b9d179b
commit
a4aeff6d79
3 changed files with 18 additions and 13 deletions
|
|
@ -1,7 +1,11 @@
|
|||
from uuid import UUID
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from langflow.api.v1.schemas import ApiKeysResponse, CreateApiKeyRequest
|
||||
from langflow.api.v1.schemas import ApiKeysResponse
|
||||
from langflow.services.auth.utils import get_current_active_user
|
||||
from langflow.services.database.models.api_key.api_key import UnmaskedApiKeyRead
|
||||
from langflow.services.database.models.api_key.api_key import (
|
||||
ApiKeyCreate,
|
||||
UnmaskedApiKeyRead,
|
||||
)
|
||||
|
||||
# Assuming you have these methods in your service layer
|
||||
from langflow.services.database.models.api_key.crud import (
|
||||
|
|
@ -26,15 +30,14 @@ def get_api_keys_route(
|
|||
user_id = current_user.id
|
||||
keys = get_api_keys(db, user_id)
|
||||
|
||||
result = {"total_count": len(keys), "user_id": user_id, "api_keys": keys}
|
||||
return ApiKeysResponse(**result)
|
||||
return ApiKeysResponse(total_count=len(keys), user_id=user_id, api_keys=keys)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/api_key", response_model=UnmaskedApiKeyRead)
|
||||
def create_api_key_route(
|
||||
req: CreateApiKeyRequest,
|
||||
req: ApiKeyCreate,
|
||||
current_user: User = Depends(get_current_active_user),
|
||||
db: Session = Depends(get_session),
|
||||
):
|
||||
|
|
@ -47,7 +50,7 @@ def create_api_key_route(
|
|||
|
||||
@router.delete("/api_key/{api_key_id}")
|
||||
def delete_api_key_route(
|
||||
api_key_id: str,
|
||||
api_key_id: UUID,
|
||||
current_user=Depends(get_current_active_user),
|
||||
db: Session = Depends(get_session),
|
||||
):
|
||||
|
|
|
|||
|
|
@ -10,15 +10,14 @@ if TYPE_CHECKING:
|
|||
|
||||
|
||||
class ApiKeyBase(SQLModelSerializable):
|
||||
api_key: str = Field(index=True, unique=True)
|
||||
name: Optional[str] = Field(index=True)
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
last_used_at: Optional[datetime] = Field(default=None)
|
||||
user_id: UUID = Field()
|
||||
|
||||
|
||||
class ApiKey(ApiKeyBase, table=True):
|
||||
id: UUID = Field(default_factory=uuid4, primary_key=True, unique=True)
|
||||
api_key: str = Field(index=True, unique=True)
|
||||
# User relationship
|
||||
user_id: UUID = Field(index=True, foreign_key="user.id")
|
||||
user: "User" = Relationship(back_populates="api_keys")
|
||||
|
|
@ -31,12 +30,14 @@ class ApiKeyCreate(ApiKeyBase):
|
|||
|
||||
class UnmaskedApiKeyRead(ApiKeyBase):
|
||||
id: UUID
|
||||
api_key: str = Field(index=True, unique=True)
|
||||
user_id: UUID = Field()
|
||||
|
||||
|
||||
class ApiKeyRead(ApiKeyBase):
|
||||
id: UUID
|
||||
api_key: Optional[str] = None
|
||||
user_id: Optional[UUID] = None
|
||||
api_key: str = Field(index=True, unique=True)
|
||||
user_id: UUID = Field()
|
||||
|
||||
@validator("api_key", always=True)
|
||||
def mask_api_key(cls, v):
|
||||
|
|
|
|||
|
|
@ -11,14 +11,14 @@ from langflow.services.database.models.api_key import (
|
|||
)
|
||||
|
||||
|
||||
def get_api_keys(session: Session, user_id: str) -> List[UnmaskedApiKeyRead]:
|
||||
def get_api_keys(session: Session, user_id: UUID) -> List[ApiKeyRead]:
|
||||
query = select(ApiKey).where(ApiKey.user_id == user_id)
|
||||
api_keys = session.exec(query).all()
|
||||
return [ApiKeyRead.from_orm(api_key) for api_key in api_keys]
|
||||
|
||||
|
||||
def create_api_key(
|
||||
session: Session, api_key_create: ApiKeyCreate, user_id: str
|
||||
session: Session, api_key_create: ApiKeyCreate, user_id: UUID
|
||||
) -> UnmaskedApiKeyRead:
|
||||
# Generate a random API key with 32 bytes of randomness
|
||||
generated_api_key = secrets.token_urlsafe(32)
|
||||
|
|
@ -26,7 +26,8 @@ def create_api_key(
|
|||
# hash the API key
|
||||
hashed_api_key = get_password_hash(generated_api_key)
|
||||
# Use the generated key to create the ApiKey object
|
||||
api_key = ApiKey(api_key=hashed_api_key, **api_key_create.dict(), user_id=user_id)
|
||||
|
||||
api_key = ApiKey(api_key=hashed_api_key, name=api_key_create.name, user_id=user_id)
|
||||
|
||||
session.add(api_key)
|
||||
session.commit()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue