65 lines
2.4 KiB
Plaintext
65 lines
2.4 KiB
Plaintext
result = {'widgettype': 'Error', 'options': {'title': 'Error', 'message': '', 'timeout': 3}}
|
||
|
||
try:
|
||
source_upappid = params_kw.get('source_upappid', '')
|
||
target_upappid = params_kw.get('target_upappid', '')
|
||
|
||
if not source_upappid:
|
||
raise Exception('请选择源上位系统')
|
||
if not target_upappid:
|
||
raise Exception('请选择目标上位系统')
|
||
if source_upappid == target_upappid:
|
||
raise Exception('源和目标上位系统不能相同')
|
||
|
||
db = DBPools()
|
||
dbname = get_module_dbname('uapi')
|
||
async with db.sqlorContext(dbname) as sor:
|
||
sql = 'select * from uapi where upappid = ${upappid}$'
|
||
source_uapis = await sor.sqlExe(sql, {'upappid': source_upappid})
|
||
|
||
if not source_uapis:
|
||
raise Exception('源上位系统中没有API记录')
|
||
|
||
existing_recs = await sor.sqlExe(sql, {'upappid': target_upappid})
|
||
existing_names = {r['name'] for r in existing_recs}
|
||
|
||
copied = 0
|
||
skipped = 0
|
||
for api in source_uapis:
|
||
if api['name'] in existing_names:
|
||
skipped += 1
|
||
continue
|
||
|
||
new_id = getID()
|
||
await sor.C('uapi', {
|
||
'id': new_id,
|
||
'upappid': target_upappid,
|
||
'name': api.get('name', ''),
|
||
'title': api.get('title', ''),
|
||
'description': api.get('description', ''),
|
||
'need_auth': api.get('need_auth', '0'),
|
||
'stream': api.get('stream', ''),
|
||
'path': api.get('path', ''),
|
||
'httpmethod': api.get('httpmethod', 'GET'),
|
||
'chunk_match': api.get('chunk_match', ''),
|
||
'headers': api.get('headers', ''),
|
||
'params': api.get('params', ''),
|
||
'data': api.get('data', ''),
|
||
'response': api.get('response', ''),
|
||
'ioid': api.get('ioid', ''),
|
||
'callbackurl': api.get('callbackurl', '')
|
||
})
|
||
copied += 1
|
||
|
||
msg = f'成功复制 {copied} 条API记录'
|
||
if skipped > 0:
|
||
msg += f',跳过 {skipped} 条(API名称已存在)'
|
||
|
||
result = {'widgettype': 'Message', 'options': {'title': '复制完成', 'message': msg, 'timeout': 3, 'type': 'success'}}
|
||
|
||
except Exception as e:
|
||
debug(f'copy_uapi error: {format_exc()}')
|
||
result = {'widgettype': 'Error', 'options': {'title': '复制失败', 'message': str(e), 'timeout': 5}}
|
||
|
||
return result
|