apppublic/appPublic/oauth_client.py

189 lines
4.5 KiB
Python
Raw Normal View History

2024-02-29 14:15:25 +08:00
import json
2024-06-26 11:30:41 +08:00
from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM, HttpError
2024-02-29 14:15:25 +08:00
from appPublic.argsConvert import ArgsConvert
2024-06-26 11:30:41 +08:00
from appPublic.dictObject import DictObject
2024-02-29 14:15:25 +08:00
class OAuthClient:
2024-04-21 06:27:07 +08:00
"""
OauthClient is a http(s) client for request a api annouce by other
it send https request and get back a json data
we can check the response json data to check if the call is success or failed
desc has the following format
{
"data": predefined data, and if nessory, you can add to new data to it
"method_name":{
"url"
"method",
"headers",
"params":arguments will appended to url with ?a=a&b=b...
"data":data will send in the request body, json format
"resp":[
{
"name":
"converter":default none, if not, convert response data first before set the returen data
"keys":resposne json data keys to achieve data
}
]
"error_if":{
"keys":
"op",
"value"
}
}
}
return:
if error:
return {
"status":"error",
"code":code returned by server
"message":"message returned by server
}
else:
return {
"status":"ok",
"data":...
}
"""
def __init__(self, desc, converters={}):
2024-02-29 14:15:25 +08:00
assert desc.get('data')
self.desc = desc
self.data = desc.get('data')
2024-04-21 06:27:07 +08:00
self.converters = converters
2024-02-29 14:15:25 +08:00
self.ac = ArgsConvert('${', '}')
2024-06-26 11:30:41 +08:00
def setup_req_data(self, data=[], ns={}):
2024-04-21 06:27:07 +08:00
d = {}
2024-06-26 11:30:41 +08:00
if data is None:
return None
for h in data:
d1 = self.setup_req_kv(h, ns)
2024-04-21 06:27:07 +08:00
d.update(d1)
if d == {}:
return None
return d
2024-06-26 11:30:41 +08:00
def setup_req_kv(self, d, ns):
2024-04-21 06:27:07 +08:00
rd = {
d.name:d.value
}
nd = self.datalize(rd, ns)
if d.converter:
f = self.converters.get(d.converter)
if f:
nd[d.name] = f(nd.get(d.name))
return nd
2024-04-06 13:43:28 +08:00
2024-06-26 11:30:41 +08:00
async def __call__(self, host, mapi, params):
2024-04-21 06:27:07 +08:00
if not self.desc.get(mapi):
2024-02-29 14:15:25 +08:00
raise Exception(f'{mapi} not defined')
2024-04-21 06:27:07 +08:00
self.api = self.desc[mapi]
if not self.api:
return {
"status":"error",
"code":'9999',
"message":f"{mapi} not defined"
}
2024-06-26 11:30:41 +08:00
path = self.datalize(self.api.path, params)
url = host + path
method = self.api.get('method', 'GET')
2024-04-21 06:27:07 +08:00
myheaders = self.setup_req_data(self.api.headers, params)
myparams = self.setup_req_data(self.api.params, params)
mydata = self.setup_req_data(self.api.data, params)
response_type = RESPONSE_JSON
2024-02-29 14:15:25 +08:00
hc = HttpClient()
2024-04-21 06:27:07 +08:00
print(f'{url=}, {method=}, {myparams=}, {mydata=}, {myheaders=}')
2024-06-26 11:30:41 +08:00
resp_data = None
try:
resp_data = await hc.request(url, method, response_type=response_type,
2024-04-21 06:27:07 +08:00
params=None if myparams is None else myparams,
data=None if mydata is None else json.dumps(mydata),
headers=myheaders)
2024-06-26 11:30:41 +08:00
resp_data = DictObject(**resp_data)
print(resp_data)
except HttpError as e:
return {
"status":"error",
"code":e.code,
"message":e.msg
}
2024-02-29 18:49:03 +08:00
if resp_data is None:
2024-04-21 06:27:07 +08:00
return {
"status":"error",
2024-06-26 11:30:41 +08:00
"code":None,
2024-04-21 06:27:07 +08:00
"message":"https error"
}
err = self.check_if_error(resp_data)
2024-02-29 18:49:03 +08:00
if err:
2024-04-21 06:27:07 +08:00
return err
return self.setup_return_data(resp_data)
2024-02-29 14:15:25 +08:00
def datalize(self, dic, data={}):
mydata = self.data.copy()
mydata.update(data)
s1 = self.ac.convert(dic, mydata)
return s1
2024-04-21 06:27:07 +08:00
def get_resp_data(self, resp, keys, converter=None):
2024-06-26 11:30:41 +08:00
d = resp.get_data_by_keys(keys)
2024-04-21 06:27:07 +08:00
if converter:
f = self.converters.get(converter)
if f:
d = f(d)
return d
def setup_return_data(self, resp):
data = {}
2024-06-26 11:30:41 +08:00
if not self.api.resp:
return {
'status':'ok',
'data':{}
}
2024-04-21 06:27:07 +08:00
for desc in self.api.resp:
k = desc.name
2024-06-26 11:30:41 +08:00
v = self.get_resp_data(resp, desc.resp_keys, desc.converter)
2024-04-21 06:27:07 +08:00
data[k] = v
return {
"status":"ok",
"data":data
}
2024-06-26 11:30:41 +08:00
def check_if_error(self, resp):
2024-04-21 06:27:07 +08:00
if not self.api.error_if:
return None
ei = self.api.error_if
2024-06-26 11:30:41 +08:00
v = resp.get_data_by_keys(ei.error_keys)
2024-04-21 06:27:07 +08:00
v1 = ei.value
if ei.converter:
f = self.converters.get(ei.converter)
if f:
v = f(v)
if not ei.op:
ei.op = '=='
2024-06-26 11:30:41 +08:00
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}')
2024-04-21 06:27:07 +08:00
code = None
message = None
if ei.code_keys:
2024-06-26 11:30:41 +08:00
code = resp.get_data_by_keys(ei.code_keys)
2024-04-21 06:27:07 +08:00
if ei.msg_keys:
2024-06-26 11:30:41 +08:00
message = resp.get_data_by_keys(ei.msg_keys)
2024-04-21 06:27:07 +08:00
return {
"status":"error",
2024-06-26 11:30:41 +08:00
"code":code,
2024-04-21 06:27:07 +08:00
"message":message
}
2024-06-26 11:30:41 +08:00
print(f'check_if_error ok:{v=}, {v1=}, {ei.op=}{v==v1}')
2024-04-21 06:27:07 +08:00
return None
2024-02-29 14:15:25 +08:00
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]