|
2 | 2 | from fastapi_cache import FastAPICache |
3 | 3 | from fastapi_cache.backends.redis import RedisBackend |
4 | 4 | from utils.redisCache import get_cache |
5 | | -from controllers.topStocks import get_top_stocks, get_stock |
| 5 | +from controllers.topStocks import get_stock, get_top_stock_info |
6 | 6 | from controllers.stockNews import fetch_news |
7 | 7 | from controllers.stockAgent import stock_analyzer_agent, extract_json_from_response, create_default_stock_data, merge_stock_data |
8 | 8 | from fastapi.responses import JSONResponse, HTMLResponse |
@@ -31,9 +31,7 @@ async def read_top_stocks(request: Request, cache: RedisBackend = Depends(get_ca |
31 | 31 | if cached_result: |
32 | 32 | result = json.loads(cached_result) |
33 | 33 | else: |
34 | | - top_stocks = ['AAPL', 'MSFT', 'AMZN', 'GOOGL', 'TSLA', 'META', 'NVDA'] |
35 | | - stocks = " ".join(top_stocks) |
36 | | - result = get_top_stocks(stocks) |
| 34 | + result = get_top_stock_info() |
37 | 35 | await cache.set(cache_key, json.dumps(result), 10) |
38 | 36 |
|
39 | 37 | # Check if request is from a browser |
@@ -79,29 +77,29 @@ async def stock_news(request: Request, cache: RedisBackend = Depends(get_cache)) |
79 | 77 |
|
80 | 78 | return result |
81 | 79 |
|
82 | | -@router.get("/stock/{name}") |
83 | | -async def read_stock(request: Request, name: str, cache: RedisBackend = Depends(get_cache)): |
84 | | - # Use f-string to properly interpolate the name variable |
85 | | - cache_key = f"stock_{name}" |
| 80 | +@router.get("/stock/{symbol}") |
| 81 | +async def read_stock(request: Request, symbol: str, cache: RedisBackend = Depends(get_cache)): |
| 82 | + # Use f-string to properly interpolate the symbol variable |
| 83 | + cache_key = f"stock_{symbol}" |
86 | 84 | cached_result = await cache.get(cache_key) |
87 | 85 |
|
88 | 86 | if cached_result: |
89 | 87 | result = json.loads(cached_result) |
90 | 88 | else: |
91 | | - result = get_stock(name) |
| 89 | + result = get_stock(symbol) |
92 | 90 | await cache.set(cache_key, json.dumps(result), 10) |
93 | 91 |
|
94 | 92 | # Check if request is from a browser |
95 | 93 | accept_header = request.headers.get("accept", "") |
96 | 94 | if "text/html" in accept_header: |
97 | 95 | return templates.TemplateResponse("route.html", { |
98 | 96 | "request": request, |
99 | | - "route_path": f"/stock/{{{name}}}", |
| 97 | + "route_path": f"/stock/{{{symbol}}}", |
100 | 98 | "method": "GET", |
101 | | - "full_path": request.url.scheme + "://" + request.url.netloc + f"/stock/{name}", |
| 99 | + "full_path": request.url.scheme + "://" + request.url.netloc + f"/stock/{symbol}", |
102 | 100 | "description": "Returns detailed information about a specific stock", |
103 | 101 | "parameters": [ |
104 | | - {"name": "name", "type": "string", "description": "Stock symbol (e.g., AAPL, MSFT)"} |
| 102 | + {"name": "symbol", "type": "string", "description": "Stock symbol (e.g., AAPL, MSFT)"} |
105 | 103 | ], |
106 | 104 | "example_response": json.dumps(result, indent=2), |
107 | 105 | "current_year": datetime.datetime.now().year |
|
0 commit comments