From ee2df73bcba031c187c3128558a1c5980a8af773 Mon Sep 17 00:00:00 2001 From: padreug Date: Tue, 11 Nov 2025 01:34:34 +0100 Subject: [PATCH] Use BQL query for get_all_accounts() instead of non-existent API endpoint --- fava_client.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/fava_client.py b/fava_client.py index 9dacf0a..9a2fc28 100644 --- a/fava_client.py +++ b/fava_client.py @@ -818,7 +818,7 @@ class FavaClient: async def get_all_accounts(self) -> List[Dict[str, Any]]: """ - Get all accounts from Beancount/Fava. + Get all accounts from Beancount/Fava using BQL query. Returns: List of account dictionaries: @@ -834,29 +834,25 @@ class FavaClient: print(acc["account"]) # "Assets:Cash" """ try: - # Use Fava's /api/accounts endpoint - async with httpx.AsyncClient(timeout=self.timeout) as client: - response = await client.get(f"{self.base_url}/accounts") - response.raise_for_status() - accounts_data = response.json() + # Use BQL to get all unique accounts + query = "SELECT DISTINCT account" + result = await self.query_bql(query) - # Fava returns array of account names - # We need to convert to dict format expected by account_sync - accounts = [] - for acc_name in accounts_data: + # Convert BQL result to expected format + accounts = [] + for row in result["rows"]: + account_name = row[0] if isinstance(row, list) else row.get("account") + if account_name: accounts.append({ - "account": acc_name, - "meta": {} # Fava /api/accounts doesn't include metadata + "account": account_name, + "meta": {} # BQL doesn't return metadata easily }) - logger.debug(f"Fava returned {len(accounts)} accounts") - return accounts + logger.debug(f"Fava returned {len(accounts)} accounts via BQL") + return accounts - except httpx.HTTPStatusError as e: - logger.error(f"Fava accounts error: {e.response.status_code} - {e.response.text}") - raise - except httpx.RequestError as e: - logger.error(f"Fava connection error: {e}") + except Exception as e: + logger.error(f"Failed to fetch accounts via BQL: {e}") raise async def get_journal_entries(self) -> List[Dict[str, Any]]: