
* 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)
49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query, Path, 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.rate import RatesResponse, RateResponse
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=RatesResponse)
|
|
async def get_rates(
|
|
ids: Optional[str] = Query(None, description="Comma-separated list of slugs to filter by (e.g. `bitcoin,ethereum`)"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Get a list of conversion rates with optional filtering
|
|
"""
|
|
try:
|
|
response = await coincap_api.get_rates(ids)
|
|
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 rates: {str(e)}"
|
|
)
|
|
|
|
|
|
@router.get("/{rate_id}", response_model=RateResponse)
|
|
async def get_rate(
|
|
rate_id: str = Path(..., description="The rate ID (slug) to retrieve"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Get a specific conversion rate by ID
|
|
"""
|
|
try:
|
|
response = await coincap_api.get_rate(rate_id)
|
|
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 rate {rate_id}: {str(e)}"
|
|
) |