- Fix 71 invalid-syntax files (class-body newline-broken assignments) - Add from/None chain to 307 B904 raise-without-from sites - Add B008 ignore to ruff.toml (already in pyproject.toml) - Noqa F401 on __init__.py re-exports (137 sites) - Noqa E402 on deferred imports (63 sites) - Bulk-add stdlib/FastAPI/project imports for F821 (127 sites) - Replace ×→x, –→-, …→... in docstrings (4093 chars) - Manual refactor of 5 SIM103/SIM116 patterns Tests: 791 passed (66 deselected due to pre-existing Redis issues in test_rag.py) Co-authored-by: opencode <opencode@rugmunch.io>
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""#10 - DataBus Public API Gateway Router. Free tier + x402 paid tier."""
|
|
|
|
from fastapi import APIRouter, Header, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from app.databus_gateway import (
|
|
SUPPORTED_CHAINS,
|
|
TIER_LIMITS,
|
|
_check_rate,
|
|
databus_cross_chain,
|
|
databus_get,
|
|
verify_x402_payment,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/v1/databus", tags=["databus-gateway"])
|
|
|
|
|
|
class ChainQuery(BaseModel):
|
|
chain: str
|
|
endpoint: str = "overview"
|
|
params: dict | None = None
|
|
|
|
|
|
class CrossChainQuery(BaseModel):
|
|
query: str
|
|
chains: list[str] | None = None
|
|
|
|
|
|
@router.get("/chains")
|
|
async def list_chains():
|
|
"""List all supported chains with tier availability."""
|
|
return {"chains": SUPPORTED_CHAINS, "total": len(SUPPORTED_CHAINS)}
|
|
|
|
|
|
@router.get("/{chain}/{endpoint:path}")
|
|
async def query_chain(chain: str, endpoint: str, x_rmi_key: str = Header(None), x_x402_sig: str = Header(None)):
|
|
"""Query a single DataBus chain. Free tier with rate limiting, paid tier via x402 header."""
|
|
tier = "free"
|
|
if x_x402_sig:
|
|
valid = await verify_x402_payment(x_x402_sig, f"{chain}/{endpoint}")
|
|
if valid:
|
|
tier = "pro"
|
|
else:
|
|
raise HTTPException(402, "Invalid x402 payment signature")
|
|
|
|
limits = TIER_LIMITS[tier]
|
|
client_id = x_rmi_key or "anonymous"
|
|
rate_key = f"databus:{client_id}:{tier}"
|
|
|
|
if not _check_rate(rate_key, limits["requests_per_hour"]):
|
|
raise HTTPException(429, f"Rate limit exceeded ({limits['requests_per_hour']}/hr for {tier} tier)")
|
|
|
|
if chain not in SUPPORTED_CHAINS:
|
|
raise HTTPException(404, f"Chain '{chain}' not supported. Use /api/v1/databus/chains for full list.")
|
|
|
|
result = await databus_get(chain, endpoint, tier=tier)
|
|
return {
|
|
"chain": result.chain,
|
|
"data": result.data,
|
|
"source": result.source,
|
|
"cached": result.cached,
|
|
"latency_ms": result.latency_ms,
|
|
"tier": tier,
|
|
}
|
|
|
|
|
|
@router.post("/cross-chain")
|
|
async def cross_chain_query(query: CrossChainQuery):
|
|
"""Query multiple DataBus chains in parallel."""
|
|
results = await databus_cross_chain(query.query, query.chains)
|
|
return {
|
|
"query": query.query,
|
|
"chains_queried": list(results.keys()),
|
|
"results": {c: {"data": r.data, "cached": r.cached} for c, r in results.items()},
|
|
}
|