diff --git a/app/api/v1/endpoints/stocks.py b/app/api/v1/endpoints/stocks.py index 5b6ef81..b372a80 100644 --- a/app/api/v1/endpoints/stocks.py +++ b/app/api/v1/endpoints/stocks.py @@ -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 current constituents of the specified stock index: - - Stock symbol - - Company name - - GICS Sector - - GICS Sub-Industry + Returns each stock's symbol, company name, GICS Sector, and GICS Sub-Industry. - **Supported indexes**: `sp500`, `nasdaq100` + **Supported values for `index_name`**: + - `sp500` — S&P 500 (~503 stocks) + - `nasdaq100` — Nasdaq 100 (~101 stocks) - **Data Source**: Wikipedia (List of S&P 500 companies / Nasdaq-100) - **Cache TTL**: 24 hours + **Data Source**: Wikipedia + **Cache TTL**: 24 hours (`X-Cache: HIT/MISS`, `ETag` headers included) + **Timeout**: 30 seconds (Wikipedia fetch) + + **Error codes**: + - `400` — unsupported `index_name` + - `504` — Wikipedia response timed out """ supported = list(index_constituents_service.INDEXES.keys()) if index_name not in supported: diff --git a/app/main.py b/app/main.py index 922f29b..a356070 100644 --- a/app/main.py +++ b/app/main.py @@ -148,6 +148,7 @@ async def root_documentation():
GET /stocks/trending - Trending stocks with intelligent parameter coordination (n=500 default)GET /stocks/most-active - Most actively traded stocksGET /stocks/52-week-gainers - Top 52-week gaining stocksGET /stocks/index/{{index_name}} - S&P 500 / Nasdaq 100 constituents from Wikipedia (24h cache) — sp500 | nasdaq100# S&P 500 constituents (~503 stocks, cached 24h)
+curl "http://localhost:18001/api/v1/stocks/index/sp500"
+
+# Nasdaq 100 constituents (~101 stocks, cached 24h)
+curl "http://localhost:18001/api/v1/stocks/index/nasdaq100"
+
+# Force refresh (bypass cache)
+curl "http://localhost:18001/api/v1/stocks/index/sp500?force_refresh=true"
+
# Get GDP series information
curl "http://localhost:18001/api/v1/fred/proxy/series?series_id=GDP"
@@ -450,6 +461,10 @@ app.openapi_tags = [
{
"name": "screener",
"description": "Stock screener — condition-based filtering by market cap, volume, price, P/E, sector, exchange"
+ },
+ {
+ "name": "stocks",
+ "description": "Stock market data — most active, 52-week gainers, trending, and index constituents (S&P 500 / Nasdaq 100)"
}
]