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]]:
"""
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]]: