统一请求封装的目的:
1.去除重复的冗余的代码
2. 跨py文件实现通过一个sess来自动关联有cookie关联的接口。
3. 设置统一的公共参数,统一的文件处理,统一的异常处理,统一的日志监控,统一的用例校验等
封装前原本代码:
py文件一:
class TestShop:session = requests.session()def test_start_list(self):method = 'post'url = 'http://101.34.221.219:8010/api.php'params = {'application':'app','application_client_type':'h5','s':'index/index'}res = TestShop.session.request(method=method,url=url,params=params)print(res.json())def test_product_detail(self):method = 'post'url = 'http://101.34.221.219:8010/api.php'params = {'application':'app','application_client_type':'h5','s':'goods/detail'}json = {"goods_id": "12"}res = TestShop.session.request(method=method,url=url,params=params,json=json)print(res.json())
py文件二:
class TestApi:access_token = ''session = requests.session() def test_phpwind(self):url = 'http://47.107.116.139/phpwind/'res = TestApi.session.request(method="get",url=url)res_token = re.search('name="access_token" value="(.*?)"',res.text)TestApi.access_token = access_token.group(1)print(TestApi.access_token)def test_file_upload(self):url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg'params = {'access_token':TestApi.access_token}# 封装前文件上传file = {'media':open('D:\\1.jpg','rb')}res = RequestUtil().send_request(method="post", url=url, params=params, files=file)print(res.json())
从这两个代码可以看出,两个py文件都创建了session回话,且在第一个py文件中有params的冗余部分,则可以将请求统一封装到一个session进行调用,去除的冗余的同时,若params内容需要修改也只用修改一个即可,并且将文件上传进行封装后代码中只需要上传文件路径即可
虽然在公共参数中的public_params只作用于一个py文件,但是对于另外一个文件虽然多余了但是不会产生影响
统一封装请求:
class RequestUtil:session = requests.session()def send_request(self,**kwargs):# 公共参数public_params = {'application': 'app','application_client_type': 'h5'}for key,value in kwargs.items():if key == "params":kwargs['params'].update(public_params)if key =='files':# 此时请求接口中的file的值为value.items(),可能会有多个文件,所以是一个列表for file_key,file_value in value.items():# 文件上传中的key则为value[file_key],value为file_valuevalue[file_key] = open(file_value,'rb')# 公共请求res = RequestUtil.session.request(**kwargs)return res
修改后代码一:
class TestShop:token = ''#首页列表接口def test_start_list(self):method = 'post'url = 'http://101.34.221.219:8010/api.php'params = {'s':'index/index'}res = RequestUtil().send_request(method=method,url=url,params=params)print(res.json())# 商品列表接口def test_product_detail(self):method = 'post'url = 'http://101.34.221.219:8010/api.php'params = {'s':'goods/detail'}json = {"goods_id": "12"}res = RequestUtil().send_request(method=method,url=url,params=params,json=json)print(res.json())
修改后代码二:
class TestApi:access_token = ''def test_phpwind(self):url = 'http://47.107.116.139/phpwind/'res = RequestUtil().send_request(method="get",url=url)# print(res.text)# 因为token是在文本中,通过正则表达式的方式获取tokenaccess_token = re.search('name="access_token" value="(.*?)"',res.text)TestApi.access_token = res_token.group(1)print(TestApi.csrf_token)def test_file_upload(self):url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg'params = {'access_token':TestApi.access_token}# 封装后文件上传,只需要写入路径file = {'media':'D:\\1.jpg'}res = RequestUtil().send_request(method="post", url=url, params=params, files=file)print(res.json())
文件上传封装: