extra_api.py 2.5 KB

123456789101112131415161718192021222324252627282930313233
  1. import json,urllib.request,urllib.error
  2. BASE='http://localhost:8980/js/a'
  3. def call(m,p,b=None,t=None):
  4. d=json.dumps(b).encode() if b is not None else None
  5. r=urllib.request.Request(BASE+p,data=d,method=m); r.add_header('Content-Type','application/json')
  6. if t:r.add_header('x-token',t)
  7. try:
  8. with urllib.request.urlopen(r,timeout=30) as x:return json.loads(x.read().decode())
  9. except urllib.error.HTTPError as e:return {'http':e.code,'body':e.read().decode()[:300]}
  10. def login(u,p):return call('POST','/auth/login',{'username':u,'password':p,'force':True})['data']['token']
  11. A=login('vsuserA','Vs1118pass');B=login('vsuserB','Vs1118pass');C=login('vsuserC','Vs1118pass')
  12. D1='2123022706158374913'
  13. # canManage flags on 4 list types for B
  14. for path,key,code_key in [('/ai/skill/list?pageSize=100','vs_skill','name'),('/ai/pack/list?pageSize=100','vs_pack','packCode'),('/ai/kb/list?pageSize=100','vs_kb','kbCode')]:
  15. r=call('GET',path,t=B); rows=r['data']['rows']
  16. by={x[code_key]:x for x in rows}
  17. dept=[k for k in by if k.endswith('_dept')][0]; pub=[k for k in by if k.endswith('_pub')][0]
  18. print(path,'B canManage dept',by[dept]['canManage'],'pub',by[pub]['canManage'])
  19. # update authorization pack/kb by B
  20. r=call('PUT','/ai/pack',{'id':'2123022767378436098','packCode':'vs_pack_dept','packName':'VS部门场景包','visibleScope':'1','ownerDeptId':D1},B)
  21. print('B update pack ->',r.get('code'),r.get('message'))
  22. r=call('PUT','/ai/kb',{'id':'2123022767873363969','kbName':'VS部门知识库','kbCode':'vs_kb_dept','embeddingModelId':1761400000000020401,'visibleScope':'1','ownerDeptId':D1},B)
  23. print('B update kb ->',r.get('code'),r.get('message'))
  24. # delete authorization pack/kb by C (create temp records first)
  25. r=call('POST','/ai/pack',{'packCode':'vs_pack_del','packName':'VS删除包','status':'0','visibleScope':'2'},A)
  26. r=call('GET','/ai/pack/list?pageSize=100',t=A); row=[x for x in r['data']['rows'] if x['packCode']=='vs_pack_del']; pid=row[0]['id']
  27. print('C delete pack ->',call('DELETE','/ai/pack/'+pid,t=C))
  28. print('B delete pack ->',call('DELETE','/ai/pack/'+pid,t=B))
  29. print('A delete pack ->',call('DELETE','/ai/pack/'+pid,t=A))
  30. r=call('POST','/ai/kb',{'kbName':'VS删除库','kbCode':'vs_kb_del','embeddingModelId':1761400000000020401,'retrievalMode':'vector','status':'0','visibleScope':'2'},A)
  31. r=call('GET','/ai/kb/list?pageSize=100',t=A); row=[x for x in r['data']['rows'] if x['kbCode']=='vs_kb_del']; kid=row[0]['id']
  32. print('C delete kb ->',call('DELETE','/ai/kb/'+kid,t=C).get('code'))
  33. print('A delete kb ->',call('DELETE','/ai/kb/'+kid,t=A).get('code'))