2015-11-25 02:32:06 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
2016-02-04 12:41:37 +00:00
|
|
|
|
'''一个简单的通联数据客户端,主要使用requests开发,比通联官网的python例子更为简洁。'''
|
|
|
|
|
|
|
|
|
|
|
2015-11-25 02:32:06 +00:00
|
|
|
|
import requests
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
FILENAME = 'datayes.json'
|
|
|
|
|
HTTP_OK = 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class DatayesClient(object):
|
|
|
|
|
"""通联数据客户端"""
|
|
|
|
|
|
|
|
|
|
name = u'通联数据客户端'
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
self.domain = '' # 主域名
|
|
|
|
|
self.version = '' # API版本
|
|
|
|
|
self.token = '' # 授权码
|
|
|
|
|
self.header = {} # http请求头部
|
|
|
|
|
self.settingLoaded = False # 配置是否已经读取
|
|
|
|
|
|
|
|
|
|
self.loadSetting()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def loadSetting(self):
|
|
|
|
|
"""载入配置"""
|
|
|
|
|
try:
|
2016-10-28 14:55:46 +00:00
|
|
|
|
path = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
FILENAME = os.path.join(path, FILENAME)
|
2015-11-25 02:32:06 +00:00
|
|
|
|
f = file(FILENAME)
|
|
|
|
|
except IOError:
|
2017-05-02 13:46:12 +00:00
|
|
|
|
print u'%s无法打开配置文件' % self.name
|
2015-11-25 02:32:06 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
setting = json.load(f)
|
|
|
|
|
try:
|
|
|
|
|
self.domain = str(setting['domain'])
|
|
|
|
|
self.version = str(setting['version'])
|
|
|
|
|
self.token = str(setting['token'])
|
|
|
|
|
except KeyError:
|
2017-05-02 13:46:12 +00:00
|
|
|
|
print u'%s配置文件字段缺失' % self.name
|
2015-11-25 02:32:06 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.header['Connection'] = 'keep_alive'
|
|
|
|
|
self.header['Authorization'] = 'Bearer ' + self.token
|
|
|
|
|
self.settingLoaded = True
|
|
|
|
|
|
2017-05-02 13:46:12 +00:00
|
|
|
|
print u'%s配置载入完成' % self.name
|
2015-11-25 02:32:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def downloadData(self, path, params):
|
|
|
|
|
"""下载数据"""
|
|
|
|
|
if not self.settingLoaded:
|
2017-05-02 13:46:12 +00:00
|
|
|
|
print u'%s配置未载入' % self.name
|
2015-11-25 02:32:06 +00:00
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
url = '/'.join([self.domain, self.version, path])
|
|
|
|
|
r = requests.get(url=url, headers=self.header, params=params)
|
|
|
|
|
|
|
|
|
|
if r.status_code != HTTP_OK:
|
2017-05-02 13:46:12 +00:00
|
|
|
|
print u'%shttp请求失败,状态代码%s' %(self.name, r.status_code)
|
2015-11-25 02:32:06 +00:00
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
result = r.json()
|
|
|
|
|
if 'retMsg' in result and result['retMsg'] == 'Success':
|
|
|
|
|
return result['data']
|
|
|
|
|
else:
|
|
|
|
|
if 'retMsg' in result:
|
2017-05-02 13:46:12 +00:00
|
|
|
|
print u'%s查询失败,返回信息%s' %(self.name, result['retMsg'])
|
2015-11-25 02:32:06 +00:00
|
|
|
|
elif 'message' in result:
|
2017-05-02 13:46:12 +00:00
|
|
|
|
print u'%s查询失败,返回信息%s' %(self.name, result['message'])
|
2015-11-25 02:32:06 +00:00
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|