feat(REQ-005): implement TrEx Basic V1 API contract with auth, assets, ledger, and user endpoints
- Add V1 API routes for assets, ledger, and user info with complete CRUD operations - Implement authentication scopes and request validation via auth_routes.py - Extend schemas with V1-specific models for accounts, holdings, and positions - Add database queries and aggregations for P&L, performance, and account data - Include comprehensive test coverage for all V1 endpoints and integration flows - Update batch and release documentation with implementation and verification evidence - Sync project structure and runtime flows per V1 API contract requirements Closes implementation phase of BAT-001 (TrEx Basic V1 API Contract)
This commit is contained in:
+58
-24
@@ -2,13 +2,17 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
import json
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
|
||||
from app.core.auth import AuthContext, require_read
|
||||
from app.repositories.factory import get_portfolio_repository_from_settings
|
||||
from app.repositories.portfolio_repository import PortfolioRepository
|
||||
from app.schemas.v1 import AssetItem, AssetsResponse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/v1", tags=["v1"])
|
||||
|
||||
|
||||
@@ -21,8 +25,21 @@ def _require_service_token(auth: AuthContext = Depends(require_read)) -> AuthCon
|
||||
return auth
|
||||
|
||||
|
||||
def _parse_strategy_params(strategy_set_config: str | None) -> dict:
|
||||
if not strategy_set_config:
|
||||
return {}
|
||||
try:
|
||||
parsed = json.loads(strategy_set_config)
|
||||
return parsed if isinstance(parsed, dict) else {}
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return {}
|
||||
|
||||
|
||||
@router.get("/assets", response_model=AssetsResponse)
|
||||
def list_assets(auth: AuthContext = Depends(_require_service_token)) -> AssetsResponse:
|
||||
def list_assets(
|
||||
status_filter: str | None = Query(default=None, alias="status"),
|
||||
auth: AuthContext = Depends(_require_service_token),
|
||||
) -> AssetsResponse:
|
||||
from app.core.config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
@@ -32,30 +49,47 @@ def list_assets(auth: AuthContext = Depends(_require_service_token)) -> AssetsRe
|
||||
owner_user_id=None,
|
||||
include_disabled=False,
|
||||
)
|
||||
|
||||
# Fetch strategy_params per portfolio
|
||||
items = []
|
||||
for p in portfolios:
|
||||
# Optional status filter
|
||||
if status_filter is not None and p.status != status_filter:
|
||||
continue
|
||||
|
||||
strategy_params: dict = {}
|
||||
if p.active_strategy_id:
|
||||
try:
|
||||
ss = repo.get_strategy_set(p.active_strategy_id)
|
||||
if ss is not None:
|
||||
strategy_params = _parse_strategy_params(ss.config)
|
||||
except Exception:
|
||||
logger.warning("strategy_set lookup failed for %s", p.active_strategy_id)
|
||||
strategy_params = {}
|
||||
|
||||
items.append(
|
||||
AssetItem(
|
||||
asset_id=p.portfolio_id,
|
||||
symbol=p.symbol,
|
||||
market=p.market,
|
||||
status=p.status,
|
||||
cash=p.cash_balance,
|
||||
quantity=p.asset_quantity,
|
||||
broker_type=p.broker,
|
||||
strategy_id=p.active_strategy_id,
|
||||
strategy_params=strategy_params,
|
||||
updated_at=p.updated_at,
|
||||
name=p.name,
|
||||
price=p.price,
|
||||
profit_buffer=p.profit_buffer,
|
||||
tags=p.tags,
|
||||
owner_user_id=p.owner_user_id,
|
||||
created_at=p.created_at,
|
||||
last_trade_at=p.last_trade_at,
|
||||
)
|
||||
)
|
||||
finally:
|
||||
if callable(getattr(type(repo), "close", None)):
|
||||
repo.close()
|
||||
|
||||
items = [
|
||||
AssetItem(
|
||||
asset_id=p.portfolio_id,
|
||||
broker=p.broker,
|
||||
market=p.market,
|
||||
symbol=p.symbol,
|
||||
name=p.name,
|
||||
status=p.status,
|
||||
cash_balance=p.cash_balance,
|
||||
asset_quantity=p.asset_quantity,
|
||||
price=p.price,
|
||||
profit_buffer=p.profit_buffer,
|
||||
active_strategy_id=p.active_strategy_id,
|
||||
available_strategies=p.avabile_strategies,
|
||||
tags=p.tags,
|
||||
owner_user_id=p.owner_user_id,
|
||||
created_at=p.created_at,
|
||||
updated_at=p.updated_at,
|
||||
last_trade_at=p.last_trade_at,
|
||||
)
|
||||
for p in portfolios
|
||||
]
|
||||
return AssetsResponse(assets=items, total=len(items))
|
||||
|
||||
Reference in New Issue
Block a user