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

98 lines
3.4 KiB
Python
Raw Normal View History

2025-12-17 15:37:32 +01:00
"""
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"
)
2025-12-25 07:44:52 +01:00
@router.patch("", response_model=UserSettings)
2025-12-17 15:37:32 +01:00
async def update_settings(
settings_update: UserSettingsUpdate,
current_user: dict = Depends(get_current_user),
db_client: DBApiClient = Depends(get_db_client)
):
"""
2025-12-25 07:44:52 +01:00
Частично обновить настройки пользователя.
Обновляются только переданные поля. Непереданные поля остаются без изменений.
2025-12-17 15:37:32 +01:00
Args:
2025-12-25 07:44:52 +01:00
settings_update: Частичные настройки для одного или нескольких окружений
2025-12-17 15:37:32 +01:00
Returns:
2025-12-25 07:44:52 +01:00
UserSettings: Обновленные настройки со всеми полями
2025-12-17 15:37:32 +01:00
"""
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"
)