Spaces:
Sleeping
Sleeping
update
Browse files- api_key_sb.py +2 -2
- proxy.py +29 -3
api_key_sb.py
CHANGED
|
@@ -12,7 +12,7 @@ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
|
| 12 |
|
| 13 |
# 初始化 Supabase 客户端
|
| 14 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 15 |
-
async def
|
| 16 |
try:
|
| 17 |
# 根据用户提供的SQL语句修改,从 'airs_model_api_keys_view' 视图中获取 'api_key'
|
| 18 |
if model:
|
|
@@ -41,7 +41,7 @@ async def get_api_key(model: str = None):
|
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
-
return
|
| 45 |
else:
|
| 46 |
return None
|
| 47 |
except Exception as e:
|
|
|
|
| 12 |
|
| 13 |
# 初始化 Supabase 客户端
|
| 14 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 15 |
+
async def get_api_key_info(model: str = None):
|
| 16 |
try:
|
| 17 |
# 根据用户提供的SQL语句修改,从 'airs_model_api_keys_view' 视图中获取 'api_key'
|
| 18 |
if model:
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
+
return api_key_info
|
| 45 |
else:
|
| 46 |
return None
|
| 47 |
except Exception as e:
|
proxy.py
CHANGED
|
@@ -1,7 +1,21 @@
|
|
| 1 |
import httpx
|
| 2 |
import asyncio
|
| 3 |
from starlette.responses import Response
|
| 4 |
-
from api_key_sb import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
async def do_proxy(url: str, method: str, headers: dict, content: str, max_retries: int = 3):
|
| 7 |
print("Proxy service started.", url)
|
|
@@ -11,8 +25,9 @@ async def do_proxy(url: str, method: str, headers: dict, content: str, max_retri
|
|
| 11 |
for attempt in range(max_retries):
|
| 12 |
|
| 13 |
# 获取API密钥信息
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
api_key_show = api_key[:5]+'*****'+api_key[-5:]
|
| 17 |
print(f"使用API密钥:{api_key_show}")
|
| 18 |
headers["Authorization"] = f"Bearer {api_key}"
|
|
@@ -30,6 +45,17 @@ async def do_proxy(url: str, method: str, headers: dict, content: str, max_retri
|
|
| 30 |
wait_time = int(retry_after) + attempt * 2 # 指数退避基础值
|
| 31 |
print(f"⚠️ 429错误!{wait_time}秒后重试 (尝试:{attempt+1})")
|
| 32 |
await asyncio.sleep(wait_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
continue
|
| 34 |
|
| 35 |
response.raise_for_status() # 触发其他4xx/5xx异常
|
|
|
|
| 1 |
import httpx
|
| 2 |
import asyncio
|
| 3 |
from starlette.responses import Response
|
| 4 |
+
from api_key_sb import get_api_key_info
|
| 5 |
+
from supabase import create_client, Client
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import os
|
| 8 |
+
from datetime import timezone, timedelta, datetime
|
| 9 |
+
from fastapi import FastAPI, Request, HTTPException, Depends, status
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Supabase 配置
|
| 14 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 15 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 16 |
+
|
| 17 |
+
# 初始化 Supabase 客户端
|
| 18 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 19 |
|
| 20 |
async def do_proxy(url: str, method: str, headers: dict, content: str, max_retries: int = 3):
|
| 21 |
print("Proxy service started.", url)
|
|
|
|
| 25 |
for attempt in range(max_retries):
|
| 26 |
|
| 27 |
# 获取API密钥信息
|
| 28 |
+
api_key_info = await get_api_key_info('gemini-2.5-flash')
|
| 29 |
+
api_key = api_key_info.get('api_key')
|
| 30 |
+
api_key_id = api_key_info.get('api_key_id')
|
| 31 |
api_key_show = api_key[:5]+'*****'+api_key[-5:]
|
| 32 |
print(f"使用API密钥:{api_key_show}")
|
| 33 |
headers["Authorization"] = f"Bearer {api_key}"
|
|
|
|
| 45 |
wait_time = int(retry_after) + attempt * 2 # 指数退避基础值
|
| 46 |
print(f"⚠️ 429错误!{wait_time}秒后重试 (尝试:{attempt+1})")
|
| 47 |
await asyncio.sleep(wait_time)
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# 创建北京时间时区对象(UTC+8)
|
| 51 |
+
beijing_tz = timezone(timedelta(hours=8))
|
| 52 |
+
now_beijing = datetime.now(beijing_tz) # 带时区的datetime对象
|
| 53 |
+
future_time = now_beijing + timedelta(days=1)
|
| 54 |
+
future_timestamp = future_time.timestamp()
|
| 55 |
+
supabase.table("airs_api_keys").update({"ran_at": future_timestamp}).eq("id", api_key_id).execute()
|
| 56 |
+
print('更新成功')
|
| 57 |
+
except Exception as e:
|
| 58 |
+
raise HTTPException(status_code=500, detail="更新API密钥运行时间失败!")
|
| 59 |
continue
|
| 60 |
|
| 61 |
response.raise_for_status() # 触发其他4xx/5xx异常
|