"""Dependency injection for FastAPI. Provides singleton-like access to SSH and Jellyfin clients via FastAPI's dependency system. Uses lru_cache so connections are reused across requests. """ from __future__ import annotations from functools import lru_cache from clients.jellyfin import JellyfinClient from clients.ssh import RemoteSSHClient from config import get_settings @lru_cache def get_jellyfin_client() -> JellyfinClient: """Return a cached Jellyfin client.""" settings = get_settings() return JellyfinClient(settings.jellyfin.url, settings.jellyfin.api_key) @lru_cache def get_ssh_client() -> RemoteSSHClient: """Return a cached SSH client (connects on first use).""" settings = get_settings() client = RemoteSSHClient( host=settings.ssh.host, username=settings.ssh.username, port=settings.ssh.port, key_filename=settings.ssh.key_filename or None, password=settings.ssh.password or None, ) client.connect() return client def get_user_id() -> str: """Return the configured Jellyfin user ID, or discover the first available user.""" settings = get_settings() if settings.jellyfin.user_id: return settings.jellyfin.user_id client = get_jellyfin_client() users = client.users() if not users: raise RuntimeError("No Jellyfin users found and JELLYFIN_USER_ID not set") return users[0]["Id"]