init
This commit is contained in:
parent
be5fb603f3
commit
859fb7a19d
11 changed files with 436 additions and 0 deletions
120
app/api/account.py
Normal file
120
app/api/account.py
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
from fastapi import APIRouter, Query, HTTPException, Body
|
||||
from app.services.bybit_service import get_bybit_client
|
||||
import json
|
||||
import os
|
||||
from typing import List, Optional
|
||||
|
||||
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")
|
||||
|
||||
# API lấy danh sách user
|
||||
@router.get("/users")
|
||||
def list_users():
|
||||
try:
|
||||
with open(ACCOUNTS_FILE, 'r', encoding='utf-8') as f:
|
||||
accounts = json.load(f)
|
||||
return accounts
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Lỗi đọc file accounts: {e}")
|
||||
|
||||
# API thêm user
|
||||
@router.post("/users")
|
||||
def add_user(
|
||||
id: str = Body(..., description="ID user"),
|
||||
bybit_api_key: str = Body(..., description="Bybit API Key"),
|
||||
bybit_api_secret: str = Body(..., description="Bybit API Secret"),
|
||||
user_name: str = Body(..., description="Tên user")
|
||||
):
|
||||
try:
|
||||
with open(ACCOUNTS_FILE, 'r+', encoding='utf-8') as f:
|
||||
accounts = json.load(f)
|
||||
if any(acc['id'] == id for acc in accounts):
|
||||
raise HTTPException(status_code=400, detail="ID đã tồn tại")
|
||||
new_user = {
|
||||
"id": id,
|
||||
"bybit_api_key": bybit_api_key,
|
||||
"bybit_api_secret": bybit_api_secret,
|
||||
"user_name": user_name
|
||||
}
|
||||
accounts.append(new_user)
|
||||
f.seek(0)
|
||||
json.dump(accounts, f, ensure_ascii=False, indent=2)
|
||||
f.truncate()
|
||||
return {"message": "Thêm user thành công"}
|
||||
except HTTPException as e:
|
||||
raise e
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Lỗi ghi file accounts: {e}")
|
||||
|
||||
# API sửa user
|
||||
@router.put("/users/{user_id}")
|
||||
def update_user(
|
||||
user_id: str,
|
||||
bybit_api_key: Optional[str] = Body(None),
|
||||
bybit_api_secret: Optional[str] = Body(None),
|
||||
user_name: Optional[str] = Body(None)
|
||||
):
|
||||
try:
|
||||
with open(ACCOUNTS_FILE, 'r+', encoding='utf-8') as f:
|
||||
accounts = json.load(f)
|
||||
for acc in accounts:
|
||||
if acc['id'] == user_id:
|
||||
if bybit_api_key is not None:
|
||||
acc['bybit_api_key'] = bybit_api_key
|
||||
if bybit_api_secret is not None:
|
||||
acc['bybit_api_secret'] = bybit_api_secret
|
||||
if user_name is not None:
|
||||
acc['user_name'] = user_name
|
||||
f.seek(0)
|
||||
json.dump(accounts, f, ensure_ascii=False, indent=2)
|
||||
f.truncate()
|
||||
return {"message": "Cập nhật user thành công"}
|
||||
raise HTTPException(status_code=404, detail="Không tìm thấy userId")
|
||||
except HTTPException as e:
|
||||
raise e
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Lỗi ghi file accounts: {e}")
|
||||
|
||||
# API xóa user
|
||||
@router.delete("/users/{user_id}")
|
||||
def delete_user(user_id: str):
|
||||
try:
|
||||
with open(ACCOUNTS_FILE, 'r+', encoding='utf-8') as f:
|
||||
accounts = json.load(f)
|
||||
new_accounts = [acc for acc in accounts if acc['id'] != user_id]
|
||||
if len(new_accounts) == len(accounts):
|
||||
raise HTTPException(status_code=404, detail="Không tìm thấy userId")
|
||||
f.seek(0)
|
||||
json.dump(new_accounts, f, ensure_ascii=False, indent=2)
|
||||
f.truncate()
|
||||
return {"message": "Xóa user thành công"}
|
||||
except HTTPException as e:
|
||||
raise e
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Lỗi ghi file accounts: {e}")
|
||||
|
||||
@router.get("/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}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue