126 lines
3.9 KiB
Python
126 lines
3.9 KiB
Python
"""services/integration_connector/capability.py
|
||
|
||
用途:Capability 强类型契约注册表(能力名称/输入输出 Schema/幂等语义/是否写)。
|
||
依赖:app.core.errors、errors.py;纯代码,不依赖 DB。
|
||
逻辑:能力必须由代码注册,不能由管理端动态定义;领域只调用已注册能力,未注册 fail-closed。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass
|
||
|
||
from app.core.errors import ValidationError
|
||
from app.services.integration_connector.errors import CapabilityNotFoundError
|
||
|
||
# 在途调拨能力输入/输出 Schema(JSON Schema 最小子集,供入参/出参校验)。
|
||
_QUERY_INPUT_SCHEMA: dict = {
|
||
"type": "object",
|
||
"required": ["invoice_no", "from_warehouse", "to_warehouse", "quantity"],
|
||
"properties": {
|
||
"invoice_no": {"type": "string"},
|
||
"from_warehouse": {"type": "string"},
|
||
"to_warehouse": {"type": "string"},
|
||
"quantity": {"type": "number"},
|
||
},
|
||
}
|
||
_QUERY_OUTPUT_SCHEMA: dict = {
|
||
"type": "object",
|
||
"properties": {
|
||
"found": {"type": "boolean"},
|
||
"quantity": {"type": ["number", "null"]},
|
||
},
|
||
}
|
||
_WRITE_OUTPUT_SCHEMA: dict = {
|
||
"type": "object",
|
||
"required": ["status"],
|
||
"properties": {
|
||
"status": {
|
||
"type": "string",
|
||
"enum": ["success", "business_failed", "technical_failed", "timeout", "result_unknown"],
|
||
},
|
||
"document_no": {"type": ["string", "null"]},
|
||
"verified_posted": {"type": ["boolean", "null"]},
|
||
},
|
||
}
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class CapabilitySpec:
|
||
"""强类型能力契约(由代码注册)。"""
|
||
|
||
name: str
|
||
input_schema: dict
|
||
output_schema: dict
|
||
idempotency: str
|
||
write: bool
|
||
|
||
|
||
_CAPABILITIES: dict[str, CapabilitySpec] = {}
|
||
_LOADED = False
|
||
|
||
|
||
def register_capability(spec: CapabilitySpec) -> CapabilitySpec:
|
||
"""注册能力;同名重复注册抛 ValidationError。"""
|
||
if spec.name in _CAPABILITIES:
|
||
raise ValidationError(
|
||
f"capability already registered: {spec.name}",
|
||
code="INTEGRATION.CAPABILITY_DUPLICATE",
|
||
details={"name": spec.name},
|
||
)
|
||
_CAPABILITIES[spec.name] = spec
|
||
return spec
|
||
|
||
|
||
def get_capability(name: str) -> CapabilitySpec:
|
||
"""按名查询能力;未注册抛 CapabilityNotFoundError(fail-closed)。"""
|
||
spec = _CAPABILITIES.get(name)
|
||
if spec is None:
|
||
raise CapabilityNotFoundError(name=name)
|
||
return spec
|
||
|
||
|
||
def list_capabilities() -> list[CapabilitySpec]:
|
||
"""返回全部已注册能力(按名排序)。"""
|
||
return sorted(_CAPABILITIES.values(), key=lambda s: s.name)
|
||
|
||
|
||
def ensure_capabilities_loaded() -> None:
|
||
"""惰性注册内置能力(幂等);由网关/API 在解析前调用。"""
|
||
global _LOADED # noqa: PLW0603
|
||
if _LOADED:
|
||
return
|
||
_LOADED = True
|
||
_register_transfer_capabilities()
|
||
|
||
|
||
def _register_transfer_capabilities() -> None:
|
||
"""注册在途调拨域内置能力;领域代码只调这些能力,外部系统差异全在连接器层。"""
|
||
register_capability(CapabilitySpec(
|
||
name="in_transit_transfer.query_inventory",
|
||
input_schema=_QUERY_INPUT_SCHEMA,
|
||
output_schema=_QUERY_OUTPUT_SCHEMA,
|
||
idempotency="none",
|
||
write=False,
|
||
))
|
||
register_capability(CapabilitySpec(
|
||
name="in_transit_transfer.create_transfer",
|
||
input_schema=_QUERY_INPUT_SCHEMA,
|
||
output_schema=_WRITE_OUTPUT_SCHEMA,
|
||
idempotency="invoice",
|
||
write=True,
|
||
))
|
||
register_capability(CapabilitySpec(
|
||
name="in_transit_transfer.verify_duplicate",
|
||
input_schema=_QUERY_INPUT_SCHEMA,
|
||
output_schema=_WRITE_OUTPUT_SCHEMA,
|
||
idempotency="invoice",
|
||
write=False,
|
||
))
|
||
|
||
|
||
def reset_capability_registry_for_tests() -> None:
|
||
"""单测隔离:清空注册表并重置加载标记。"""
|
||
global _LOADED # noqa: PLW0603
|
||
_CAPABILITIES.clear()
|
||
_LOADED = False
|