bugfix
This commit is contained in:
parent
07f0074e71
commit
d0b60e772f
@ -159,3 +159,37 @@ def dictObjectFactory(_klassName__,**kwargs):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("dictObjectFactory()",e,_klassName__)
|
print("dictObjectFactory()",e,_klassName__)
|
||||||
raise e
|
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
|
||||||
|
@ -6,6 +6,7 @@ RESPONSE_BIN = 0
|
|||||||
RESPONSE_TEXT = 1
|
RESPONSE_TEXT = 1
|
||||||
RESPONSE_JSON = 2
|
RESPONSE_JSON = 2
|
||||||
RESPONSE_FILE = 3
|
RESPONSE_FILE = 3
|
||||||
|
RESPONSE_STREAM = 4
|
||||||
|
|
||||||
class HttpClient:
|
class HttpClient:
|
||||||
def __init__(self,coding='utf-8'):
|
def __init__(self,coding='utf-8'):
|
||||||
@ -37,7 +38,7 @@ class HttpClient:
|
|||||||
self.session = aiohttp.ClientSession(cookie_jar=jar)
|
self.session = aiohttp.ClientSession(cookie_jar=jar)
|
||||||
return self.session
|
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:
|
if resp.cookies is not None:
|
||||||
self.setCookie(url,resp.cookies)
|
self.setCookie(url,resp.cookies)
|
||||||
|
|
||||||
@ -45,21 +46,24 @@ class HttpClient:
|
|||||||
return await resp.read()
|
return await resp.read()
|
||||||
if resp_type == RESPONSE_JSON:
|
if resp_type == RESPONSE_JSON:
|
||||||
return await resp.json()
|
return await resp.json()
|
||||||
# default resp_type == RESPONSE_TEXT:
|
if resp_type == RESPONSE_TEXT:
|
||||||
return await resp.text(self.coding)
|
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):
|
def grapCookie(self,url):
|
||||||
session = self.getsession(url)
|
session = self.getsession(url)
|
||||||
domain = self.url2domain(url)
|
domain = self.url2domain(url)
|
||||||
filtered = session.cookie_jar.filter_cookies(domain)
|
filtered = session.cookie_jar.filter_cookies(domain)
|
||||||
return filtered
|
return filtered
|
||||||
print(f'=====domain={domain},cookies={fltered},type={type(fltered)}===')
|
|
||||||
|
|
||||||
async def request(self, url, method,
|
async def request(self, url, method,
|
||||||
response_type=RESPONSE_TEXT,
|
response_type=RESPONSE_TEXT,
|
||||||
params=None,
|
params=None,
|
||||||
data=None,
|
data=None,
|
||||||
jd=None,
|
jd=None,
|
||||||
|
stream_func=None,
|
||||||
headers=None,
|
headers=None,
|
||||||
**kw):
|
**kw):
|
||||||
session = self.getsession(url)
|
session = self.getsession(url)
|
||||||
@ -78,7 +82,7 @@ class HttpClient:
|
|||||||
json=jd,
|
json=jd,
|
||||||
headers=headers, **kw)
|
headers=headers, **kw)
|
||||||
if resp.status==200:
|
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=})')
|
raise Exception(f'http error({resp.status}, {url=},{params=}, {data=}, {jd=}, {headers=}, {kw=})')
|
||||||
|
|
||||||
async def get(self,url,**kw):
|
async def get(self,url,**kw):
|
||||||
|
@ -1,18 +1,26 @@
|
|||||||
import json
|
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
|
from appPublic.argsConvert import ArgsConvert
|
||||||
|
|
||||||
class OAuthClient:
|
class OAuthClient:
|
||||||
def __init__(self, desc):
|
def __init__(self, desc, chunk_handle=None):
|
||||||
assert desc.get('data')
|
assert desc.get('data')
|
||||||
assert desc.get('mapis')
|
assert desc.get('mapis')
|
||||||
self.desc = desc
|
self.desc = desc
|
||||||
self.data = desc.get('data')
|
self.data = desc.get('data')
|
||||||
self.mapis = desc.get('mapis')
|
self.mapis = desc.get('mapis')
|
||||||
self.mkeys = self.mapis.keys()
|
self.mkeys = self.mapis.keys()
|
||||||
|
self._chunk_handle = chunk_handle
|
||||||
self.ac = ArgsConvert('${', '}')
|
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):
|
async def __call__(self, mapi, params):
|
||||||
|
self.chunk_data = ''
|
||||||
if mapi not in self.mkeys:
|
if mapi not in self.mkeys:
|
||||||
raise Exception(f'{mapi} not defined')
|
raise Exception(f'{mapi} not defined')
|
||||||
d = self.mapis[mapi]
|
d = self.mapis[mapi]
|
||||||
@ -31,14 +39,16 @@ class OAuthClient:
|
|||||||
_params = self.datalize(myparams, params)
|
_params = self.datalize(myparams, params)
|
||||||
_headers = self.datalize(headers, params)
|
_headers = self.datalize(headers, params)
|
||||||
_data = self.datalize(mydata, params)
|
_data = self.datalize(mydata, params)
|
||||||
response_type = d.get('response_type', RESPONSE_JSON)
|
response_type = RESPONSE_STREAM
|
||||||
hc = HttpClient()
|
hc = HttpClient()
|
||||||
print(f'{url=}, {method=}, {_params=}, {_data=}, {_headers=}')
|
print(f'{url=}, {method=}, {_params=}, {_data=}, {_headers=}')
|
||||||
resp_data = await hc.request(url, method, response_type=response_type,
|
resp_data = await hc.request(url, method, response_type=response_type,
|
||||||
params=_params,
|
params=_params,
|
||||||
data=None if _data == {} else json.dumps(_data),
|
data=None if _data == {} else json.dumps(_data),
|
||||||
|
stream_func=self.stream_handle,
|
||||||
headers=_headers)
|
headers=_headers)
|
||||||
print(f'{resp_data=}')
|
if not self._chunk_handle:
|
||||||
|
resp_data = self.chunk_data
|
||||||
if resp_data is None:
|
if resp_data is None:
|
||||||
raise Exception(f'request error:{url=}\n{method=}\n{response_type=}\n{_params=}\n{_headers=}')
|
raise Exception(f'request error:{url=}\n{method=}\n{response_type=}\n{_params=}\n{_headers=}')
|
||||||
errfield = d.get('error_field')
|
errfield = d.get('error_field')
|
||||||
|
Loading…
Reference in New Issue
Block a user