Merge branch 'main' of git.opencomputing.cn:yumoqing/uapi

This commit is contained in:
yumoqing 2025-08-06 14:36:46 +08:00
commit bbe8ac33fb

View File

@ -67,10 +67,11 @@ class UAPI:
raise e raise e
return None, None return None, None
apiset = apisets[0] apiset = apisets[0]
recs = await sor.R('uapi', {'name':apiname, 'apisetid': upapp.apisetid}) r recs = await sor.R('uapi', {'name':apiname, 'apisetid': upapp.apisetid})
if len(recs)==0: if len(recs)==0:
return None, None return None, None
uapi = recs[0] 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 auth_uapi = None
@ -81,6 +82,7 @@ class UAPI:
exception(f'{e=}\n{format_exc()}') exception(f'{e=}\n{format_exc()}')
raise e raise e
auth_uapi = uapi[0] auth_uapi = uapi[0]
self.auth_uapi = auth_uapi
return auth_uapi, uapi return auth_uapi, uapi
return None, None return None, None
@ -95,10 +97,15 @@ class UAPI:
await self.do_auth(auth_uapi) await self.do_auth(auth_uapi)
async for chunk in self.stream_resp(uapi): async for chunk in self.stream_resp(uapi):
yield chunk yield chunk
t
async stream_linify(self, upappid, apiname, callerid, params={}):
gen = liner(self.__call__(upappid, apiname, callerid, params=params))
async for line in gen:
yield line
async def request(self, upappid, apiname, callerid, params={}): async def request(self, upappid, apiname, callerid, params={}):
b = b'' b = b''
async for chunk in self(upapiid, apiname, callerid, params=params): async for chunk in self__call__(upapiid, apiname, callerid, params=params):
b += chunk b += chunk
return b return b
@ -110,7 +117,7 @@ class UAPI:
self.env.update(d) self.env.update(d)
return return
async def stream_resp(self, api, params={}): async def stream_resp(self, api):
url = self.env.get('baseurl') + api.path url = self.env.get('baseurl') + api.path
method = api.httpmethod method = api.httpmethod
headers = await self.rendertmpl(api.headers) headers = await self.rendertmpl(api.headers)
@ -132,53 +139,6 @@ class UAPI:
async for chunk in gen: async for chunk in gen:
yield chunk yield chunk
async def stream_gen(self, api, params={}):
url = self.env.get('baseurl') + api.path
method = api.httpmethod
headers = await self.rendertmpl(api.headers)
headers = json.loads(headers)
body = await self.rendertmpl(api.data)
if body:
bdy = json.loads(body)
bdy['stream'] = True
body = json.dumps(bdy, ensure_ascii=False)
_params = await self.rendertmpl(api.params)
if _params:
_params = json.loads(_params)
debug(f'{headers=}, {body=}. {method=}, {url=}')
shc = StreamHttpClient()
gen = shc(method, url,
headers=headers,
data=body,
params=_params)
chunk_match = api.chunk_match or ''
cmlen = len(chunk_match)
async for line in liner(gen):
line = line.decode('utf-8')
if line.startswith(chunk_match):
line = line[cmlen:]
cvt_line = await self.streamline_handle(line,
api.streamresponse)
if cvt_line is not None:
yield cvt_line
else:
debug(f'{line=} after convert is None')
else:
debug(f'{chunk_match=},{line=} not matched')
async def streamline_handle(self, line, resptmpl):
try:
dic = json.loads(line)
if resptmpl:
jstr = await self.rendertmpl(resptmpl, params=dic)
jstr += '\n'
else:
jstr = json.dumps(dic, ensure_ascii=False) + '\n'
return jstr
except Exception as e:
exception(f'{line=}\n{e=},{format_exc()}')
return None
async def get_userapikey(self, sor, upappid, callerid): async def get_userapikey(self, sor, upappid, callerid):
""" """
argumemts: argumemts: