refactor: fix code review issues across routes and services
- Extract shared route_utils.py (validate_symbol, safe decorator)
removing duplication from 6 route files
- Extract shared obb_utils.py (to_list, extract_single, safe_last)
removing duplication from calendar_service and market_service
- Fix _to_list dict mutation during iteration (use comprehension)
- Fix double vars() call and live __dict__ mutation risk
- Fix route ordering: /etf/search and /crypto/search now registered
before /{symbol} path params to prevent shadowing
- Add date format validation (YYYY-MM-DD pattern) on calendar routes
- Use timezone-aware datetime.now(tz=timezone.utc) in all services
- Add explicit type annotation for asyncio.gather results
This commit is contained in:
@@ -1,41 +1,16 @@
|
||||
"""Routes for macroeconomic data (FRED-powered)."""
|
||||
|
||||
import functools
|
||||
import logging
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import ParamSpec, TypeVar
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from fastapi import APIRouter, Query
|
||||
|
||||
from models import ApiResponse
|
||||
from route_utils import safe
|
||||
import macro_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/v1")
|
||||
|
||||
P = ParamSpec("P")
|
||||
R = TypeVar("R")
|
||||
|
||||
|
||||
def _safe(fn: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]:
|
||||
@functools.wraps(fn)
|
||||
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||
try:
|
||||
return await fn(*args, **kwargs)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception:
|
||||
logger.exception("Upstream data error")
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail="Data provider error. Check server logs.",
|
||||
)
|
||||
return wrapper # type: ignore[return-value]
|
||||
|
||||
|
||||
@router.get("/macro/overview", response_model=ApiResponse)
|
||||
@_safe
|
||||
@safe
|
||||
async def macro_overview():
|
||||
"""Get key macro indicators: Fed rate, treasury yields, CPI, unemployment, GDP, VIX."""
|
||||
data = await macro_service.get_macro_overview()
|
||||
@@ -43,7 +18,7 @@ async def macro_overview():
|
||||
|
||||
|
||||
@router.get("/macro/series/{series_id}", response_model=ApiResponse)
|
||||
@_safe
|
||||
@safe
|
||||
async def macro_series(
|
||||
series_id: str,
|
||||
limit: int = Query(default=30, ge=1, le=1000),
|
||||
|
||||
Reference in New Issue
Block a user