from fastapi import APIRouter from pydantic import BaseModel router = APIRouter() class ExchangeRateResponse(BaseModel): from_currency: str to_currency: str rate: float updated_at: str EXCHANGE_RATES = { ("USD", "CNY"): 7.24, ("EUR", "CNY"): 7.85, ("GBP", "CNY"): 9.15, ("CNY", "USD"): 0.138, ("USD", "EUR"): 0.92, ("EUR", "USD"): 1.09, ("GBP", "USD"): 1.27, ("USD", "GBP"): 0.79, } @router.get("/convert") async def convert_currency( from_currency: str = "USD", to_currency: str = "CNY", amount: float = 1.0, ): rate = EXCHANGE_RATES.get((from_currency, to_currency), 1.0) return { "from_currency": from_currency, "to_currency": to_currency, "amount": amount, "converted": round(amount * rate, 2), "rate": rate, "updated_at": "2026-05-08T00:00:00Z", } @router.get("/rates") async def get_rates(base: str = "USD"): rates = {} for (from_curr, to_curr), rate in EXCHANGE_RATES.items(): if from_curr == base: rates[to_curr] = rate return { "base": base, "rates": rates, "updated_at": "2026-05-08T00:00:00Z", }