Update app.py
Browse files
app.py
CHANGED
|
@@ -46,3 +46,36 @@ def csv_data():
|
|
| 46 |
error_message = f"Error reading the CSV file: {str(e)}"
|
| 47 |
return JSONResponse(content={"error": error_message}, status_code=500)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
error_message = f"Error reading the CSV file: {str(e)}"
|
| 47 |
return JSONResponse(content={"error": error_message}, status_code=500)
|
| 48 |
|
| 49 |
+
|
| 50 |
+
from pydantic import BaseModel
|
| 51 |
+
import random
|
| 52 |
+
import time
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# Define a Pydantic model for stock data
|
| 56 |
+
class StockData(BaseModel):
|
| 57 |
+
ticker: str
|
| 58 |
+
price: float
|
| 59 |
+
volume: int
|
| 60 |
+
timestamp: float
|
| 61 |
+
|
| 62 |
+
# Create a mock function to simulate fetching stock data
|
| 63 |
+
def generate_mock_stock_data(ticker: str):
|
| 64 |
+
return {
|
| 65 |
+
"ticker": ticker,
|
| 66 |
+
"price": round(random.uniform(100, 500), 2), # Generate a random stock price
|
| 67 |
+
"volume": random.randint(1000, 10000), # Random trading volume
|
| 68 |
+
"timestamp": time.time() # Current timestamp
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# Define the /api/stocks endpoint
|
| 72 |
+
@app.get("/api/stocks", response_model=StockData)
|
| 73 |
+
async def get_stock_data(ticker: str):
|
| 74 |
+
"""
|
| 75 |
+
Fetches stock data for a given ticker symbol.
|
| 76 |
+
The data includes price, volume, and timestamp.
|
| 77 |
+
"""
|
| 78 |
+
# Generate mock data for the given ticker
|
| 79 |
+
stock_data = generate_mock_stock_data(ticker)
|
| 80 |
+
return stock_data
|
| 81 |
+
|