Fix MTGJSON download functionality
- Add refresh router with admin-only endpoints - Fix _decompress_file to handle .psql files correctly - Fix duplicate imports in mtgjson_manager.py - Mount refresh router in main.py - Add download_mtgjson.py standalone script - Add SUPPORTED_FILE_TYPES.md documentation
This commit is contained in:
@@ -12,6 +12,7 @@ from app.routers import games
|
||||
from app.routers import admin
|
||||
from app.routers import card_router
|
||||
from app.routers import interactions
|
||||
from app.routers import refresh
|
||||
|
||||
__all__ = [
|
||||
"auth",
|
||||
@@ -22,4 +23,5 @@ __all__ = [
|
||||
"admin",
|
||||
"card_router",
|
||||
"interactions",
|
||||
"refresh",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
"""
|
||||
MTGJSON Data Refresh Router
|
||||
|
||||
Provides endpoints for triggering dataset downloads and managing refresh operations.
|
||||
"""
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status
|
||||
from typing import Optional
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import get_current_user
|
||||
from app.services.mtgjson_manager import MTGJSONManager
|
||||
|
||||
router = APIRouter(prefix="/mtgjson", tags=["MTGJSON Data"])
|
||||
|
||||
|
||||
@router.post("/refresh")
|
||||
async def trigger_refresh(
|
||||
background_tasks: BackgroundTasks,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
Trigger a refresh of MTGJSON datasets.
|
||||
|
||||
Downloads files from MTGJSON API and upserts data into PostgreSQL.
|
||||
Requires Admin privileges.
|
||||
|
||||
The refresh runs in the background and may take several minutes.
|
||||
"""
|
||||
# Check if current user is admin
|
||||
if current_user.get("privlevel") != "Admin":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Admin privileges required",
|
||||
)
|
||||
|
||||
# Create manager and trigger refresh in background
|
||||
manager = MTGJSONManager()
|
||||
background_tasks.add_task(manager.download_and_refresh, force=False)
|
||||
|
||||
return {
|
||||
"message": "Refresh initiated in background",
|
||||
"status": "started",
|
||||
}
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
async def get_refresh_status(current_user: dict = Depends(get_current_user)):
|
||||
"""
|
||||
Get current refresh status and data health.
|
||||
|
||||
Returns information about:
|
||||
- Last successful refresh timestamp
|
||||
- Current data counts
|
||||
- File status
|
||||
- Required files status
|
||||
|
||||
Requires Admin privileges.
|
||||
"""
|
||||
# Check if current user is admin
|
||||
if current_user.get("privlevel") != "Admin":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Admin privileges required",
|
||||
)
|
||||
|
||||
manager = MTGJSONManager()
|
||||
return await manager.get_health_status()
|
||||
|
||||
|
||||
@router.post("/verify")
|
||||
async def verify_files(current_user: dict = Depends(get_current_user)):
|
||||
"""
|
||||
Verify the integrity of downloaded MTGJSON files.
|
||||
|
||||
Checks that all required files exist and are valid.
|
||||
Requires Admin privileges.
|
||||
"""
|
||||
# Check if current user is admin
|
||||
if current_user.get("privlevel") != "Admin":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Admin privileges required",
|
||||
)
|
||||
|
||||
manager = MTGJSONManager()
|
||||
all_valid, errors = await manager.verify_files()
|
||||
|
||||
if not all_valid:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail={
|
||||
"valid": False,
|
||||
"errors": errors,
|
||||
"message": "Some files failed verification",
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
"valid": True,
|
||||
"message": "All files verified successfully",
|
||||
}
|
||||
Reference in New Issue
Block a user