
* Created FastAPI application structure * Added database models for assets, exchanges, markets, and rates * Integrated with CoinCap API * Implemented REST API endpoints * Setup SQLite persistence with Alembic migrations * Added comprehensive documentation Generated with BackendIM... (backend.im)
47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import Optional
|
|
|
|
from app.services.coincap_api import coincap_api
|
|
from app.core.database import get_db
|
|
from app.api.schemas.market import MarketsResponse
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=MarketsResponse)
|
|
async def get_markets(
|
|
exchange_id: Optional[str] = Query(None, description="Filter by exchange ID"),
|
|
base_symbol: Optional[str] = Query(None, description="Filter by base asset symbol"),
|
|
base_id: Optional[str] = Query(None, description="Filter by base asset ID"),
|
|
quote_symbol: Optional[str] = Query(None, description="Filter by quote asset symbol"),
|
|
quote_id: Optional[str] = Query(None, description="Filter by quote asset ID"),
|
|
asset_symbol: Optional[str] = Query(None, description="Filter by asset symbol (matches base or quote)"),
|
|
asset_id: Optional[str] = Query(None, description="Filter by asset ID (matches base or quote)"),
|
|
limit: Optional[int] = Query(10, le=2000),
|
|
offset: Optional[int] = Query(0, ge=0),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Get a list of markets with optional filters and pagination
|
|
"""
|
|
try:
|
|
response = await coincap_api.get_markets(
|
|
exchange_id,
|
|
base_symbol,
|
|
base_id,
|
|
quote_symbol,
|
|
quote_id,
|
|
asset_symbol,
|
|
asset_id,
|
|
limit,
|
|
offset
|
|
)
|
|
return response
|
|
except HTTPException as e:
|
|
raise e
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to fetch markets: {str(e)}"
|
|
) |