- Add V1 API endpoints: assets, ledger, user-info - Implement Fernet-based credential payload encryption (req-010) - Extend health response schema with runtime info (req-004) - Add service token runtime ID issuance (req-005) - Implement database schema migration for V1 API (req-009) - Add comprehensive test coverage for V1 endpoints - Update SRS with 10 new requirements (REQ-004 through REQ-011) - Create batch planning, design, and verification for BAT-001 - Integrate review-discovery StagePilot skill - Update documentation indices and baseline specs
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
"""GET /v1/assets — service-token authenticated asset list endpoint."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, 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
|
|
|
|
router = APIRouter(prefix="/v1", tags=["v1"])
|
|
|
|
|
|
def _require_service_token(auth: AuthContext = Depends(require_read)) -> AuthContext:
|
|
if auth.token_source != "db" or auth.token_type != "service":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Service token required",
|
|
)
|
|
return auth
|
|
|
|
|
|
@router.get("/assets", response_model=AssetsResponse)
|
|
def list_assets(auth: AuthContext = Depends(_require_service_token)) -> AssetsResponse:
|
|
from app.core.config import get_settings
|
|
|
|
settings = get_settings()
|
|
repo = get_portfolio_repository_from_settings(settings)
|
|
try:
|
|
portfolios = repo.list_portfolios_with_strategies(
|
|
owner_user_id=None,
|
|
include_disabled=False,
|
|
)
|
|
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))
|