""" Attachment API Handler 职责: - 加载附件列表(依赖 Project) - 附件创建/更新/删除(批量接口) """ from typing import List, Dict, Any from ...datasource.api.handler import BaseApiHandler from ...datasource.task_result import TaskResult from ...common.sync_node import SyncNode from .sync_node import AttachmentSyncNode, AttachmentSchema class AttachmentApiHandler(BaseApiHandler): """附件 API Handler""" def __init__(self): super().__init__(node_class=AttachmentSyncNode, schema=AttachmentSchema) async def load(self) -> List[SyncNode]: """加载附件列表(依赖 Project)""" projects = self._collection.filter(node_type="project") project_ids = [p.data_id for p in projects if p.data_id] if not project_ids: return [] all_attachments = [] for project_id in project_ids: try: attachments = await api_get_attachment_list(self.api_client, project_id) all_attachments.extend(attachments) except Exception as e: print(f"❌ Failed to load attachments for project {project_id}: {e}") return all_attachments async def create_all(self, nodes: List[SyncNode]) -> List[TaskResult]: """批量创建附件(已禁用)""" return [ TaskResult.failed(node_id=node.node_id, error="Attachment push disabled") for node in nodes ] async def update_all(self, nodes: List[SyncNode]) -> List[TaskResult]: """批量更新附件(已禁用)""" return [ TaskResult.failed(node_id=node.node_id, error="Attachment push disabled") for node in nodes ] async def delete_all(self, nodes: List[SyncNode]) -> List[TaskResult]: """批量删除附件(已禁用)""" return [ TaskResult.failed(node_id=node.node_id, error="Attachment push disabled") for node in nodes ] async def poll_tasks(self, task_ids: List[str]) -> Dict[str, TaskResult]: """轮询异步任务状态(占位)""" return await super().poll_tasks(task_ids) def _create_node(self, data: Dict[str, Any]) -> SyncNode: node = self._create_basic_node(data, depend_ids=[]) if data.get("project_id"): node.context["project_id"] = data["project_id"] return node # ========== 静态 API 函数 ========== async def api_get_attachment_list(client, project_id: str) -> List[Dict[str, Any]]: response = await client.request( method="GET", endpoint="/attachment/list", params={"project_id": project_id} ) if response.get("code") != 200: raise Exception(f"API error: {response.get('message', 'Unknown error')}") return response.get("list", []) async def api_create_attachment(client, data: Dict[str, Any], push_id: str, biz_id: str) -> Dict[str, Any]: request_data = {"push_id": push_id, "biz_id": biz_id, **data} response = await client.request(method="POST", endpoint="/attachment/create", data=request_data) if response.get("code") != 200: raise Exception(f"API error: {response.get('message', 'Unknown error')}") return response async def api_delete_attachment(client, attachment_id: str, push_id: str, biz_id: str) -> None: request_data = {"push_id": push_id, "biz_id": biz_id, "id": attachment_id} response = await client.request(method="DELETE", endpoint="/attachment/delete", data=request_data) if response.get("code") != 200: raise Exception(f"API error: {response.get('message', 'Unknown error')}")