Files
home-automation/app/dependencies.py
T

47 lines
1.5 KiB
Python
Raw Normal View History

2026-04-19 20:19:58 +02:00
from collections.abc import Generator
2026-04-20 15:16:47 +02:00
from fastapi import Depends, Request
2026-04-19 20:19:58 +02:00
from sqlalchemy.orm import Session
2026-04-20 15:16:47 +02:00
from app.auth_db import get_auth_db_session
2026-04-19 20:19:58 +02:00
from app.config import Settings, get_settings
from app.db import get_db_session
from app.integrations.homeassistant import HomeAssistantClient
2026-04-20 17:06:03 +02:00
from app.integrations.ticktick import TickTickClient
from app.poo_db import get_poo_db_session
2026-04-20 15:16:47 +02:00
from app.services.auth import AuthenticatedSession, get_authenticated_session
from app.services.config_page import build_runtime_settings
2026-04-19 20:19:58 +02:00
2026-04-20 15:16:47 +02:00
def get_auth_db() -> Generator[Session, None, None]:
yield from get_auth_db_session()
def get_app_settings(session: Session = Depends(get_auth_db)) -> Settings:
return build_runtime_settings(session, get_settings())
2026-04-19 20:19:58 +02:00
def get_db() -> Generator[Session, None, None]:
yield from get_db_session()
def get_poo_db() -> Generator[Session, None, None]:
yield from get_poo_db_session()
def get_homeassistant_client(settings: Settings = Depends(get_app_settings)) -> HomeAssistantClient:
return HomeAssistantClient(settings)
2026-04-20 15:16:47 +02:00
2026-04-20 17:06:03 +02:00
def get_ticktick_client(settings: Settings = Depends(get_app_settings)) -> TickTickClient:
return TickTickClient(settings)
2026-04-20 15:16:47 +02:00
def get_current_auth_session(
request: Request,
session: Session = Depends(get_auth_db),
settings: Settings = Depends(get_app_settings),
) -> AuthenticatedSession | None:
raw_token = request.cookies.get(settings.auth_session_cookie_name)
return get_authenticated_session(session, raw_token=raw_token)