接口请求格式为:
- 固定url
- key:requestJson
value:{"head ":{固定参数及值},"body":{需要的参数和值}}
例子:
{
"head":{
"service":"front.a.query",
"version":"1.0",
"input_charset":"UTF-8",
"core_merchant_no":"EW_N2012672000",
"sign":"dc1337a0c5b6c703a14005a1d30eab8c"
},
"body":{
"channel_type":"WX",
"out_request_no":"otn20201106173104635106",
"merchant_no":"EW_N4061492000"
}
}
import requests
from a.utils.md5_python import md5Python as md5
from a.utils.date_random import get_random_param as get_unique
from a.log.logger import Logger
from a.utils.tools import dict_process
from a.utils.get_excle_path import get_excle_path
from a.utils.get_excle_data import get_all_excle_data
from a.utils.tools import del_null
import json
def front_a_query(excle_file,sheet_name,row):
# 获取接口的数据
excle_data = get_all_excle_data(excle_file, sheet_name, row)
host = excle_data['host']
params = excle_data['params']
outTradeNo = get_unique('otn')
head = {
"service": "front.a.query",
"version": "1.0",
"input_charset": "UTF-8",
"core_merchant_no": params['core_merchant_no']
}
body = {
"channel_type": "WX",
"out_request_no": outTradeNo,
"merchant_no": params['merchant_no']
}
# 非必填的参数没有值,请求信息就去掉
body = del_null(body)
requestJson = {
"head": head,
"body": body
}
# 加密串的值
sign_values = dict_process(requestJson)
# 将按规则生成的sign放在请求的head里面
requestJson["head"]['sign'] = md5.md5_sign_pc(sign_values, params['key'])
# 请求信息为json格式
params = {
"requestJson": json.dumps(requestJson)
}
# 接口请求
r = requests.post(host, data=params)
# 返回接口的请求和返回信息
result = {
'req': requestJson,
'res': r.json(),
'url': host
}
return result
if __name__ == '__main__':
log = Logger("info")
excle_file = get_excle_path()
sheet_name = 'front'
row = 1
result = front_a_query(excle_file,sheet_name,row)
log.info("请求地址:" + result['url'])
log.info("请求信息:" + str(json.dumps(result['req'], ensure_ascii=False)))
log.info("返回信息:" + str(json.dumps(result['res'], ensure_ascii=False)))
print("商户配置信息:" + str(result['res']['body']['wx_configure_info']))