Skip to content

Commit d83579c

Browse files
committed
config file
1 parent df53ee9 commit d83579c

File tree

6 files changed

+94
-9
lines changed

6 files changed

+94
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,4 @@ cython_debug/
139139

140140
.idea
141141
.DS_Store
142+
*.properties

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ rsa
88
stomp.py
99
getmac
1010
cryptography
11-
backoff
11+
backoff
12+
jproperties

tigeropen/common/consts/service_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
55
@author: gaoan
66
"""
7+
USER_TOKEN_REFRESH = "user_token_refresh"
78

9+
# 订单
810
ORDER_NO = "order_no"
911
PREVIEW_ORDER = "preview_order"
1012
PLACE_ORDER = "place_order"

tigeropen/common/util/web_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ def do_request(method, url, query_string=None, headers=None, params=None, timeou
6969
connection.connect()
7070
except Exception as e:
7171
raise RequestException('[' + THREAD_LOCAL.uuid + ']' + method + ' connect failed. url: ' + url
72+
+ ' headers: ' + str(headers)
7273
+ ' params: ' + str(params) + ' detail: ' + str(e))
7374
try:
7475
connection.request(method, url, body=json.dumps(params), headers=headers)
7576
except Exception as e:
7677
raise RequestException('[' + THREAD_LOCAL.uuid + ']' + method + ' request failed. url: ' + url
78+
+ ' headers: ' + str(headers)
7779
+ ' params: ' + str(params) + ' detail: ' + str(e))
7880
response = connection.getresponse()
7981
result = response.read()
@@ -82,7 +84,8 @@ def do_request(method, url, query_string=None, headers=None, params=None, timeou
8284
if charset:
8385
result = result.decode(charset)
8486
raise ResponseException('[' + THREAD_LOCAL.uuid + ']invalid http status ' + str(response.status) +
85-
',detail body:' + result + ' params: ' + str(params))
87+
' headers: ' + str(headers) +
88+
' detail body:' + result + ' params: ' + str(params))
8689
try:
8790
response.close()
8891
connection.close()

tigeropen/tiger_open_client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from tigeropen.common.consts import OPEN_API_SERVICE_VERSION, THREAD_LOCAL
1616
from tigeropen.common.consts.params import P_TIMESTAMP, P_TIGER_ID, P_METHOD, P_CHARSET, P_VERSION, P_SIGN_TYPE, \
1717
P_DEVICE_ID, P_NOTIFY_URL, COMMON_PARAM_KEYS, P_SIGN
18-
from tigeropen.common.consts.service_types import USER_LICENSE, PLACE_ORDER, CANCEL_ORDER, MODIFY_ORDER
18+
from tigeropen.common.consts.service_types import USER_LICENSE, PLACE_ORDER, CANCEL_ORDER, MODIFY_ORDER, \
19+
USER_TOKEN_REFRESH
1920
from tigeropen.common.exceptions import ResponseException, RequestException
2021
from tigeropen.common.request import OpenApiRequest
2122
from tigeropen.common.response import TigerResponse
@@ -58,6 +59,7 @@ def __init__(self, client_config, logger=None):
5859
self.__device_id = self.__get_device_id()
5960
self.__init_license()
6061
self.__refresh_server_info()
62+
self.__refresh_token()
6163

6264
def __init_license(self):
6365
if self.__config.license is None and self.__config.enable_dynamic_domain:
@@ -156,6 +158,9 @@ def __parse_response(self, response_str, timestamp=None):
156158

157159
return response_content
158160

161+
def _update_header(self):
162+
self.__headers['Authorization'] = self.__config.token
163+
159164
def _get_retry_deco(self, service):
160165
if service not in SKIP_RETRY_SERVICES and self.__config.retry_max_tries > 0:
161166
return backoff.on_exception(backoff.fibo,
@@ -169,6 +174,7 @@ def _get_retry_deco(self, service):
169174
执行接口请求
170175
"""
171176
def execute(self, request, url=None):
177+
self._update_header()
172178
if url is None:
173179
url = self.__config.server_url
174180
THREAD_LOCAL.uuid = str(uuid.uuid1())
@@ -198,3 +204,19 @@ def query_license(self):
198204
if response.is_success():
199205
return json.loads(response.data).get('license')
200206
self.__logger.error(f"failed to query license, response: {response_content}")
207+
208+
def __refresh_token(self):
209+
request = OpenApiRequest(method=USER_TOKEN_REFRESH)
210+
211+
response_content = None
212+
try:
213+
response_content = self.execute(request)
214+
except Exception as e:
215+
self.__logger.error(e, exc_info=True)
216+
if response_content:
217+
response = TigerResponse()
218+
response.parse_response_content(response_content)
219+
if response.is_success():
220+
return json.loads(response.data).get('token')
221+
self.__logger.error(f"failed to refresh token, response: {response_content}")
222+

tigeropen/tiger_open_config.py

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"""
77
import json
88
import logging
9+
import os
910

11+
from jproperties import Properties
1012
from pytz import timezone
1113
from tigeropen.common.consts import Language, ServiceType
1214
from tigeropen.common.util.account_util import AccountUtil
@@ -47,9 +49,12 @@
4749
'MidihTvHHf+tJ0PYD0o3PruI0hl3qhEjHTAxb75T5YD3SGK4IBhHn/Rk6mhqlGgI+bBrBVYaXixm' \
4850
'HfRo75RpUUuWACyeqQkZckgR0McxuW9xRMIa2cXZOoL1E4SL4lXKGhKoWbwIDAQAB'
4951

52+
DEFAULT_PROPS_FILE = 'tiger_openapi_config.properties'
53+
DEFAULT_TOKEN_FILE = 'tiger_openapi_token.properties'
54+
5055

5156
class TigerOpenClientConfig:
52-
def __init__(self, sandbox_debug=False, enable_dynamic_domain=True):
57+
def __init__(self, sandbox_debug=False, enable_dynamic_domain=True, props_path='.'):
5358
# 开发者应用id
5459
self._tiger_id = ''
5560
# 授权账户
@@ -86,17 +91,21 @@ def __init__(self, sandbox_debug=False, enable_dynamic_domain=True):
8691
self._quote_server_url = SANDBOX_SERVER_URL
8792
self._socket_host_port = SANDBOX_SOCKET_HOST_PORT
8893

94+
self.log_level = None
95+
self.log_path = None
96+
self.retry_max_time = 60
97+
self.retry_max_tries = 5
98+
self.props_path = props_path
99+
self.token = None
100+
self._load_props()
101+
self._load_token()
102+
89103
self.domain_conf = dict()
90104
self.enable_dynamic_domain = enable_dynamic_domain
91105
if enable_dynamic_domain:
92106
self.domain_conf = self.query_domains()
93107
self.refresh_server_info()
94108

95-
self.log_level = None
96-
self.log_path = None
97-
self.retry_max_time = 60
98-
self.retry_max_tries = 5
99-
100109
@property
101110
def tiger_id(self):
102111
return self._tiger_id
@@ -221,6 +230,53 @@ def secret_key(self):
221230
def secret_key(self, value):
222231
self._secret_key = value
223232

233+
@property
234+
def token(self):
235+
return self._token
236+
237+
@token.setter
238+
def token(self, value):
239+
self._token = value
240+
241+
def _get_props_path(self, filename):
242+
if self.props_path is not None and os.path.exists(self.props_path):
243+
if os.path.isdir(self.props_path):
244+
full_path = os.path.join(self.props_path, filename)
245+
else:
246+
full_path = self.props_path
247+
return full_path
248+
return None
249+
250+
def _load_props(self):
251+
full_path = self._get_props_path(DEFAULT_PROPS_FILE)
252+
if full_path and os.path.exists(full_path):
253+
try:
254+
p = Properties()
255+
with open(full_path, "rb") as f:
256+
p.load(f, "utf-8")
257+
if not self.tiger_id:
258+
self.tiger_id = getattr(p.get('tiger_id'), 'data', '')
259+
if not self.private_key:
260+
self.private_key = getattr(p.get('private_key_pk1'), 'data', '')
261+
if not self.account:
262+
self.account = getattr(p.get('account'), 'data', '')
263+
if not self.license:
264+
self.license = getattr(p.get('license'), 'data', '')
265+
except Exception as e:
266+
logging.error(e, exc_info=True)
267+
268+
def _load_token(self):
269+
full_path = self._get_props_path(DEFAULT_TOKEN_FILE)
270+
if full_path and os.path.exists(full_path):
271+
try:
272+
p = Properties()
273+
with open(full_path, "r+b") as f:
274+
p.load(f, "utf-8")
275+
if not self.token:
276+
self.token = getattr(p.get('token'), 'data', '')
277+
except Exception as e:
278+
logging.error(e, exc_info=True)
279+
224280
def refresh_server_info(self):
225281
if self.enable_dynamic_domain and self.domain_conf:
226282
if self.license:

0 commit comments

Comments
 (0)