"""Economy data: FRED search, regional data, Fed holdings, FOMC documents.""" import asyncio import logging from typing import Any from openbb import obb from obb_utils import to_list logger = logging.getLogger(__name__) async def get_cpi(country: str = "united_states") -> list[dict[str, Any]]: """Get Consumer Price Index data.""" try: result = await asyncio.to_thread( obb.economy.cpi, country=country, provider="fred" ) return to_list(result) except Exception: logger.warning("CPI failed for %s", country, exc_info=True) return [] _VALID_GDP_TYPES = {"nominal", "real", "forecast"} async def get_gdp(gdp_type: str = "real") -> list[dict[str, Any]]: """Get GDP data (nominal, real, or forecast).""" if gdp_type not in _VALID_GDP_TYPES: return [] try: fn = getattr(obb.economy.gdp, gdp_type, None) if fn is None: return [] result = await asyncio.to_thread(fn, provider="oecd") return to_list(result) except Exception: logger.warning("GDP %s failed", gdp_type, exc_info=True) return [] async def get_unemployment(country: str = "united_states") -> list[dict[str, Any]]: """Get unemployment rate data.""" try: result = await asyncio.to_thread( obb.economy.unemployment, country=country, provider="oecd" ) return to_list(result) except Exception: logger.warning("Unemployment failed for %s", country, exc_info=True) return [] async def get_composite_leading_indicator( country: str = "united_states", ) -> list[dict[str, Any]]: """Get Composite Leading Indicator (recession predictor).""" try: result = await asyncio.to_thread( obb.economy.composite_leading_indicator, country=country, provider="oecd" ) return to_list(result) except Exception: logger.warning("CLI failed for %s", country, exc_info=True) return [] async def get_house_price_index( country: str = "united_states", ) -> list[dict[str, Any]]: """Get housing price index.""" try: result = await asyncio.to_thread( obb.economy.house_price_index, country=country, provider="oecd" ) return to_list(result) except Exception: logger.warning("HPI failed for %s", country, exc_info=True) return [] async def get_pce() -> list[dict[str, Any]]: """Get Personal Consumption Expenditures (Fed preferred inflation).""" try: result = await asyncio.to_thread( obb.economy.pce, provider="fred" ) return to_list(result) except Exception: logger.warning("PCE failed", exc_info=True) return [] async def get_money_measures() -> list[dict[str, Any]]: """Get M1/M2 money supply data.""" try: result = await asyncio.to_thread( obb.economy.money_measures, provider="federal_reserve" ) return to_list(result) except Exception: logger.warning("Money measures failed", exc_info=True) return [] async def fred_search(query: str) -> list[dict[str, Any]]: """Search FRED series by keyword.""" try: result = await asyncio.to_thread( obb.economy.fred_search, query, provider="fred" ) return to_list(result) except Exception: logger.warning("FRED search failed for %s", query, exc_info=True) return [] async def get_balance_of_payments() -> list[dict[str, Any]]: """Get balance of payments (current/capital/financial account).""" try: result = await asyncio.to_thread( obb.economy.balance_of_payments, provider="fred" ) return to_list(result) except Exception: logger.warning("Balance of payments failed", exc_info=True) return [] async def get_central_bank_holdings() -> list[dict[str, Any]]: """Get Fed SOMA portfolio holdings.""" try: result = await asyncio.to_thread( obb.economy.central_bank_holdings, provider="federal_reserve" ) return to_list(result) except Exception: logger.warning("Central bank holdings failed", exc_info=True) return [] async def get_fred_regional( series_id: str, region: str | None = None, ) -> list[dict[str, Any]]: """Get geographically disaggregated FRED data (by state, county, MSA).""" try: kwargs: dict[str, Any] = {"symbol": series_id, "provider": "fred"} if region: kwargs["region_type"] = region result = await asyncio.to_thread( obb.economy.fred_regional, **kwargs ) return to_list(result) except Exception: logger.warning("FRED regional failed for %s", series_id, exc_info=True) return [] async def get_primary_dealer_positioning() -> list[dict[str, Any]]: """Get primary dealer net positions in treasuries, MBS, corporate bonds.""" try: result = await asyncio.to_thread( obb.economy.primary_dealer_positioning, provider="federal_reserve" ) return to_list(result) except Exception: logger.warning("Primary dealer positioning failed", exc_info=True) return [] async def get_fomc_documents(year: int | None = None) -> list[dict[str, Any]]: """Get FOMC meeting documents (minutes, projections, etc.).""" try: kwargs: dict[str, Any] = {"provider": "federal_reserve"} if year is not None: kwargs["year"] = year result = await asyncio.to_thread( obb.economy.fomc_documents, **kwargs ) return to_list(result) except Exception: logger.warning("FOMC documents failed", exc_info=True) return []