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.exchange import ExchangesResponse, ExchangeResponse router = APIRouter() @router.get("", response_model=ExchangesResponse) async def get_exchanges( limit: Optional[int] = Query(10, le=2000), offset: Optional[int] = Query(0, ge=0), db: Session = Depends(get_db) ): """ Get a list of exchanges with optional pagination """ try: response = await coincap_api.get_exchanges(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 exchanges: {str(e)}" ) @router.get("/{exchange_id}", response_model=ExchangeResponse) async def get_exchange( exchange_id: str = Path(..., description="The exchange ID to retrieve"), db: Session = Depends(get_db) ): """ Get details for a specific exchange """ try: response = await coincap_api.get_exchange(exchange_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 exchange {exchange_id}: {str(e)}" )