This commit is contained in:
yumoqing 2025-08-08 15:49:19 +08:00
parent 472f0d3562
commit 76cbd6dd5b

View File

@ -9,6 +9,45 @@ from appPublic.myTE import MyTemplateEngine
from appPublic.log import debug, exception, error from appPublic.log import debug, exception, error
from ahserver.globalEnv import password_decode from ahserver.globalEnv import password_decode
from ahserver.serverenv import get_serverenv from ahserver.serverenv import get_serverenv
from random import randint
async def get_callerid(orgid):
dbname = get_dbname()
db = DBPools()
async with db.sqlorContext(dbname) as sor:
return await sor_get_callerid(sor, orgid
return None
async def sor_get_callerid(sor, orgid):
sql = """select a.ownerid from upappkey a, users b
where b.orgid = ${orgid}$
and a.ownerid = b.id"""
recs = await sor.sqlExe(sql, {'orgid': orgid})
cnt = len(recs)
if cnt == 0:
return None
i = randint(0, cnt - 1)
return recs[i].ownerid
async def get_uapi(upappid, apiname):
dbname = get_dbname()
db = DBPools()
async with db.sqlorContext(dbname) as sor:
return await sor.get_uapi(sor, upappid, apiname)
return None
async def sor_get_uapi(sor, upappid, apiname):
sql = """select a.*,
c.auth_apiname
from uapi a, upapp b, uapiset c
where a.apisetid = b.apisetid
and b.apisetid = c.id
and a.name = ${apiname}$
and b.id = ${uappid}$"""
recs = await sor.sqlExe(sql, {'upappid': upappid, 'apiname': apiname})
if len(recs) == 0:
return None
return recs[0]
def get_dbname(): def get_dbname():
f = get_serverenv('get_module_dbname') f = get_serverenv('get_module_dbname')
@ -50,47 +89,15 @@ class UAPI:
async def get_uapis(self, sor, upappid, apiname, callerid, params={}): async def get_uapis(self, sor, upappid, apiname, callerid, params={}):
self.env.update(params) self.env.update(params)
db = DBPools()
uapi = None uapi = None
auth_uapi = None auth_uapi = None
if self.sor is None: uapi = await sor_get_uapi(sor, upappid, apiname)
dbname = get_dbname() if uapi.auth_apiname:
self.sor = db.sqlorContext(dbname) auth_uapi = await sor_get_uapi(sor, upappid, iuapi.auth_apiname)
async with self.sor as sor:
upapps = await sor.R('upapp', {'id': upappid})
if len(upapps) == 0:
e = Exceptions(f'{upappid=}, {apiname=}, {callerid=} upapp not found')
exception(f'{e=}\n{format_exc()}')
raise e
upapp = upapps[0]
apisets = await sor.R('uapiset', {'id': upapp.apisetid})
if len(apisets) == 0:
e = Exceptions(f'{upappid=}, {apiname=}, {callerid=} apiset not found')
exception(f'{e=}\n{format_exc()}')
raise e
return None, None
apiset = apisets[0]
recs = await sor.R('uapi', {
'name':apiname,
'apisetid': upapp.apisetid
})
if len(recs)==0:
return None, None
uapi = recs[0]
self.uapi = uapi
kinfo = await self.get_userapikey(sor, upappid, callerid) kinfo = await self.get_userapikey(sor, upappid, callerid)
self.env.update(kinfo) self.env.update(kinfo)
auth_uapi = None
if apiset.auth_apiname:
uapis = await sor.R('uapi', {'name': apiset.auth_apiname, 'apisetid': upapp.apisetid})
if len(uapis) == 0:
e = Exceptions(f'{upappid=}, {uapiid=}, {callerid=} {apiset.auth_apinamed=} auth_api not found')
exception(f'{e=}\n{format_exc()}')
raise e
auth_uapi = uapi[0]
self.auth_uapi = auth_uapi
return auth_uapi, uapi return auth_uapi, uapi
return None, None
async def __call__(self, upappid, apiname, callerid, params={}): async def __call__(self, upappid, apiname, callerid, params={}):
""" """
@ -117,12 +124,12 @@ class UAPI:
async def stream_linify(self, upappid, apiname, callerid, params={}): async def stream_linify(self, upappid, apiname, callerid, params={}):
gen = liner(self.__call__(upappid, apiname, callerid, params=params)) gen = liner(self.__call__(upappid, apiname, callerid, params=params))
async for line in gen: async for line in gen:
filter = self.api.chunk_match filter = self.uapi.chunk_match
if line.startswith(filter): if line.startswith(filter):
line = line[len(filter):] line = line[len(filter):]
if self.api.streamresponse: if self.uapi.streamresponse:
dic = json.loads(line) dic = json.loads(line)
line = self.rendertmpl(self.api.streamresponse, dic) line = self.rendertmpl(self.uapi.streamresponse, dic)
yield line + '\n' yield line + '\n'
else: else:
debug(f'invalid line:{line}') debug(f'invalid line:{line}')