5.2 Building Your Own API Layer
Once the engine returns structured opportunities, you can wrap it with an HTTP/WebSocket API and plug it anywhere.
5.2.1 Minimal FastAPI wrapper (REST + WebSocket)
# api_server.py
import asyncio
from fastapi import FastAPI, WebSocket
from capture_engine import CaptureEngine
app = FastAPI()
engine = CaptureEngine(min_profit_threshold=0.2, sleep_seconds=2)
latest_opportunities: list[dict] = []
async def engine_loop():
global latest_opportunities
while True:
latest_opportunities = await engine.tick()
await asyncio.sleep(engine.sleep_seconds)
@app.on_event("startup")
async def startup_event():
asyncio.create_task(engine_loop())
@app.get("/opportunities")
async def get_opportunities():
return latest_opportunities
@app.websocket("/ws")
async def ws_opportunities(ws: WebSocket):
await ws.accept()
try:
while True:
await ws.send_json(latest_opportunities)
await asyncio.sleep(engine.sleep_seconds)
except Exception:
await ws.close()Now you have:
GET /opportunities→ JSON snapshot (top N opportunities).WS /ws→ periodic push of the same JSON.
You can swap FastAPI with any other framework; the pattern is the same.
Last updated

