MCPcopy Create free account
hub / github.com/ZyphrZero/z.ai2api_python / toggle_token

Function toggle_token

app/admin/api.py:870–909  ·  view source on GitHub ↗

切换 Token 启用状态

(token_id: int, enabled: bool)

Source from the content-addressed store, hash-verified

868
869@router.post("/tokens/toggle/{token_id}")
870async def toggle_token(token_id: int, enabled: bool):
871 """切换 Token 启用状态"""
872 from app.services.token_dao import get_token_dao
873 from app.utils.token_pool import get_token_pool
874
875 dao = get_token_dao()
876 await dao.update_token_status(token_id, enabled)
877
878 # 同步 Token 池状态
879 pool = get_token_pool()
880 if pool:
881 # 获取 Token 的提供商信息
882 async with dao.get_connection() as conn:
883 cursor = await conn.execute("SELECT provider FROM tokens WHERE id = ?", (token_id,))
884 row = await cursor.fetchone()
885 if row:
886 provider = row[0]
887 await pool.sync_from_database(provider)
888 logger.info("✅ Token 池已同步")
889
890 # 根据状态返回不同样式的按钮
891 if enabled:
892 button_class = "bg-green-100 text-green-800 hover:bg-green-200"
893 indicator_class = "bg-green-500"
894 label = "已启用"
895 next_state = "false"
896 else:
897 button_class = "bg-red-100 text-red-800 hover:bg-red-200"
898 indicator_class = "bg-red-500"
899 label = "已禁用"
900 next_state = "true"
901
902 return HTMLResponse(f"""
903 <button hx-post="/admin/api/tokens/toggle/{token_id}?enabled={next_state}"
904 hx-swap="outerHTML"
905 class="inline-flex items-center px-2.5 py-0.5 text-xs font-semibold rounded-full transition-colors {button_class}">
906 <span class="h-2 w-2 rounded-full mr-1.5 {indicator_class}"></span>
907 {label}
908 </button>
909 """)
910
911
912@router.delete("/tokens/delete/{token_id}")

Callers

nothing calls this directly

Calls 5

get_token_daoFunction · 0.90
get_token_poolFunction · 0.90
update_token_statusMethod · 0.80
sync_from_databaseMethod · 0.80
get_connectionMethod · 0.45

Tested by

no test coverage detected