ForcePilot/backend/package/yuxi/channel/interfaces/rest/router/bindings.py

147 lines
4.4 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from datetime import datetime
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field
from yuxi.channel.container import ChannelContainer, get_channel
from yuxi.channel.domain.exception.duplicate_binding import DuplicateBindingException
from yuxi.channel.domain.exception.invalid_agent_config import InvalidAgentConfigException
from yuxi.channel.interfaces.rest.auth.depends import channel_auth_depends
logger = logging.getLogger(__name__)
router = APIRouter()
class BindingCreateRequest(BaseModel):
channel_type: str = Field(..., min_length=1, max_length=20)
account_id: str = Field(..., min_length=1, max_length=64)
group_id: str = Field("", max_length=128)
agent_config_id: int = Field(..., gt=0)
class BindingUpdateRequest(BaseModel):
agent_config_id: int | None = None
is_enabled: bool | None = None
class BindingResponse(BaseModel):
id: int
channel_type: str
account_id: str
group_id: str
agent_config_id: int
is_enabled: bool
created_at: datetime
class PaginatedMeta(BaseModel):
total: int
offset: int
limit: int
class BindingListResponse(BaseModel):
items: list[BindingResponse]
meta: PaginatedMeta
@router.post("/channel/bindings", response_model=BindingResponse, status_code=201)
async def create_binding(
req: BindingCreateRequest,
channel: ChannelContainer = Depends(get_channel),
_: bool = Depends(channel_auth_depends),
):
try:
created = await channel.binding_service.create(
channel_type=req.channel_type,
account_id=req.account_id,
group_id=req.group_id,
agent_config_id=req.agent_config_id,
)
except DuplicateBindingException as e:
raise HTTPException(status_code=409, detail=str(e))
except InvalidAgentConfigException as e:
raise HTTPException(status_code=400, detail=str(e))
return BindingResponse(
id=created.id,
channel_type=created.channel_type,
account_id=created.account_id,
group_id=created.group_id,
agent_config_id=created.agent_config_id,
is_enabled=created.is_enabled,
created_at=created.created_at,
)
@router.get("/channel/bindings", response_model=BindingListResponse)
async def list_bindings(
channel_type: str | None = None,
offset: int = 0,
limit: int = 50,
channel: ChannelContainer = Depends(get_channel),
_: bool = Depends(channel_auth_depends),
):
bindings, total = await channel.binding_service.list(
channel_type=channel_type,
offset=offset,
limit=limit,
)
return BindingListResponse(
items=[
BindingResponse(
id=b.id,
channel_type=b.channel_type,
account_id=b.account_id,
group_id=b.group_id,
agent_config_id=b.agent_config_id,
is_enabled=b.is_enabled,
created_at=b.created_at,
)
for b in bindings
],
meta=PaginatedMeta(total=total, offset=offset, limit=limit),
)
@router.patch("/channel/bindings/{binding_id}", response_model=BindingResponse)
async def update_binding(
binding_id: int,
req: BindingUpdateRequest,
channel: ChannelContainer = Depends(get_channel),
_: bool = Depends(channel_auth_depends),
):
if req.agent_config_id is None and req.is_enabled is None:
raise HTTPException(status_code=400, detail="no fields to update")
updated = await channel.binding_service.update(
binding_id, agent_config_id=req.agent_config_id, is_enabled=req.is_enabled
)
if not updated:
raise HTTPException(status_code=404, detail="binding not found")
return BindingResponse(
id=updated.id,
channel_type=updated.channel_type,
account_id=updated.account_id,
group_id=updated.group_id,
agent_config_id=updated.agent_config_id,
is_enabled=updated.is_enabled,
created_at=updated.created_at,
)
@router.delete("/channel/bindings/{binding_id}")
async def delete_binding(
binding_id: int,
channel: ChannelContainer = Depends(get_channel),
_: bool = Depends(channel_auth_depends),
):
deleted = await channel.binding_service.delete(binding_id)
if not deleted:
raise HTTPException(status_code=404, detail="binding not found")
return {"ok": True}