tanbushi commited on
Commit
fe42adb
·
1 Parent(s): 8e183da
Files changed (4) hide show
  1. api_key_sb copy.py +30 -0
  2. api_key_sb.py +21 -2
  3. app.py +1 -1
  4. proxy.py +3 -3
api_key_sb copy.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from supabase import create_client, Client
2
+ import os
3
+ from fastapi import FastAPI, Request, HTTPException, Depends, status
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+ # Supabase 配置
9
+ SUPABASE_URL = os.getenv("SUPABASE_URL")
10
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
11
+
12
+ # 初始化 Supabase 客户端
13
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
14
+ async def get_api_key_info(model: str = None):
15
+ try:
16
+ # 根据用户提供的SQL语句修改,从 'airs_model_api_keys_view' 视图中获取 'api_key'
17
+ if model:
18
+ response = supabase.from_('airs_model_api_keys_view').select('api_key', 'api_key_id').eq('model_name', model).order('api_key_ran_at', desc=False).limit(1).execute()
19
+ else:
20
+ # 如果没有提供model,则获取所有模型的api_key
21
+ raise HTTPException(status_code=400, detail="请提供模型名称!")
22
+
23
+ if response.data:
24
+ api_key_info = response.data[0]
25
+ return api_key_info
26
+ else:
27
+ return None
28
+ except Exception as e:
29
+ print(f"从Supabase获取API密钥失败: {e}")
30
+ return None
api_key_sb.py CHANGED
@@ -2,6 +2,7 @@ from supabase import create_client, Client
2
  import os
3
  from fastapi import FastAPI, Request, HTTPException, Depends, status
4
  from dotenv import load_dotenv
 
5
 
6
  load_dotenv()
7
 
@@ -11,7 +12,7 @@ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
11
 
12
  # 初始化 Supabase 客户端
13
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
14
- async def get_api_key_info(model: str = None):
15
  try:
16
  # 根据用户提供的SQL语句修改,从 'airs_model_api_keys_view' 视图中获取 'api_key'
17
  if model:
@@ -22,7 +23,25 @@ async def get_api_key_info(model: str = None):
22
 
23
  if response.data:
24
  api_key_info = response.data[0]
25
- return api_key_info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  else:
27
  return None
28
  except Exception as e:
 
2
  import os
3
  from fastapi import FastAPI, Request, HTTPException, Depends, status
4
  from dotenv import load_dotenv
5
+ from datetime import timezone, timedelta, datetime
6
 
7
  load_dotenv()
8
 
 
12
 
13
  # 初始化 Supabase 客户端
14
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
15
+ async def get_api_key(model: str = None):
16
  try:
17
  # 根据用户提供的SQL语句修改,从 'airs_model_api_keys_view' 视图中获取 'api_key'
18
  if model:
 
23
 
24
  if response.data:
25
  api_key_info = response.data[0]
26
+ api_key = api_key_info.get('api_key')
27
+ api_key_id = api_key_info.get('api_key_id')
28
+
29
+
30
+ try:
31
+ # 创建北京时间时区对象(UTC+8)
32
+ beijing_tz = timezone(timedelta(hours=8))
33
+ now_beijing = datetime.now(beijing_tz) # 带时区的datetime对象
34
+ current_local_time = now_beijing.isoformat()
35
+ # current_local_time = int(now_beijing.timestamp()) # 正确的时间戳
36
+ # current_local_time = datetime.now().isoformat()
37
+ supabase.table("airs_api_keys").update({"ran_at": current_local_time}).eq("id", send_req.get('api_key_info').get('api_key_id')).execute()
38
+ print('更新成功')
39
+ except Exception as e:
40
+ raise HTTPException(status_code=500, detail="更新API密钥运行时间失败!")
41
+
42
+
43
+
44
+ return api_key
45
  else:
46
  return None
47
  except Exception as e:
app.py CHANGED
@@ -8,7 +8,7 @@ import os, json
8
  from dotenv import load_dotenv
9
  from enum import Enum
10
  from proxy import do_proxy
11
- from api_key_sb import get_api_key_info
12
 
13
  # 加载环境变量
14
  load_dotenv()
 
8
  from dotenv import load_dotenv
9
  from enum import Enum
10
  from proxy import do_proxy
11
+ # from api_key_sb import get_api_key
12
 
13
  # 加载环境变量
14
  load_dotenv()
proxy.py CHANGED
@@ -1,7 +1,7 @@
1
  import httpx
2
  import asyncio
3
  from starlette.responses import Response
4
- from api_key_sb import get_api_key_info
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 +11,8 @@ 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
- api_key_info = await get_api_key_info('gemini-2.5-flash')
15
- api_key = api_key_info.get('api_key')
16
  api_key_show = api_key[:5]+'*****'+api_key[-5:]
17
  print(f"使用API密钥:{api_key_show}")
18
  headers["Authorization"] = f"Bearer {api_key}"
 
1
  import httpx
2
  import asyncio
3
  from starlette.responses import Response
4
+ from api_key_sb import get_api_key
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
  for attempt in range(max_retries):
12
 
13
  # 获取API密钥信息
14
+ api_key = await get_api_key('gemini-2.5-flash')
15
+ # api_key = api_key_info.get('api_key')
16
  api_key_show = api_key[:5]+'*****'+api_key[-5:]
17
  print(f"使用API密钥:{api_key_show}")
18
  headers["Authorization"] = f"Bearer {api_key}"