From 76cbd6dd5b086d5eeaaf9cc80390827532ea1e2d Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 8 Aug 2025 15:49:19 +0800 Subject: [PATCH] bugfix --- uapi/appapi.py | 91 +++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/uapi/appapi.py b/uapi/appapi.py index 9cfd0b8..87b2e92 100644 --- a/uapi/appapi.py +++ b/uapi/appapi.py @@ -9,6 +9,45 @@ from appPublic.myTE import MyTemplateEngine from appPublic.log import debug, exception, error from ahserver.globalEnv import password_decode 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(): f = get_serverenv('get_module_dbname') @@ -50,47 +89,15 @@ class UAPI: async def get_uapis(self, sor, upappid, apiname, callerid, params={}): self.env.update(params) - db = DBPools() uapi = None auth_uapi = None - if self.sor is None: - dbname = get_dbname() - self.sor = db.sqlorContext(dbname) - 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) - 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 None, None + uapi = await sor_get_uapi(sor, upappid, apiname) + if uapi.auth_apiname: + auth_uapi = await sor_get_uapi(sor, upappid, iuapi.auth_apiname) + + kinfo = await self.get_userapikey(sor, upappid, callerid) + self.env.update(kinfo) + return auth_uapi, uapi async def __call__(self, upappid, apiname, callerid, params={}): """ @@ -117,12 +124,12 @@ class UAPI: async def stream_linify(self, upappid, apiname, callerid, params={}): gen = liner(self.__call__(upappid, apiname, callerid, params=params)) async for line in gen: - filter = self.api.chunk_match + filter = self.uapi.chunk_match if line.startswith(filter): line = line[len(filter):] - if self.api.streamresponse: + if self.uapi.streamresponse: dic = json.loads(line) - line = self.rendertmpl(self.api.streamresponse, dic) + line = self.rendertmpl(self.uapi.streamresponse, dic) yield line + '\n' else: debug(f'invalid line:{line}')