34 lines
1.4 KiB
Python
34 lines
1.4 KiB
Python
from fastapi import APIRouter, Query, HTTPException
|
|
from app.schemas.account import AccountResponseSchema
|
|
from app.services.bybit_service import get_bybit_client
|
|
import json
|
|
import os
|
|
|
|
router = APIRouter()
|
|
|
|
ACCOUNTS_FILE = os.path.join(os.path.dirname(__file__), '../../accounts.json')
|
|
|
|
def get_user_api(user_id: str):
|
|
try:
|
|
with open(ACCOUNTS_FILE, 'r', encoding='utf-8') as f:
|
|
accounts = json.load(f)
|
|
for acc in accounts:
|
|
if acc['id'] == user_id:
|
|
return acc['bybit_api_key'], acc['bybit_api_secret']
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Lỗi đọc file accounts: {e}")
|
|
raise HTTPException(status_code=404, detail="Không tìm thấy userId")
|
|
|
|
@router.get("/account", response_model=AccountResponseSchema, tags=["Account"])
|
|
def get_account_info(userId: str = Query(..., description="ID của user để lấy API key/secret")):
|
|
try:
|
|
api_key, api_secret = get_user_api(userId)
|
|
client = get_bybit_client(api_key, api_secret)
|
|
res = client.get_wallet_balance(accountType="UNIFIED")
|
|
if 'retCode' in res and res['retCode'] != 0:
|
|
raise HTTPException(status_code=400, detail=f"Bybit API error: {res.get('retMsg', 'Unknown error')}")
|
|
return res
|
|
except HTTPException as e:
|
|
raise e
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Lỗi hệ thống: {e}")
|