mirror of
https://github.com/hoshikawa2/agent_platform_oci.git
synced 2026-07-09 22:04:21 +00:00
Ajustes na documentação e remanejamento dos folders
This commit is contained in:
40
apps/agent_gateway/app/routes/governed_proxy_example.py
Normal file
40
apps/agent_gateway/app/routes/governed_proxy_example.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
|
||||
from app.governance_middleware import AgentGatewayGovernance
|
||||
|
||||
router = APIRouter()
|
||||
governance = AgentGatewayGovernance()
|
||||
|
||||
|
||||
@router.post("/gateway/message/governed")
|
||||
async def governed_gateway_message(request: Request):
|
||||
"""Example governed proxy route.
|
||||
|
||||
Use as reference to patch the existing /gateway/message handler.
|
||||
"""
|
||||
|
||||
body: dict[str, Any] = await request.json()
|
||||
backend_url = os.getenv("DEFAULT_AGENT_BACKEND_URL", "http://localhost:8000")
|
||||
|
||||
governed_body, headers = governance.prepare_backend_request(body)
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=90) as client:
|
||||
resp = await client.post(
|
||||
f"{backend_url.rstrip('/')}/gateway/message",
|
||||
json=governed_body,
|
||||
headers=headers,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
return governance.process_backend_response(data)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
raise HTTPException(status_code=exc.response.status_code, detail=exc.response.text) from exc
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||
Reference in New Issue
Block a user