brief-rags-bench/app/api/v1/settings.py

98 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Settings API endpoints.
Управление настройками пользователя для всех трех окружений (IFT, PSI, PROD).
"""
from fastapi import APIRouter, Depends, HTTPException, status
from app.models.settings import UserSettings, UserSettingsUpdate
from app.interfaces.db_api_client import DBApiClient
from app.dependencies import get_db_client, get_current_user
import httpx
import logging
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/settings", tags=["settings"])
@router.get("", response_model=UserSettings)
async def get_settings(
current_user: dict = Depends(get_current_user),
db_client: DBApiClient = Depends(get_db_client)
):
"""
Получить настройки пользователя для всех окружений.
Returns:
UserSettings: Настройки пользователя для IFT, PSI, PROD
"""
user_id = current_user["user_id"]
try:
settings = await db_client.get_user_settings(user_id)
return settings
except httpx.HTTPStatusError as e:
if e.response.status_code == 404:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User settings not found"
)
logger.error(f"Failed to get user settings: {e}")
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
detail="Failed to retrieve settings from DB API"
)
except Exception as e:
logger.error(f"Unexpected error getting user settings: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Internal server error"
)
@router.patch("", response_model=UserSettings)
async def update_settings(
settings_update: UserSettingsUpdate,
current_user: dict = Depends(get_current_user),
db_client: DBApiClient = Depends(get_db_client)
):
"""
Частично обновить настройки пользователя.
Обновляются только переданные поля. Непереданные поля остаются без изменений.
Args:
settings_update: Частичные настройки для одного или нескольких окружений
Returns:
UserSettings: Обновленные настройки со всеми полями
"""
user_id = current_user["user_id"]
try:
updated_settings = await db_client.update_user_settings(user_id, settings_update)
return updated_settings
except httpx.HTTPStatusError as e:
if e.response.status_code == 404:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
elif e.response.status_code == 400:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid settings format"
)
logger.error(f"Failed to update user settings: {e}")
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
detail="Failed to update settings in DB API"
)
except Exception as e:
logger.error(f"Unexpected error updating user settings: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Internal server error"
)