From d0b60e772f5a7e7f4bd34c13ec6ef8278bacfb7c Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sat, 6 Apr 2024 13:43:28 +0800 Subject: [PATCH] bugfix --- appPublic/dictObject.py | 34 ++++++++++++++++++++++++++++++++++ appPublic/httpclient.py | 14 +++++++++----- appPublic/oauth_client.py | 18 ++++++++++++++---- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/appPublic/dictObject.py b/appPublic/dictObject.py index cd7e798..27a016f 100755 --- a/appPublic/dictObject.py +++ b/appPublic/dictObject.py @@ -159,3 +159,37 @@ def dictObjectFactory(_klassName__,**kwargs): except Exception as e: print("dictObjectFactory()",e,_klassName__) raise e + +class DotDict(dict): + def __getattr__(self, attr): + """ + 实现点操作符访问字典中的键值对 + """ + try: + return self.__DOitem(self[attr]) + except KeyError: + return None + + def __setattr__(self, attr, value): + """ + 实现点操作符设置字典中的键值对 + """ + self[attr] = value + + def __DOArray(self,a): + b = [ self.__DOitem(i) for i in a ] + return b + + def __DOitem(self, i): + if isinstance(i,DotDict): + return i + if isinstance(i,dict): + i = {k:v for k,v in i.items() if isinstance(k,str)} + try: + d = DotDict(**i) + return d + except Exception as e: + raise e + if type(i) == type([]) or type(i) == type(()) : + return self.__DOArray(i) + return i diff --git a/appPublic/httpclient.py b/appPublic/httpclient.py index 0cbea4a..1930548 100755 --- a/appPublic/httpclient.py +++ b/appPublic/httpclient.py @@ -6,6 +6,7 @@ RESPONSE_BIN = 0 RESPONSE_TEXT = 1 RESPONSE_JSON = 2 RESPONSE_FILE = 3 +RESPONSE_STREAM = 4 class HttpClient: def __init__(self,coding='utf-8'): @@ -37,7 +38,7 @@ class HttpClient: self.session = aiohttp.ClientSession(cookie_jar=jar) return self.session - async def handleResp(self,url,resp,resp_type): + async def handleResp(self,url,resp,resp_type, stream_func=None): if resp.cookies is not None: self.setCookie(url,resp.cookies) @@ -45,21 +46,24 @@ class HttpClient: return await resp.read() if resp_type == RESPONSE_JSON: return await resp.json() - # default resp_type == RESPONSE_TEXT: - return await resp.text(self.coding) + if resp_type == RESPONSE_TEXT: + return await resp.text(self.coding) + async for chunk in resp.content.iter_chunked(1024): + if stream_func: + stream_func(chunk) def grapCookie(self,url): session = self.getsession(url) domain = self.url2domain(url) filtered = session.cookie_jar.filter_cookies(domain) return filtered - print(f'=====domain={domain},cookies={fltered},type={type(fltered)}===') async def request(self, url, method, response_type=RESPONSE_TEXT, params=None, data=None, jd=None, + stream_func=None, headers=None, **kw): session = self.getsession(url) @@ -78,7 +82,7 @@ class HttpClient: json=jd, headers=headers, **kw) if resp.status==200: - return await self.handleResp(url, resp, response_type) + return await self.handleResp(url, resp, response_type, stream_func=stream_func) raise Exception(f'http error({resp.status}, {url=},{params=}, {data=}, {jd=}, {headers=}, {kw=})') async def get(self,url,**kw): diff --git a/appPublic/oauth_client.py b/appPublic/oauth_client.py index 23fa760..7ff5d30 100644 --- a/appPublic/oauth_client.py +++ b/appPublic/oauth_client.py @@ -1,18 +1,26 @@ import json -from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE +from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM from appPublic.argsConvert import ArgsConvert class OAuthClient: - def __init__(self, desc): + def __init__(self, desc, chunk_handle=None): assert desc.get('data') assert desc.get('mapis') self.desc = desc self.data = desc.get('data') self.mapis = desc.get('mapis') self.mkeys = self.mapis.keys() + self._chunk_handle = chunk_handle self.ac = ArgsConvert('${', '}') + def stream_handle(self, chunk): + if self._chunk_handle: + self._chunk_handle(chunk) + else: + self.chunk_data = self.chunk_data + chunk + async def __call__(self, mapi, params): + self.chunk_data = '' if mapi not in self.mkeys: raise Exception(f'{mapi} not defined') d = self.mapis[mapi] @@ -31,14 +39,16 @@ class OAuthClient: _params = self.datalize(myparams, params) _headers = self.datalize(headers, params) _data = self.datalize(mydata, params) - response_type = d.get('response_type', RESPONSE_JSON) + response_type = RESPONSE_STREAM hc = HttpClient() print(f'{url=}, {method=}, {_params=}, {_data=}, {_headers=}') resp_data = await hc.request(url, method, response_type=response_type, params=_params, data=None if _data == {} else json.dumps(_data), + stream_func=self.stream_handle, headers=_headers) - print(f'{resp_data=}') + if not self._chunk_handle: + resp_data = self.chunk_data if resp_data is None: raise Exception(f'request error:{url=}\n{method=}\n{response_type=}\n{_params=}\n{_headers=}') errfield = d.get('error_field')