5 new endpoints under /api/v1/cn/:
- GET /cn/a-share/{symbol}/quote - A-share real-time quote
- GET /cn/a-share/{symbol}/historical - A-share historical OHLCV
- GET /cn/a-share/search?query= - search A-shares by name
- GET /cn/hk/{symbol}/quote - HK stock real-time quote
- GET /cn/hk/{symbol}/historical - HK stock historical OHLCV
Features:
- Chinese column names auto-mapped to English
- Symbol validation: A-share ^[036]\d{5}$, HK ^\d{5}$
- qfq (forward-adjusted) prices by default
- 79 new tests (51 service unit + 28 route integration)
- All 470 tests passing
104 lines
3.4 KiB
Python
104 lines
3.4 KiB
Python
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
# Patch curl_cffi to use safari TLS fingerprint instead of chrome.
|
|
# curl_cffi's chrome impersonation triggers BoringSSL SSL_ERROR_SYSCALL on
|
|
# some networks; safari works reliably. This must happen before any import
|
|
# that creates a curl_cffi Session (yfinance, openbb).
|
|
import curl_cffi.requests as _cffi_requests
|
|
|
|
_orig_session_init = _cffi_requests.Session.__init__
|
|
|
|
|
|
def _patched_session_init(self, *args, **kwargs):
|
|
if kwargs.get("impersonate") == "chrome":
|
|
kwargs["impersonate"] = "safari"
|
|
_orig_session_init(self, *args, **kwargs)
|
|
|
|
|
|
_cffi_requests.Session.__init__ = _patched_session_init
|
|
|
|
from openbb import obb # noqa: E402 - must be after curl_cffi patch
|
|
|
|
from config import settings # noqa: E402
|
|
from routes import router # noqa: E402
|
|
from routes_calendar import router as calendar_router # noqa: E402
|
|
from routes_economy import router as economy_router # noqa: E402
|
|
from routes_fixed_income import router as fixed_income_router # noqa: E402
|
|
from routes_macro import router as macro_router # noqa: E402
|
|
from routes_market import router as market_router # noqa: E402
|
|
from routes_quantitative import router as quantitative_router # noqa: E402
|
|
from routes_regulators import router as regulators_router # noqa: E402
|
|
from routes_sentiment import router as sentiment_router # noqa: E402
|
|
from routes_shorts import router as shorts_router # noqa: E402
|
|
from routes_surveys import router as surveys_router # noqa: E402
|
|
from routes_technical import router as technical_router # noqa: E402
|
|
from routes_portfolio import router as portfolio_router # noqa: E402
|
|
from routes_backtest import router as backtest_router # noqa: E402
|
|
from routes_cn import router as cn_router # noqa: E402
|
|
|
|
logging.basicConfig(
|
|
level=settings.log_level.upper(),
|
|
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Register provider credentials once at startup."""
|
|
if settings.fred_api_key:
|
|
obb.user.credentials.fred_api_key = settings.fred_api_key
|
|
logger.info("FRED API key registered")
|
|
yield
|
|
|
|
|
|
app = FastAPI(
|
|
title="OpenBB Investment Analysis API",
|
|
version="0.1.0",
|
|
description="REST API for stock data and rule-based investment analysis, powered by OpenBB SDK.",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins,
|
|
allow_credentials=False,
|
|
allow_methods=["GET", "POST"],
|
|
allow_headers=["Content-Type", "Authorization"],
|
|
)
|
|
|
|
app.include_router(router)
|
|
app.include_router(sentiment_router)
|
|
app.include_router(macro_router)
|
|
app.include_router(technical_router)
|
|
app.include_router(quantitative_router)
|
|
app.include_router(calendar_router)
|
|
app.include_router(market_router)
|
|
app.include_router(shorts_router)
|
|
app.include_router(fixed_income_router)
|
|
app.include_router(economy_router)
|
|
app.include_router(surveys_router)
|
|
app.include_router(regulators_router)
|
|
app.include_router(portfolio_router)
|
|
app.include_router(backtest_router)
|
|
app.include_router(cn_router)
|
|
|
|
|
|
@app.get("/health", response_model=dict[str, str])
|
|
async def health() -> dict[str, str]:
|
|
return {"status": "ok"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(
|
|
"main:app",
|
|
host=settings.host,
|
|
port=settings.port,
|
|
reload=settings.debug,
|
|
)
|