53 lines
1.7 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from typing import List, Optional
import logging
from app.database import get_db
from app.schemas.exchanges import (
ExchangesResponse, SingleExchangeResponse, ErrorResponse
)
from app.services.coincap_client import CoinCapClient
router = APIRouter(prefix="/exchanges", tags=["Exchanges"])
logger = logging.getLogger(__name__)
# Initialize client
client = CoinCapClient()
@router.get(
"",
response_model=ExchangesResponse,
summary="Get list of exchanges",
description="Retrieve a list of exchanges with optional pagination"
)
async def get_exchanges(
limit: Optional[int] = Query(10, ge=1, le=2000),
offset: Optional[int] = Query(0, ge=0),
db: Session = Depends(get_db)
):
try:
response = await client.get_exchanges(limit=limit, offset=offset)
return response
except Exception as e:
logger.error(f"Error fetching exchanges: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get(
"/{exchange_id}",
response_model=SingleExchangeResponse,
responses={404: {"model": ErrorResponse}},
summary="Get a specific exchange",
description="Retrieve details for a specific exchange"
)
async def get_exchange(exchange_id: str, db: Session = Depends(get_db)):
try:
response = await client.get_exchange(exchange_id)
if not response.get("data"):
raise HTTPException(status_code=404, detail=f"Exchange {exchange_id} not found")
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error fetching exchange {exchange_id}: {e}")
raise HTTPException(status_code=500, detail=str(e))