36 lines
No EOL
1.3 KiB
Python
36 lines
No EOL
1.3 KiB
Python
from fastapi import APIRouter, Query, HTTPException
|
|
from typing import Optional
|
|
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("/positions")
|
|
def get_positions(
|
|
userId: str = Query(..., description="ID của user để lấy API key/secret"),
|
|
symbol: Optional[str] = Query(None, description="Mã giao dịch, ví dụ: BTCUSDT"),
|
|
category: str = Query("linear", description="Loại lệnh: linear (future) hoặc spot")
|
|
):
|
|
api_key, api_secret = get_user_api(userId)
|
|
client = get_bybit_client(api_key, api_secret)
|
|
params = {
|
|
"category": category
|
|
}
|
|
if symbol:
|
|
params["symbol"] = symbol
|
|
res = client.get_positions(**params)
|
|
return res |