This commit is contained in:
yumoqing 2024-06-26 11:30:41 +08:00
parent 5447713c3a
commit f618ebde56
3 changed files with 56 additions and 20 deletions

View File

@ -8,6 +8,17 @@ RESPONSE_JSON = 2
RESPONSE_FILE = 3 RESPONSE_FILE = 3
RESPONSE_STREAM = 4 RESPONSE_STREAM = 4
class HttpError(Exception):
def __init__(self, code, msg, *args, **kw):
super().__init__(*msg, **kw)
self.code = code
self.msg = msg
def __str__(self):
return f"Error Code:{self.code}, {self.msg}"
def __expr__(self):
return str(self)
class HttpClient: class HttpClient:
def __init__(self,coding='utf-8'): def __init__(self,coding='utf-8'):
self.coding = coding self.coding = coding
@ -83,7 +94,8 @@ class HttpClient:
headers=headers, **kw) headers=headers, **kw)
if resp.status==200: if resp.status==200:
return await self.handleResp(url, resp, response_type, stream_func=stream_func) 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=})') msg = f'http error({resp.status}, {url=},{params=}, {data=}, {jd=}, {headers=}, {kw=})'
raise HttpError(resp.status, msg)
async def get(self,url,**kw): async def get(self,url,**kw):
return self.request(url, 'GET', **kw) return self.request(url, 'GET', **kw)

View File

@ -1,6 +1,7 @@
import json import json
from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM, HttpError
from appPublic.argsConvert import ArgsConvert from appPublic.argsConvert import ArgsConvert
from appPublic.dictObject import DictObject
class OAuthClient: class OAuthClient:
""" """
@ -51,16 +52,18 @@ class OAuthClient:
self.converters = converters self.converters = converters
self.ac = ArgsConvert('${', '}') self.ac = ArgsConvert('${', '}')
def setup_req_datas(self, data=[], ns={}): def setup_req_data(self, data=[], ns={}):
d = {} d = {}
for h in headers: if data is None:
d1 = self.setup_req_data(h, ns) return None
for h in data:
d1 = self.setup_req_kv(h, ns)
d.update(d1) d.update(d1)
if d == {}: if d == {}:
return None return None
return d return d
def setup_req_data(d, ns): def setup_req_kv(self, d, ns):
rd = { rd = {
d.name:d.value d.name:d.value
} }
@ -71,7 +74,7 @@ class OAuthClient:
nd[d.name] = f(nd.get(d.name)) nd[d.name] = f(nd.get(d.name))
return nd return nd
async def __call__(self, mapi, params): async def __call__(self, host, mapi, params):
if not self.desc.get(mapi): if not self.desc.get(mapi):
raise Exception(f'{mapi} not defined') raise Exception(f'{mapi} not defined')
self.api = self.desc[mapi] self.api = self.desc[mapi]
@ -81,23 +84,34 @@ class OAuthClient:
"code":'9999', "code":'9999',
"message":f"{mapi} not defined" "message":f"{mapi} not defined"
} }
assert d.get('url') path = self.datalize(self.api.path, params)
method = d.get('method', 'GET') url = host + path
method = self.api.get('method', 'GET')
myheaders = self.setup_req_data(self.api.headers, params) myheaders = self.setup_req_data(self.api.headers, params)
myparams = self.setup_req_data(self.api.params, params) myparams = self.setup_req_data(self.api.params, params)
mydata = self.setup_req_data(self.api.data, params) mydata = self.setup_req_data(self.api.data, params)
url = self.api.get('url')
response_type = RESPONSE_JSON response_type = RESPONSE_JSON
hc = HttpClient() hc = HttpClient()
print(f'{url=}, {method=}, {myparams=}, {mydata=}, {myheaders=}') print(f'{url=}, {method=}, {myparams=}, {mydata=}, {myheaders=}')
resp_data = None
try:
resp_data = await hc.request(url, method, response_type=response_type, resp_data = await hc.request(url, method, response_type=response_type,
params=None if myparams is None else myparams, params=None if myparams is None else myparams,
data=None if mydata is None else json.dumps(mydata), data=None if mydata is None else json.dumps(mydata),
headers=myheaders) headers=myheaders)
resp_data = DictObject(**resp_data)
print(resp_data)
except HttpError as e:
return {
"status":"error",
"code":e.code,
"message":e.msg
}
if resp_data is None: if resp_data is None:
return { return {
"status":"error", "status":"error",
"code":None "code":None,
"message":"https error" "message":"https error"
} }
err = self.check_if_error(resp_data) err = self.check_if_error(resp_data)
@ -112,7 +126,7 @@ class OAuthClient:
return s1 return s1
def get_resp_data(self, resp, keys, converter=None): def get_resp_data(self, resp, keys, converter=None):
d = resp.get_dic_data_by_keys(desc.keys) d = resp.get_data_by_keys(keys)
if converter: if converter:
f = self.converters.get(converter) f = self.converters.get(converter)
if f: if f:
@ -121,20 +135,26 @@ class OAuthClient:
def setup_return_data(self, resp): def setup_return_data(self, resp):
data = {} data = {}
if not self.api.resp:
return {
'status':'ok',
'data':{}
}
for desc in self.api.resp: for desc in self.api.resp:
k = desc.name k = desc.name
v = self.get_resp_data(resp, desc.keys, desc.converter) v = self.get_resp_data(resp, desc.resp_keys, desc.converter)
data[k] = v data[k] = v
return { return {
"status":"ok", "status":"ok",
"data":data "data":data
} }
def check_if_error(self, resp, error_field): def check_if_error(self, resp):
if not self.api.error_if: if not self.api.error_if:
return None return None
ei = self.api.error_if ei = self.api.error_if
v = resp.get_dic_data_by_keys(ei.keys) v = resp.get_data_by_keys(ei.error_keys)
v1 = ei.value v1 = ei.value
if ei.converter: if ei.converter:
f = self.converters.get(ei.converter) f = self.converters.get(ei.converter)
@ -142,18 +162,21 @@ class OAuthClient:
v = f(v) v = f(v)
if not ei.op: if not ei.op:
ei.op = '==' ei.op = '=='
if ei.op in ['==', '='] and v == v1 or ei.op == '!=' and v != v1: print(f'{ei.keys=},{v=}, {v1=}, {ei.op=}{v==v1}, {resp.base_resp.status_code=}')
if (ei.op == '==' and v == v1) or (ei.op == '!=' and v != v1):
print(f'{v=}, {v1=}, {ei.op=}{v==v1}')
code = None code = None
message = None message = None
if ei.code_keys: if ei.code_keys:
code = resp.get_dic_data_by_keys(ei.code_keys) code = resp.get_data_by_keys(ei.code_keys)
if ei.msg_keys: if ei.msg_keys:
message = resp.get_dic_data_by_keys(ei.msg_keys) message = resp.get_data_by_keys(ei.msg_keys)
return { return {
"status":"error", "status":"error",
"code:code, "code":code,
"message":message "message":message
} }
print(f'check_if_error ok:{v=}, {v1=}, {ei.op=}{v==v1}')
return None return None
def set_data(self, resp_data, data_desc): def set_data(self, resp_data, data_desc):

View File

@ -74,6 +74,7 @@ class SSHNode:
else: else:
self.conn = await asyncssh.connect(host, self.conn = await asyncssh.connect(host,
username=username, username=username,
password=password,
port=port) port=port)
def close(self): def close(self):