Use BQL query for get_all_accounts() instead of non-existent API endpoint

This commit is contained in:
padreug 2025-11-11 01:34:34 +01:00
parent c70695f330
commit ee2df73bcb

View file

@ -818,7 +818,7 @@ class FavaClient:
async def get_all_accounts(self) -> List[Dict[str, Any]]: 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: Returns:
List of account dictionaries: List of account dictionaries:
@ -834,29 +834,25 @@ class FavaClient:
print(acc["account"]) # "Assets:Cash" print(acc["account"]) # "Assets:Cash"
""" """
try: try:
# Use Fava's /api/accounts endpoint # Use BQL to get all unique accounts
async with httpx.AsyncClient(timeout=self.timeout) as client: query = "SELECT DISTINCT account"
response = await client.get(f"{self.base_url}/accounts") result = await self.query_bql(query)
response.raise_for_status()
accounts_data = response.json()
# Fava returns array of account names # Convert BQL result to expected format
# We need to convert to dict format expected by account_sync
accounts = [] accounts = []
for acc_name in accounts_data: for row in result["rows"]:
account_name = row[0] if isinstance(row, list) else row.get("account")
if account_name:
accounts.append({ accounts.append({
"account": acc_name, "account": account_name,
"meta": {} # Fava /api/accounts doesn't include metadata "meta": {} # BQL doesn't return metadata easily
}) })
logger.debug(f"Fava returned {len(accounts)} accounts") logger.debug(f"Fava returned {len(accounts)} accounts via BQL")
return accounts return accounts
except httpx.HTTPStatusError as e: except Exception as e:
logger.error(f"Fava accounts error: {e.response.status_code} - {e.response.text}") logger.error(f"Failed to fetch accounts via BQL: {e}")
raise
except httpx.RequestError as e:
logger.error(f"Fava connection error: {e}")
raise raise
async def get_journal_entries(self) -> List[Dict[str, Any]]: async def get_journal_entries(self) -> List[Dict[str, Any]]: