Fix Issue 5: Create NFOServiceFactory for centralized initialization
- Created NFOServiceFactory in src/core/services/nfo_factory.py - Enforces configuration precedence: explicit params > ENV > config.json - Provides create() and create_optional() methods - Singleton factory instance via get_nfo_factory() - Updated 4 files to use factory (nfo.py, SeriesApp.py, series_manager_service.py, nfo_cli.py) - Fixed test mocks: added ensure_folder_with_year(), corrected dependency test - Tests: 17/18 NFO passing, 15/16 anime passing - Resolves Code Duplication 2 (NFO initialization)
This commit is contained in:
@@ -14,6 +14,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from src.config.settings import settings
|
||||
from src.core.entities.series import Serie
|
||||
from src.core.SeriesApp import SeriesApp
|
||||
from src.core.services.nfo_factory import get_nfo_factory
|
||||
from src.core.services.nfo_service import NFOService
|
||||
from src.core.services.tmdb_client import TMDBAPIError
|
||||
from src.server.models.nfo import (
|
||||
@@ -46,32 +47,16 @@ async def get_nfo_service() -> NFOService:
|
||||
Raises:
|
||||
HTTPException: If NFO service not configured
|
||||
"""
|
||||
# Check if TMDB API key is in settings
|
||||
tmdb_api_key = settings.tmdb_api_key
|
||||
|
||||
# If not in settings, try to load from config.json
|
||||
if not tmdb_api_key:
|
||||
try:
|
||||
from src.server.services.config_service import get_config_service
|
||||
config_service = get_config_service()
|
||||
config = config_service.load_config()
|
||||
if config.nfo and config.nfo.tmdb_api_key:
|
||||
tmdb_api_key = config.nfo.tmdb_api_key
|
||||
except Exception:
|
||||
pass # Config loading failed, tmdb_api_key remains None
|
||||
|
||||
if not tmdb_api_key:
|
||||
try:
|
||||
# Use centralized factory for consistent initialization
|
||||
factory = get_nfo_factory()
|
||||
return factory.create()
|
||||
except ValueError as e:
|
||||
# Factory raises ValueError if API key not configured
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="NFO service not configured. TMDB API key required."
|
||||
)
|
||||
|
||||
return NFOService(
|
||||
tmdb_api_key=tmdb_api_key,
|
||||
anime_directory=settings.anime_directory,
|
||||
image_size=settings.nfo_image_size,
|
||||
auto_create=settings.nfo_auto_create
|
||||
)
|
||||
detail=str(e)
|
||||
) from e
|
||||
|
||||
|
||||
@router.get("/{serie_id}/check", response_model=NFOCheckResponse)
|
||||
|
||||
@@ -165,7 +165,7 @@ async def get_optional_database_session() -> AsyncGenerator:
|
||||
"""
|
||||
try:
|
||||
from src.server.database import get_db_session
|
||||
|
||||
|
||||
# Try to get a session - if database not initialized, this will raise RuntimeError
|
||||
async with get_db_session() as session:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user