|
6 | 6 | """ |
7 | 7 | import json |
8 | 8 | import logging |
| 9 | +import os |
9 | 10 |
|
| 11 | +from jproperties import Properties |
10 | 12 | from pytz import timezone |
11 | 13 | from tigeropen.common.consts import Language, ServiceType |
12 | 14 | from tigeropen.common.util.account_util import AccountUtil |
|
47 | 49 | 'MidihTvHHf+tJ0PYD0o3PruI0hl3qhEjHTAxb75T5YD3SGK4IBhHn/Rk6mhqlGgI+bBrBVYaXixm' \ |
48 | 50 | 'HfRo75RpUUuWACyeqQkZckgR0McxuW9xRMIa2cXZOoL1E4SL4lXKGhKoWbwIDAQAB' |
49 | 51 |
|
| 52 | +DEFAULT_PROPS_FILE = 'tiger_openapi_config.properties' |
| 53 | +DEFAULT_TOKEN_FILE = 'tiger_openapi_token.properties' |
| 54 | + |
50 | 55 |
|
51 | 56 | 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='.'): |
53 | 58 | # 开发者应用id |
54 | 59 | self._tiger_id = '' |
55 | 60 | # 授权账户 |
@@ -86,17 +91,21 @@ def __init__(self, sandbox_debug=False, enable_dynamic_domain=True): |
86 | 91 | self._quote_server_url = SANDBOX_SERVER_URL |
87 | 92 | self._socket_host_port = SANDBOX_SOCKET_HOST_PORT |
88 | 93 |
|
| 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 | + |
89 | 103 | self.domain_conf = dict() |
90 | 104 | self.enable_dynamic_domain = enable_dynamic_domain |
91 | 105 | if enable_dynamic_domain: |
92 | 106 | self.domain_conf = self.query_domains() |
93 | 107 | self.refresh_server_info() |
94 | 108 |
|
95 | | - self.log_level = None |
96 | | - self.log_path = None |
97 | | - self.retry_max_time = 60 |
98 | | - self.retry_max_tries = 5 |
99 | | - |
100 | 109 | @property |
101 | 110 | def tiger_id(self): |
102 | 111 | return self._tiger_id |
@@ -221,6 +230,53 @@ def secret_key(self): |
221 | 230 | def secret_key(self, value): |
222 | 231 | self._secret_key = value |
223 | 232 |
|
| 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 | + |
224 | 280 | def refresh_server_info(self): |
225 | 281 | if self.enable_dynamic_domain and self.domain_conf: |
226 | 282 | if self.license: |
|
0 commit comments