from __future__ import annotations from typing import Optional import httpx GOLDPRICE_URL_TEMPLATE = "https://data-asg.goldprice.org/dbXRates/{currency}" DEFAULT_CURRENCY = "USD" HEADERS = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0 Safari/537.36", "Accept": "application/json", } async def fetch_goldprice_quote(currency: str = DEFAULT_CURRENCY) -> Optional[dict]: url = GOLDPRICE_URL_TEMPLATE.format(currency=currency.upper()) async with httpx.AsyncClient(timeout=10.0, headers=HEADERS) as client: response = await client.get(url) response.raise_for_status() data = response.json() items = data.get("items") or [] if not items: return None quote = items[0] xau_price = quote.get("xauPrice") if xau_price is None: return None return { "price": float(xau_price), "change": float(quote.get("chgXau") or 0.0), "change_percent": float(quote.get("pcXau") or 0.0), "previous_close": float(quote.get("xauClose") or 0.0), "timestamp_ms": int(data.get("ts") or 0), }