feat: news endpoint

This commit is contained in:
ryana mittens 2024-09-06 17:07:11 +08:00
parent 39f67b45de
commit 6e3ba3701e
2 changed files with 37 additions and 0 deletions

35
app/api/v1/routes/news.py Normal file
View file

@ -0,0 +1,35 @@
from fastapi import APIRouter, Depends
import requests
from app.core.config import settings
from app.core.websocket import start_finnhub_websocket
router = APIRouter()
@router.get("/search")
async def lookup(query: str, exchange: str | None = None):
"""
API to search for a stock symbol.
"""
params = {
"token": settings.FINNHUB_API_KEY,
"q": query
}
if exchange:
params["exchange"] = exchange
response = requests.get("https://finnhub.io/api/v1/search", params=params)
return response.json()
@router.get("/marketnews")
async def market_news():
"""
API to get latest market news.
"""
params = {
"token": settings.FINNHUB_API_KEY,
"category": "general"
}
response = requests.get("https://finnhub.io/api/v1/news", params=params)
return response.json()

View file

@ -1,6 +1,7 @@
from fastapi import FastAPI from fastapi import FastAPI
from app.core.config import settings from app.core.config import settings
from app.api.v1.routes import stock from app.api.v1.routes import stock
from app.api.v1.routes import news
from app.ws.routes import trades from app.ws.routes import trades
app = FastAPI() app = FastAPI()
@ -9,6 +10,7 @@ app = FastAPI()
@app.on_event("startup") @app.on_event("startup")
async def startup_event(): async def startup_event():
app.include_router(stock.router, prefix="/api/v1") app.include_router(stock.router, prefix="/api/v1")
app.include_router(news.router, prefix="/api/v1")
app.include_router(trades.router, prefix="/ws") app.include_router(trades.router, prefix="/ws")
pass pass