mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-07-08 00:01:32 +08:00
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:
@@ -7,7 +7,7 @@ from sqlalchemy import inspect
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database.connection import engine, Base
|
||||
from app.database.models import Settings, FileRecord
|
||||
from app.database.models import Settings
|
||||
from app.log.logger import get_database_logger
|
||||
|
||||
logger = get_database_logger()
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
数据库服务模块
|
||||
"""
|
||||
from typing import List, Optional, Dict, Any, Union
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import func, desc, asc, select, insert, update, delete
|
||||
import json
|
||||
from app.database.connection import database
|
||||
|
||||
@@ -76,8 +76,6 @@ class ServiceUnavailableError(APIError):
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
def setup_exception_handlers(app: FastAPI) -> None:
|
||||
"""
|
||||
设置应用程序的异常处理器
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
Files API 路由
|
||||
"""
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Request, Response, Query, Depends, Body, Header, HTTPException
|
||||
from fastapi import APIRouter, Request, Query, Depends, Header, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.config.config import settings
|
||||
from app.domain.file_models import (
|
||||
FileMetadata,
|
||||
ListFilesResponse,
|
||||
CreateFileRequest,
|
||||
DeleteFileResponse
|
||||
)
|
||||
from app.log.logger import get_files_logger
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user