Automated Action 3a29a346a9 Initial implementation of cryptocurrency data API service using CoinCap API
* 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)
2025-05-14 12:18:27 +00:00

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)}"
)