2026-05-30 21:53:09 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
2026-05-31 21:42:03 +08:00
|
|
|
from datetime import datetime
|
2026-05-30 21:53:09 +08:00
|
|
|
|
2026-05-31 21:42:03 +08:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
2026-05-30 21:53:09 +08:00
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
|
|
|
from yuxi.channel.container import ChannelContainer, get_channel
|
2026-05-31 21:42:03 +08:00
|
|
|
from yuxi.channel.domain.exception.duplicate_binding import DuplicateBindingException
|
|
|
|
|
from yuxi.channel.domain.exception.invalid_agent_config import InvalidAgentConfigException
|
2026-05-30 21:53:09 +08:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2026-05-31 21:42:03 +08:00
|
|
|
class BindingUpdateRequest(BaseModel):
|
|
|
|
|
agent_config_id: int | None = None
|
|
|
|
|
is_enabled: bool | None = None
|
|
|
|
|
|
|
|
|
|
|
2026-05-30 21:53:09 +08:00
|
|
|
class BindingResponse(BaseModel):
|
|
|
|
|
id: int
|
|
|
|
|
channel_type: str
|
|
|
|
|
account_id: str
|
|
|
|
|
group_id: str
|
|
|
|
|
agent_config_id: int
|
|
|
|
|
is_enabled: bool
|
2026-05-31 21:42:03 +08:00
|
|
|
created_at: datetime
|
2026-05-30 21:53:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
):
|
2026-05-31 21:42:03 +08:00
|
|
|
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))
|
2026-05-30 21:53:09 +08:00
|
|
|
|
|
|
|
|
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,
|
2026-05-31 21:42:03 +08:00
|
|
|
offset: int = 0,
|
|
|
|
|
limit: int = 50,
|
2026-05-30 21:53:09 +08:00
|
|
|
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),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-05-31 21:42:03 +08:00
|
|
|
@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,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-05-30 21:53:09 +08:00
|
|
|
@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}
|