diff --git a/uapi/appapi.py b/uapi/appapi.py index 3ab2132..67a597b 100644 --- a/uapi/appapi.py +++ b/uapi/appapi.py @@ -67,10 +67,11 @@ class UAPI: raise e return None, None 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: return None, None uapi = recs[0] + self.uapi = uapi kinfo = await self.get_userapikey(sor, upappid, callerid) self.env.update(kinfo) auth_uapi = None @@ -81,6 +82,7 @@ class UAPI: exception(f'{e=}\n{format_exc()}') raise e auth_uapi = uapi[0] + self.auth_uapi = auth_uapi return auth_uapi, uapi return None, None @@ -95,10 +97,15 @@ class UAPI: await self.do_auth(auth_uapi) async for chunk in self.stream_resp(uapi): 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={}): 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 return b @@ -110,7 +117,7 @@ class UAPI: self.env.update(d) return - async def stream_resp(self, api, params={}): + async def stream_resp(self, api): url = self.env.get('baseurl') + api.path method = api.httpmethod headers = await self.rendertmpl(api.headers) @@ -132,53 +139,6 @@ class UAPI: async for chunk in gen: 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): """ argumemts: