|
|
|
|
@ -18,7 +18,11 @@ router = APIRouter()
|
|
|
|
|
logger = logging.getLogger("app.api.v1.stocks")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/index/{index_name}")
|
|
|
|
|
@router.get(
|
|
|
|
|
"/index/{index_name}",
|
|
|
|
|
summary="Get index constituents (S&P 500 / Nasdaq 100)",
|
|
|
|
|
response_description="List of constituent stocks with symbol, name, sector, and industry",
|
|
|
|
|
)
|
|
|
|
|
@with_cache(namespace="stocks:index", ttl=86400, key_params=["index_name"])
|
|
|
|
|
async def get_index_constituents(
|
|
|
|
|
index_name: str,
|
|
|
|
|
@ -26,18 +30,21 @@ async def get_index_constituents(
|
|
|
|
|
force_refresh: bool = Query(False, description="If true, bypasses cache and fetches fresh data"),
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Get index constituents from Wikipedia
|
|
|
|
|
Get current constituents of a major stock index from Wikipedia.
|
|
|
|
|
|
|
|
|
|
Returns each stock's symbol, company name, GICS Sector, and GICS Sub-Industry.
|
|
|
|
|
|
|
|
|
|
Returns current constituents of the specified stock index:
|
|
|
|
|
- Stock symbol
|
|
|
|
|
- Company name
|
|
|
|
|
- GICS Sector
|
|
|
|
|
- GICS Sub-Industry
|
|
|
|
|
**Supported values for `index_name`**:
|
|
|
|
|
- `sp500` — S&P 500 (~503 stocks)
|
|
|
|
|
- `nasdaq100` — Nasdaq 100 (~101 stocks)
|
|
|
|
|
|
|
|
|
|
**Supported indexes**: `sp500`, `nasdaq100`
|
|
|
|
|
**Data Source**: Wikipedia
|
|
|
|
|
**Cache TTL**: 24 hours (`X-Cache: HIT/MISS`, `ETag` headers included)
|
|
|
|
|
**Timeout**: 30 seconds (Wikipedia fetch)
|
|
|
|
|
|
|
|
|
|
**Data Source**: Wikipedia (List of S&P 500 companies / Nasdaq-100)
|
|
|
|
|
**Cache TTL**: 24 hours
|
|
|
|
|
**Error codes**:
|
|
|
|
|
- `400` — unsupported `index_name`
|
|
|
|
|
- `504` — Wikipedia response timed out
|
|
|
|
|
"""
|
|
|
|
|
supported = list(index_constituents_service.INDEXES.keys())
|
|
|
|
|
if index_name not in supported:
|
|
|
|
|
|