first commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
"""
|
||||
Attachment Domain
|
||||
"""
|
||||
from .sync_node import AttachmentSyncNode
|
||||
from .sync_strategy import AttachmentSyncStrategy
|
||||
from .jsonl_handler import AttachmentJsonlHandler
|
||||
|
||||
__all__ = ["AttachmentSyncNode", "AttachmentSyncStrategy", "AttachmentJsonlHandler"]
|
||||
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
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__()
|
||||
self._node_type = "attachment"
|
||||
self._node_class = AttachmentSyncNode
|
||||
self._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')}")
|
||||
@@ -0,0 +1,12 @@
|
||||
"""
|
||||
Attachment JSONL Handler
|
||||
"""
|
||||
from typing import Dict, Any
|
||||
from ...datasource import BaseJsonlHandler, JsonlDataSource
|
||||
from .sync_node import AttachmentSyncNode
|
||||
|
||||
|
||||
class AttachmentJsonlHandler(BaseJsonlHandler):
|
||||
"""附件 JSONL Handler"""
|
||||
def __init__(self, datasource: JsonlDataSource):
|
||||
super().__init__(datasource, "attachment", AttachmentSyncNode, Dict[str, Any])
|
||||
@@ -0,0 +1,16 @@
|
||||
"""Attachment domain registration"""
|
||||
from ...common.registry import DomainRegistry
|
||||
from .sync_node import AttachmentSyncNode, AttachmentSchema
|
||||
from .sync_strategy import AttachmentSyncStrategy
|
||||
from .jsonl_handler import AttachmentJsonlHandler
|
||||
from .api_handler import AttachmentApiHandler
|
||||
|
||||
# 注册 attachment domain
|
||||
DomainRegistry.register(
|
||||
node_type="attachment",
|
||||
schema=AttachmentSchema,
|
||||
node_class=AttachmentSyncNode,
|
||||
strategy_class=AttachmentSyncStrategy,
|
||||
jsonl_handler_class=AttachmentJsonlHandler,
|
||||
api_handler_class=AttachmentApiHandler,
|
||||
)
|
||||
@@ -0,0 +1,17 @@
|
||||
"""
|
||||
Attachment Domain
|
||||
"""
|
||||
from typing import Dict, Any
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from ...common import SyncNode
|
||||
|
||||
|
||||
class AttachmentSchema(BaseModel):
|
||||
"""允许任意附件字段的 schema"""
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
|
||||
class AttachmentSyncNode(SyncNode[Dict[str, Any]]):
|
||||
"""附件同步节点"""
|
||||
node_type = "attachment"
|
||||
schema = AttachmentSchema
|
||||
@@ -0,0 +1,20 @@
|
||||
from ...sync_system.strategy import DefaultSyncStrategy
|
||||
from ...sync_system.config import StrategyConfig, OrphanAction, UpdateDirection
|
||||
from .sync_node import AttachmentSchema
|
||||
|
||||
|
||||
class AttachmentSyncStrategy(DefaultSyncStrategy[AttachmentSchema]):
|
||||
"""
|
||||
Attachment 同步策略(仅作为依赖,不执行同步动作)。
|
||||
"""
|
||||
|
||||
schema = AttachmentSchema
|
||||
|
||||
default_config = StrategyConfig(
|
||||
auto_bind=False,
|
||||
auto_bind_fields=[],
|
||||
depend_fields={},
|
||||
local_orphan_action=OrphanAction.NONE,
|
||||
remote_orphan_action=OrphanAction.NONE,
|
||||
update_direction=UpdateDirection.PUSH,
|
||||
)
|
||||
Reference in New Issue
Block a user