78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
# 联软LV7000配置管理
|
||
"""
|
||
从system_configs表读取联软API配置,构建LianruanClient实例。
|
||
|
||
联软配置键(前缀 integration_lianruan_):
|
||
- integration_lianruan_base_url: 联软API地址(如 http://192.168.x.x:30098)
|
||
- integration_lianruan_api_account: API账号
|
||
- integration_lianruan_api_password: API密码
|
||
- integration_lianruan_validate_key: 验证密钥(可选)
|
||
|
||
配置方式:管理后台 → 系统集成 → 联软LV7000 → 填入账号密码
|
||
"""
|
||
|
||
import logging
|
||
|
||
from sqlalchemy import select
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
||
from app.integrations.lianruan.client import LianruanClient
|
||
from app.integrations.lianruan.exceptions import LianruanConfigError
|
||
from app.models.system_config import SystemConfig
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
# 联软配置在 system_configs 表中的 key 前缀
|
||
LIANRUAN_CONFIG_PREFIX = "integration_lianruan_"
|
||
|
||
|
||
async def get_lianruan_client(db: AsyncSession) -> LianruanClient:
|
||
"""从系统配置表构建联软API客户端。
|
||
|
||
读取 integration_lianruan_ 前缀的配置项,构建 LianruanClient 实例。
|
||
如果任何必填配置缺失,抛出 LianruanConfigError。
|
||
|
||
Args:
|
||
db: 数据库会话
|
||
|
||
Returns:
|
||
LianruanClient: 已配置的联软API客户端实例
|
||
|
||
Raises:
|
||
LianruanConfigError: 缺少必填配置
|
||
"""
|
||
# 读取四个配置
|
||
result = await db.execute(
|
||
select(SystemConfig).where(
|
||
SystemConfig.config_key.startswith(LIANRUAN_CONFIG_PREFIX)
|
||
)
|
||
)
|
||
configs = list(result.scalars().all())
|
||
|
||
# 构建 key→value 映射
|
||
config_map = {cfg.config_key: cfg.config_value for cfg in configs}
|
||
|
||
base_url = config_map.get(f"{LIANRUAN_CONFIG_PREFIX}base_url", "")
|
||
api_account = config_map.get(f"{LIANRUAN_CONFIG_PREFIX}api_account", "")
|
||
api_password = config_map.get(f"{LIANRUAN_CONFIG_PREFIX}api_password", "")
|
||
validate_key = config_map.get(f"{LIANRUAN_CONFIG_PREFIX}validate_key", "")
|
||
|
||
# 校验必填项
|
||
missing = []
|
||
if not base_url:
|
||
missing.append("Base URL")
|
||
if not api_account:
|
||
missing.append("API账号")
|
||
if not api_password:
|
||
missing.append("API密码")
|
||
|
||
if missing:
|
||
raise LianruanConfigError(f"联软API未配置:缺少{', '.join(missing)}")
|
||
|
||
return LianruanClient(
|
||
base_url=base_url,
|
||
api_account=api_account,
|
||
api_password=api_password,
|
||
validate_key=validate_key or None,
|
||
)
|