-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathsetup.py
More file actions
63 lines (51 loc) · 1.86 KB
/
Copy pathsetup.py
File metadata and controls
63 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Setup API Routes
----------------
Endpoints for initial application setup (uploading credentials).
"""
import json
import logging
import os
from fastapi import APIRouter, File, HTTPException, UploadFile, status
from app.core import settings
router = APIRouter(prefix="/api", tags=["Setup"])
logger = logging.getLogger(__name__)
@router.post("/setup")
async def setup_credentials(file: UploadFile = File(...)):
"""Upload credentials.json file."""
try:
content = await file.read()
# Validate JSON structure
try:
data = json.loads(content)
except json.JSONDecodeError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid JSON file",
)
# Validate content (must be Google OAuth credentials)
if "installed" not in data and "web" not in data:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid credentials file. Must contain 'installed' or 'web' client configuration.",
)
# Ensure directory exists
os.makedirs(
os.path.dirname(os.path.abspath(settings.credentials_file)), exist_ok=True
)
# Save to settings.credentials_file
with open(settings.credentials_file, "wb") as f:
f.write(content)
logger.info(f"Credentials uploaded successfully to {settings.credentials_file}")
return {
"message": "Credentials uploaded successfully",
"path": settings.credentials_file,
}
except HTTPException:
raise
except Exception as e:
logger.exception("Error uploading credentials")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to upload credentials: {str(e)}",
)