market.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. 智能股票分析助手 — 行情数据API路由
  3. 提供个股行情、指数行情、板块行情查询接口。
  4. """
  5. from fastapi import APIRouter, Query
  6. from app.services import market_service
  7. from app.utils.mx_http import mx_result_to_http
  8. from app.utils.response import error_response
  9. router = APIRouter(prefix="/market", tags=["行情数据"])
  10. @router.get("/quote/{code}")
  11. async def get_stock_quote(code: str):
  12. """获取个股实时行情
  13. - **code**: 6位股票代码,如 600519(贵州茅台)、000001(平安银行)
  14. """
  15. if not code or len(code) < 4:
  16. return error_response(code=400, message="请输入有效的股票代码")
  17. result = market_service.get_stock_quote(code)
  18. return mx_result_to_http(result)
  19. @router.get("/index")
  20. async def get_index_quote(name: str = Query(default="沪深300", description="指数名称")):
  21. """获取指数行情
  22. - **name**: 指数名称,如 沪深300、上证指数、创业板指
  23. """
  24. result = market_service.get_index_quote(name)
  25. return mx_result_to_http(result)
  26. @router.get("/sector/{name}")
  27. async def get_sector_quote(name: str):
  28. """获取板块行情
  29. - **name**: 板块名称,如 白酒、新能源、半导体
  30. """
  31. if not name:
  32. return error_response(code=400, message="请输入板块名称")
  33. result = market_service.get_sector_quote(name)
  34. return mx_result_to_http(result)