import logging import httpx from ..config import settings logger = logging.getLogger("firefly_client") class FireflyClient: def __init__(self): self.base_url = settings.firefly_url.rstrip("/") self.headers = {"Authorization": f"Bearer {settings.firefly_api_token}"} async def _get(self, path, params=None): url = f"{self.base_url}{path}" logger.info("Firefly GET %s params=%s", url, params) try: async with httpx.AsyncClient(timeout=30.0) as client: response = await client.get(url, headers=self.headers, params=params) logger.info("Firefly response %s %s", response.status_code, url) response.raise_for_status() data = response.json().get("data", []) logger.info("Firefly returned %d items for %s", len(data) if hasattr(data, '__len__') else 0, path) return data except Exception as e: logger.exception("Error fetching from Firefly %s: %s", url, e) # re-raise so callers can handle/fail loudly raise async def get_transactions(self, start_date=None, end_date=None): params = {} if start_date: params["start_date"] = start_date if end_date: params["end_date"] = end_date return await self._get("/api/v1/transactions", params=params) async def get_categories(self): return await self._get("/api/v1/categories") async def get_accounts(self): return await self._get("/api/v1/accounts") firefly_client = FireflyClient()