为什么要做二次封装request请求接口?接口请求包含很多种类型:GET、POST、PUT、DELETE等… 数据已经成功获取到了,接下来就可以封装request请求来请求接口。
def __init__(self):
self.session = requests.Session()
self.host = con.host
使用request请求 依赖包:requests == 2.24.0 安装依赖包:pip3 install request == 2.24.0
def send_request(self,method, url, headers=None, data=None, **kwargs):
log.info("请求方式:【%s】,请求host:【%s】,请求地址:【%s】,请求Headers:【%s】,请求参数Body:【%s】"
% (method, self.host, url, headers, data))
method = method.upper()
if 'POST' == method:
response = self.session.request(method=method, url=self.host+url,headers=headers, data=json.dumps(data), stream=True, verify=False,**kwargs)
log.info(response.json())
elif 'GET' == method:
response = self.session.request(method=method, url=self.host+url, headers=headers, stream=True, verify=False, **kwargs)
log.info(response.json())
elif 'PUT' == method:
response = self.session.request(method=method, url=self.host+url,headers=headers, data=json.dumps(data), stream=True, verify=False,**kwargs)
log.info(response.json())
elif 'DELETE' == method:
response = self.session.request(method=method, url=self.host+url,headers=headers,stream=True, verify=False,**kwargs)
log.info(response.json())
else:
raise ValueError('request method "{}" error ! please check'.format(method))
return response
|