feat: saxo connector
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
from typing import List, Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
|
||||
from services import saxo_auth
|
||||
from services.saxo_scheduler import get_watchlist, set_watchlist
|
||||
from services.database import get_saxo_snapshots, get_saxo_snapshot_symbols
|
||||
from services.database import (
|
||||
get_saxo_snapshots, get_saxo_snapshot_symbols,
|
||||
get_saxo_catalog, get_saxo_catalog_summary, upsert_saxo_catalog_rows,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/saxo", tags=["saxo"])
|
||||
|
||||
@@ -90,3 +93,44 @@ def history(
|
||||
date_to: Optional[str] = Query(None),
|
||||
):
|
||||
return get_saxo_snapshots(symbol, date_from, date_to)
|
||||
|
||||
|
||||
class CatalogRefreshRequest(BaseModel):
|
||||
asset_types: Optional[List[str]] = None # default: CATALOG_ASSET_TYPES (FuturesOption, FxVanillaOption)
|
||||
|
||||
|
||||
@router.post("/catalog/refresh")
|
||||
def refresh_catalog(req: CatalogRefreshRequest):
|
||||
from services.saxo_client import list_instruments, CATALOG_ASSET_TYPES, SaxoNotConnected, SaxoApiError
|
||||
asset_types = req.asset_types or CATALOG_ASSET_TYPES
|
||||
counts: Dict[str, int] = {}
|
||||
try:
|
||||
for asset_type in asset_types:
|
||||
items = list_instruments(asset_type)
|
||||
rows = [{
|
||||
"uic": item.get("Identifier") or item.get("Uic"),
|
||||
"symbol": item.get("Symbol"),
|
||||
"asset_type": asset_type,
|
||||
"description": item.get("Description"),
|
||||
} for item in items if item.get("Identifier") or item.get("Uic")]
|
||||
upsert_saxo_catalog_rows(rows)
|
||||
counts[asset_type] = len(rows)
|
||||
except SaxoNotConnected as e:
|
||||
raise HTTPException(status_code=401, detail=str(e))
|
||||
except SaxoApiError as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
return {"refreshed": counts}
|
||||
|
||||
|
||||
@router.get("/catalog")
|
||||
def catalog(
|
||||
asset_type: Optional[str] = Query(None),
|
||||
q: Optional[str] = Query(None),
|
||||
limit: int = Query(200),
|
||||
):
|
||||
return get_saxo_catalog(asset_type, q, limit)
|
||||
|
||||
|
||||
@router.get("/catalog/summary")
|
||||
def catalog_summary():
|
||||
return get_saxo_catalog_summary()
|
||||
|
||||
Reference in New Issue
Block a user