update
This commit is contained in:
parent
859fb7a19d
commit
990f944ec6
8 changed files with 174 additions and 94 deletions
|
|
@ -1,8 +1,8 @@
|
|||
from fastapi import APIRouter, Query, HTTPException, Body
|
||||
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
|
||||
from typing import List, Optional
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
|
@ -19,93 +19,7 @@ def get_user_api(user_id: str):
|
|||
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")
|
||||
@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)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from fastapi import APIRouter, Query, HTTPException
|
||||
from typing import Optional
|
||||
from app.services.bybit_service import get_bybit_client
|
||||
from app.schemas.candle import CandleResponseSchema
|
||||
import json
|
||||
import os
|
||||
import pandas as pd
|
||||
|
|
@ -42,7 +43,7 @@ def rsi(series, period=14):
|
|||
rsi = rsi.replace([np.inf, -np.inf], np.nan)
|
||||
return rsi
|
||||
|
||||
@router.get("/candles")
|
||||
@router.get("/candles", response_model=CandleResponseSchema, response_model_exclude_none=True, tags=["Candle"])
|
||||
def get_candles(
|
||||
userId: str = Query(..., description="ID của user để lấy API key/secret"),
|
||||
symbol: str = Query(..., description="Mã giao dịch, ví dụ: BTCUSDT"),
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ def get_user_api(user_id: str):
|
|||
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("/orders")
|
||||
@router.get("/orders", tags=["Order"])
|
||||
def get_orders(
|
||||
userId: str = Query(..., description="ID của user để lấy API key/secret"),
|
||||
symbol: str = Query(..., description="Mã giao dịch, ví dụ: BTCUSDT"),
|
||||
|
|
@ -34,7 +34,7 @@ def get_orders(
|
|||
return res
|
||||
|
||||
# Submit order
|
||||
@router.post("/orders")
|
||||
@router.post("/orders", tags=["Order"])
|
||||
def submit_order(
|
||||
userId: str = Body(..., embed=True, description="ID của user để lấy API key/secret"),
|
||||
symbol: str = Body(..., embed=True, description="Mã giao dịch, ví dụ: BTCUSDT"),
|
||||
|
|
@ -61,7 +61,7 @@ def submit_order(
|
|||
return res
|
||||
|
||||
# Cancel order
|
||||
@router.delete("/orders/{order_id}")
|
||||
@router.delete("/orders/{order_id}", tags=["Order"])
|
||||
def cancel_order(
|
||||
order_id: str,
|
||||
userId: str = Query(..., description="ID của user để lấy API key/secret"),
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ def get_user_api(user_id: str):
|
|||
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")
|
||||
@router.get("/positions", tags=["Position"])
|
||||
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"),
|
||||
|
|
|
|||
94
app/api/user/user.py
Normal file
94
app/api/user/user.py
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
from fastapi import APIRouter, Query, HTTPException, Body
|
||||
import json
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
ACCOUNTS_FILE = os.path.join(os.path.dirname(__file__), '../../../accounts.json')
|
||||
|
||||
# API lấy danh sách user
|
||||
@router.get("/users", tags=["User"])
|
||||
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", tags=["User"])
|
||||
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}", tags=["User"])
|
||||
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}", tags=["User"])
|
||||
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}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue