|
| 1 | +"""Integrations management and dispatch API routes.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from fastapi import APIRouter, Depends, HTTPException, status |
| 6 | +from pydantic import BaseModel |
| 7 | +from sqlalchemy import select |
| 8 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 9 | + |
| 10 | +from docsgraph_server.db.models import DbIntegrationConfig |
| 11 | +from docsgraph_server.db.session import get_session |
| 12 | +from docsgraph_server.integrations.engine import IntegrationManager |
| 13 | + |
| 14 | +router = APIRouter(prefix="/integrations", tags=["integrations"]) |
| 15 | + |
| 16 | + |
| 17 | +class IntegrationConfigUpdate(BaseModel): |
| 18 | + """Pydantic model for updating an integration configuration.""" |
| 19 | + |
| 20 | + enabled: bool |
| 21 | + config_json: dict[str, Any] |
| 22 | + |
| 23 | + |
| 24 | +class IntegrationStatusResponse(BaseModel): |
| 25 | + """Pydantic model for integration status responses.""" |
| 26 | + |
| 27 | + id: str |
| 28 | + name: str |
| 29 | + enabled: bool |
| 30 | + config_json: dict[str, Any] |
| 31 | + |
| 32 | + |
| 33 | +class DispatchTestRequest(BaseModel): |
| 34 | + """Pydantic model for triggering a test event dispatch.""" |
| 35 | + |
| 36 | + event_type: str |
| 37 | + payload: dict[str, Any] |
| 38 | + |
| 39 | + |
| 40 | +@router.get("", response_model=list[IntegrationStatusResponse]) |
| 41 | +async def list_integrations( |
| 42 | + db: AsyncSession = Depends(get_session), # noqa: B008 |
| 43 | +) -> list[dict[str, Any]]: |
| 44 | + """List all registered integrations and their current configurations.""" |
| 45 | + manager = IntegrationManager() |
| 46 | + await manager.load_configurations(db) |
| 47 | + |
| 48 | + response = [] |
| 49 | + for integration_id, integration in manager.registry.items(): |
| 50 | + result = await db.execute( |
| 51 | + select(DbIntegrationConfig).where(DbIntegrationConfig.id == integration_id) |
| 52 | + ) |
| 53 | + db_cfg = result.scalar_one_or_none() |
| 54 | + |
| 55 | + response.append( |
| 56 | + { |
| 57 | + "id": integration_id, |
| 58 | + "name": integration.get_name(), |
| 59 | + "enabled": db_cfg.enabled if db_cfg else False, |
| 60 | + "config_json": db_cfg.config_json if db_cfg else {}, |
| 61 | + } |
| 62 | + ) |
| 63 | + return response |
| 64 | + |
| 65 | + |
| 66 | +@router.post("/{integration_id}/configure", response_model=IntegrationStatusResponse) |
| 67 | +async def configure_integration( |
| 68 | + integration_id: str, |
| 69 | + req: IntegrationConfigUpdate, |
| 70 | + db: AsyncSession = Depends(get_session), # noqa: B008 |
| 71 | +) -> dict[str, Any]: |
| 72 | + """Configure and enable/disable a specific integration.""" |
| 73 | + manager = IntegrationManager() |
| 74 | + if integration_id not in manager.registry: |
| 75 | + raise HTTPException( |
| 76 | + status_code=status.HTTP_404_NOT_FOUND, |
| 77 | + detail=f"Integration '{integration_id}' not found", |
| 78 | + ) |
| 79 | + |
| 80 | + result = await db.execute( |
| 81 | + select(DbIntegrationConfig).where(DbIntegrationConfig.id == integration_id) |
| 82 | + ) |
| 83 | + db_cfg = result.scalar_one_or_none() |
| 84 | + |
| 85 | + if not db_cfg: |
| 86 | + db_cfg = DbIntegrationConfig( |
| 87 | + id=integration_id, |
| 88 | + enabled=req.enabled, |
| 89 | + config_json=req.config_json, |
| 90 | + ) |
| 91 | + db.add(db_cfg) |
| 92 | + else: |
| 93 | + db_cfg.enabled = req.enabled |
| 94 | + db_cfg.config_json = req.config_json |
| 95 | + |
| 96 | + await db.commit() |
| 97 | + |
| 98 | + await manager.load_configurations(db) |
| 99 | + integration = manager.registry[integration_id] |
| 100 | + |
| 101 | + return { |
| 102 | + "id": integration_id, |
| 103 | + "name": integration.get_name(), |
| 104 | + "enabled": db_cfg.enabled, |
| 105 | + "config_json": db_cfg.config_json, |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +@router.post("/dispatch") |
| 110 | +async def dispatch_event( |
| 111 | + req: DispatchTestRequest, |
| 112 | + db: AsyncSession = Depends(get_session), # noqa: B008 |
| 113 | +) -> dict[str, Any]: |
| 114 | + """Dispatch a test event to all active integrations.""" |
| 115 | + manager = IntegrationManager() |
| 116 | + await manager.load_configurations(db) |
| 117 | + outcomes = await manager.dispatch(req.event_type, req.payload) |
| 118 | + return {"outcomes": outcomes} |
0 commit comments