Код: Выделить всё
class UserResponse(BaseModel):
id: int
name: str
email: EmailStr
class Config:
from_attributes = True
Код: Выделить всё
, response_model=list[UserResponse])
async def get_users(db: AsyncSession = Depends(get_db), current_user = Depends(get_current_user)):
try:
cache_key = f"users_list:{current_user.id}"
cached = r.get(cache_key)
if cached:
return json.loads(cached)
#IF NOT CACHED → QUERY DATABASE
users = await crud.get_all_users(db, current_user.id)
# # Convert SQLAlchemy objects → dict
serialized = [
{"id": u.id, "name": u.name, "email": u.email}
for u in users
]
# SAVE INTO REDIS WITH TTL (60 seconds)
r.setex(cache_key, 60, json.dumps(serialized))
return serialized
except Exception as e:
logger.error(f"cant show the user: {e}")
raise HTTPException(status_code=500, detail="Database failed")
Код: Выделить всё
async def get_all_users(db: AsyncSession, owner_id: int):
result = await db.execute(select(User).where(User.owner_id == owner_id))
return result.scalars().all()
Код: Выделить всё
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from database import get_db
from models import Auth
from utils.jwt_handler import decode_token
bearer_scheme = HTTPBearer()
async def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme),
db: AsyncSession = Depends(get_db),
):
print("USING AUTH FILE:", __file__)
# Extract token
token = credentials.credentials
# Decode
payload = decode_token(token)
if not payload:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or expired token",
)
email = payload.get("sub")
if not email:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token payload",
)
# Fetch user
result = await db.execute(select(Auth).where(Auth.email == email))
current_user = result.scalars().first()
if not current_user: # FIXED THIS LINE
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User no longer exists",
)
return current_user
Подробнее здесь: https://stackoverflow.com/questions/798 ... ike-object
Мобильная версия