|
| 1 | +from fastapi import APIRouter, HTTPException |
| 2 | +from google import genai |
| 3 | +from loguru import logger |
| 4 | + |
| 5 | +from app.api.models.validation import BaseValidationInput, BaseValidationResponse, PosterRatingValidationInput |
| 6 | +from app.services.poster_ratings.factory import PosterProvider, poster_ratings_factory |
| 7 | +from app.services.simkl import simkl_service |
| 8 | +from app.services.tmdb.client import TMDBClient |
| 9 | + |
| 10 | +router = APIRouter(tags=["Validation"]) |
| 11 | + |
| 12 | + |
| 13 | +@router.post("/gemini/validation") |
| 14 | +async def validate_gemini_api_key(data: BaseValidationInput) -> BaseValidationResponse: |
| 15 | + try: |
| 16 | + client = genai.Client(api_key=data.api_key.strip()) |
| 17 | + await client.aio.models.list() |
| 18 | + return BaseValidationResponse(valid=True, message="Gemini API key is valid") |
| 19 | + except Exception as e: |
| 20 | + logger.debug(f"Gemini API key validation failed: {e}") |
| 21 | + return BaseValidationResponse(valid=False, message="Invalid Gemini API key") |
| 22 | + |
| 23 | + |
| 24 | +@router.post("/tmdb/validation") |
| 25 | +async def validate_tmdb_api_key(data: BaseValidationInput) -> BaseValidationResponse: |
| 26 | + try: |
| 27 | + client = TMDBClient(api_key=data.api_key.strip(), language="en-US") |
| 28 | + await client.get("/configuration") |
| 29 | + await client.close() |
| 30 | + return BaseValidationResponse(valid=True, message="TMDB API key is valid") |
| 31 | + except Exception as e: |
| 32 | + logger.debug(f"TMDB API key validation failed: {e}") |
| 33 | + return BaseValidationResponse(valid=False, message="Invalid TMDB API key") |
| 34 | + |
| 35 | + |
| 36 | +@router.post("/poster-rating/validate") |
| 37 | +async def validate_poster_rating_api_key(payload: PosterRatingValidationInput) -> BaseValidationResponse: |
| 38 | + if not payload.api_key or not payload.api_key.strip(): |
| 39 | + return BaseValidationResponse(valid=False, message="API key cannot be empty") |
| 40 | + |
| 41 | + try: |
| 42 | + provider_enum = PosterProvider(payload.provider) |
| 43 | + except ValueError: |
| 44 | + raise HTTPException(status_code=400, detail=f"Invalid provider: {payload.provider}") |
| 45 | + |
| 46 | + try: |
| 47 | + if provider_enum == PosterProvider.RPDB: |
| 48 | + is_valid = await poster_ratings_factory.rpdb_service.validate_api_key(payload.api_key.strip()) |
| 49 | + elif provider_enum == PosterProvider.TOP_POSTERS: |
| 50 | + is_valid = await poster_ratings_factory.top_posters_service.validate_api_key(payload.api_key.strip()) |
| 51 | + else: |
| 52 | + raise HTTPException(status_code=400, detail=f"Unsupported provider: {payload.provider}") |
| 53 | + |
| 54 | + if is_valid: |
| 55 | + return BaseValidationResponse(valid=True, message="API key is valid") |
| 56 | + return BaseValidationResponse(valid=False, message="Invalid API key") |
| 57 | + except Exception as e: |
| 58 | + logger.error(f"Validation failed: {str(e)}") |
| 59 | + raise HTTPException(status_code=500, detail="Validation failed due to an internal error.") |
| 60 | + |
| 61 | + |
| 62 | +@router.post("/simkl/validation") |
| 63 | +async def validate_simkl_api_key(data: BaseValidationInput) -> BaseValidationResponse: |
| 64 | + try: |
| 65 | + response = await simkl_service.get_trending(data.api_key) |
| 66 | + if response: |
| 67 | + return BaseValidationResponse(valid=True, message="Valid API Key") |
| 68 | + return BaseValidationResponse(valid=False, message="Invalid API Key") |
| 69 | + except Exception as e: |
| 70 | + logger.error(f"Validation failed: {str(e)}") |
| 71 | + raise HTTPException(status_code=500, detail="Validation failed due to an internal error.") |
0 commit comments