This commit is contained in:
yumoqing 2024-02-29 14:15:25 +08:00
parent c0d901ec72
commit d60ff4b2b3
2 changed files with 78 additions and 13 deletions

View File

@ -55,24 +55,29 @@ class HttpClient:
return filtered
print(f'=====domain={domain},cookies={fltered},type={type(fltered)}===')
async def get(self,url,
params={},headers={},resp_type=RESPONSE_TEXT):
async def request(self, url, method,
resp_type=RESPONSE_TEXT,
params=None,
data=None,
json=None,
headers=None,
**kw):
session = self.getsession(url)
resp = await session.get(url,params=params,headers=headers)
resp = await session.request(method, url,
params=params,
data=data,
json=json,
headers=headers, **kw)
if resp.status==200:
return await self.handleResp(url,resp,resp_type)
return await self.handleResponse(url, resp, resp_type)
return None
async def post(self,url,
data=b'',headers={},cookies=None,
resp_type=RESPONSE_TEXT):
async def get(self,url,**kw):
return self.request(url, 'GET', **kw)
async def post(self,url, **kw):
return self.request(url, 'POST', **kw)
session = self.getsession(url)
resp = await session.post(url,data=data,
headers=headers,cookies=cookies)
if resp.status==200:
return await self.handleResp(url,resp,resp_type)
print(f'**ERROR**{url} response code={resp.status}')
return None
if __name__ == '__main__':
async def gbaidu(hc):

60
appPublic/oauth_client.py Normal file
View File

@ -0,0 +1,60 @@
import json
from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE
from appPublic.argsConvert import ArgsConvert
class OAuthClient:
def __init__(self, desc):
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.ac = ArgsConvert('${', '}')
aysnc def __call__(self, mapi, params):
if mapi not in self.mkeys:
raise Exception(f'{mapi} not defined')
d = self.mapis[mapi]
assert d.get('url')
method = d.get('method', 'GET')
headers = {}
for h in d.get('headers',{}):
headers[h.get('name')] = h.get('value')
myparams = {}
for p in d.get('params', {}):
myparams[[p.get('name')] = p.get('value')
url = d.get('url')
_params = self.datalize(myparams, params)
_headers = self.datalize(headers, params)
hc = HttpClient()
resp_data = hc.request(url, method, response_type=resp_type,
params=params,
headers=headers)
respdesc= d.get('response', [])
for r in respdesc:
v = r['value']
if v.startswith('${') and v.endswith('}'):
k = v[2:-1]
self.data[k] = resp_data[r['name']]
return resp_data
def datalize(self, dic, data={}):
mydata = self.data.copy()
mydata.update(data)
s1 = self.ac.convert(dic, mydata)
return s1
def is_error(self, resp_data, error_field):
if resp_data.get(error_feield):
return True
def set_data(self, resp_data, data_desc):
for dd in data_desc:
f = dd['field']
n = dd['name']
if resp_data.get(f):
self.data[n] = resp_data[f]