refactor: Centralize API base URL and clean up

Replaces hardcoded Google API base URLs with `settings.BASE_URL` for improved configurability and maintainability across services.

Removes unused imports and variables from various modules to reduce code bloat and enhance readability.
This commit is contained in:
cxyfer
2025-07-16 04:50:55 +08:00
parent 8e0a834daa
commit 5d55325c12
7 changed files with 14 additions and 21 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ def _extract_file_references(contents: List[Dict[str, Any]]) -> List[str]:
file_uri = file_data["fileUri"]
# 從 URI 中提取文件名
# 1. https://generativelanguage.googleapis.com/v1beta/files/{file_id}
match = re.match(r"https://generativelanguage.googleapis.com/v1beta/(files/.*)", file_uri)
match = re.match(rf"{re.escape(settings.BASE_URL)}/(files/.*)", file_uri)
if not match:
logger.warning(f"Invalid file URI: {file_uri}")
continue
+3 -4
View File
@@ -2,14 +2,13 @@
文件上传处理器
处理 Google 的可恢复上传协议
"""
import hashlib
from typing import Dict, Any, Optional
from typing import Optional
from datetime import datetime, timezone, timedelta
from httpx import AsyncClient
from fastapi import Request, Response, HTTPException
from fastapi.responses import StreamingResponse
from app.config.config import settings
from app.database import services as db_services
from app.database.models import FileState
from app.log.logger import get_files_logger
@@ -136,7 +135,7 @@ class FileUploadHandler:
mime_type=file_data.get("mimeType", session_info["mime_type"]),
size_bytes=int(file_data.get("sizeBytes", session_info["size_bytes"])),
api_key=session_info["api_key"],
uri=file_data.get("uri", f"https://generativelanguage.googleapis.com/v1beta/{real_file_name}"),
uri=file_data.get("uri", f"{settings.BASE_URL}/{real_file_name}"),
create_time=now,
update_time=now,
expiration_time=datetime.fromisoformat(expiration_time_str),
+7 -10
View File
@@ -2,17 +2,14 @@
文件管理服务
"""
import json
import hashlib
import uuid
from datetime import datetime, timedelta, timezone
from typing import Optional, Dict, Any, Tuple, List
from httpx import AsyncClient, Headers
from collections import defaultdict
from typing import Optional, Dict, Any, Tuple
from httpx import AsyncClient
import asyncio
from app.config.config import settings
from app.database import services as db_services
from app.database.models import FileRecord, FileState
from app.database.models import FileState
from app.domain.file_models import FileMetadata, ListFilesResponse
from fastapi import HTTPException
from app.log.logger import get_files_logger
@@ -254,7 +251,7 @@ class FilesService:
async with AsyncClient() as client:
response = await client.get(
f"https://generativelanguage.googleapis.com/v1beta/{file_name}",
f"{settings.BASE_URL}/{file_name}",
params={"key": api_key}
)
@@ -383,7 +380,7 @@ class FilesService:
async with AsyncClient() as client:
response = await client.delete(
f"https://generativelanguage.googleapis.com/v1beta/{file_name}",
f"{settings.BASE_URL}/{file_name}",
params={"key": api_key}
)
@@ -422,7 +419,7 @@ class FilesService:
try:
async with AsyncClient() as client:
response = await client.get(
f"https://generativelanguage.googleapis.com/v1beta/{file_name}",
f"{settings.BASE_URL}/{file_name}",
params={"key": api_key}
)
@@ -475,7 +472,7 @@ class FilesService:
async with AsyncClient() as client:
await client.delete(
f"https://generativelanguage.googleapis.com/v1beta/{file_name}",
f"{settings.BASE_URL}/{file_name}",
params={"key": api_key}
)
except Exception as e: