connectors.py
3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""厂商系统连接器登记处(云端侧)。一个连接器 = 一套厂商 DMS 的登录方式和接口;取数端里有同名的实现
(edge/edgeclient/connectors/<key>/)。门店卖哪个品牌,就在后台给门店配哪个连接器的账号。
以后接入新品牌:这里加一个 Connector(账号字段 + 连接参数默认值),取数端加对应实现,业务功能声明支持哪些连接器。
"""
from __future__ import annotations
from dataclasses import dataclass
@dataclass(frozen=True)
class OptionDef:
key: str
title: str
default: object
description: str = ""
advanced: bool = False # 高级参数:后台默认折叠
@dataclass(frozen=True)
class Connector:
key: str
name: str
brand: str
description: str
options: tuple[OptionDef, ...]
def defaults(self) -> dict:
return {o.key: o.default for o in self.options}
def merged(self, stored: dict | None) -> dict:
out = self.defaults()
for o in self.options:
v = (stored or {}).get(o.key)
if v is None or v == "":
continue
d = o.default
try:
out[o.key] = (str(v).strip().lower() in ("1", "true", "yes", "on")) if isinstance(d, bool) else \
int(v) if isinstance(d, int) else float(v) if isinstance(d, float) else str(v).strip()
except (TypeError, ValueError):
raise ValueError(f"「{o.title}」格式不对:{v}")
return out
CONNECTORS: dict[str, Connector] = {
"gtmc": Connector(
key="gtmc", name="广汽丰田 DMS", brand="广汽丰田",
description="通过 IAM 统一认证登录,配件模块接口查询库存 / 全国仓 / 创建采购单,浏览器截图采购单页面",
options=(
OptionDef("dms_url", "DMS 首页地址", "https://dms.gtmc.com.cn/"),
OptionDef("iam_base", "IAM 认证地址", "https://iam.gtmc.com.cn:18010"),
OptionDef("auth_api", "认证接口地址", "https://auth-api.dms.gtmc.com.cn"),
OptionDef("parts_api", "配件接口地址", "https://parts-api.dms.gtmc.com.cn"),
OptionDef("client_id", "客户端 ID", "S-80", advanced=True),
OptionDef("entity_id", "实体 ID", "S-80", advanced=True),
OptionDef("verify_ssl", "校验 DMS 证书", False, "门店专网证书常不完整,一般关闭", advanced=True),
OptionDef("warmup_dms", "登录前先打开 DMS 首页", True, advanced=True),
OptionDef("token_exchange_delay", "换 token 前等待(秒)", 2.0, "认证接口间歇性断连时适当调大", advanced=True),
OptionDef("token_exchange_retries", "换 token 重试次数", 7, "auth-api 断连时按 2~15 秒退避重试,只用授权码不会锁号",
advanced=True),
OptionDef("fallback_public_key", "备用 RSA 公钥", "", "仅在无法从 IAM 取公钥时使用,一般留空", advanced=True),
OptionDef("po_list_path", "采购单列表页路径", "dms-parts/purchaseOrderManage", advanced=True),
OptionDef("po_detail_path", "采购单详情页路径", "dms-parts/purchaseOrderDetails", advanced=True),
)),
}
def get(key: str) -> Connector:
c = CONNECTORS.get(key)
if not c:
raise ValueError(f"不支持的厂商系统:{key}")
return c
def catalog() -> list[dict]:
return [{"key": c.key, "name": c.name, "brand": c.brand, "description": c.description,
"options": [{"key": o.key, "title": o.title, "default": o.default, "description": o.description,
"advanced": o.advanced, "type": type(o.default).__name__} for o in c.options]}
for c in CONNECTORS.values()]