原理
封装
import requests
class BaseApi:
def request_http(self,req):
# 直接用关键字传参方式,把请求提传给requests.request方法,**解码字典
r = requests.request(**req)
#print(r.json())
return r
调用
import requests
from api.base_api import BaseApi
class GetToken(BaseApi):
_corpid = "xxxx"
_corp_secret = "yyyy"
def get_token(self):
#把请求信息转换成一个规范的字典体
req = {
"method":"get",
"url":"https://qqyapi.weixin.qq.com/cgi_bin/gettoken",
"params":
{
"corpid": self._corpid,
"corp_secret": self._corp_secret
}
}
r = self.request_http(req)
#r = requests.request(**req)
#等同于下面这个
#r = requests.request(method="get",url="https://qqyapi.weixin.qq.com/cgi_bin/gettoken",params={"corpid":self._corpid,"corp_secret": self._corp_secret})
print(r.json)
return r
def add_token(self):
pass
if __name__ == '__main__':
ba = GetToken()
ba.get_token()
这样可以做到分离一部分代码