44
55@author: gaoan
66"""
7+ import base64
78import json
89import logging
910import os
11+ import time
1012
1113from jproperties import Properties
1214from pytz import timezone
5153
5254DEFAULT_PROPS_FILE = 'tiger_openapi_config.properties'
5355DEFAULT_TOKEN_FILE = 'tiger_openapi_token.properties'
56+ TOKEN_REFRESH_DURATION = 24 * 60 * 60 # seconds
5457
5558
5659class TigerOpenClientConfig :
57- def __init__ (self , sandbox_debug = False , enable_dynamic_domain = True , props_path = '.' ):
60+ def __init__ (self , sandbox_debug = None , enable_dynamic_domain = True , props_path = '.' ):
5861 # 开发者应用id
5962 self ._tiger_id = ''
6063 # 授权账户
@@ -85,11 +88,6 @@ def __init__(self, sandbox_debug=False, enable_dynamic_domain=True, props_path='
8588 self ._server_url = SERVER_URL
8689 self ._quote_server_url = SERVER_URL
8790 self ._socket_host_port = SOCKET_HOST_PORT
88- if sandbox_debug :
89- self ._tiger_public_key = SANDBOX_TIGER_PUBLIC_KEY
90- self ._server_url = SANDBOX_SERVER_URL
91- self ._quote_server_url = SANDBOX_SERVER_URL
92- self ._socket_host_port = SANDBOX_SOCKET_HOST_PORT
9391
9492 self .log_level = None
9593 self .log_path = None
@@ -98,11 +96,16 @@ def __init__(self, sandbox_debug=False, enable_dynamic_domain=True, props_path='
9896 self .props_path = props_path
9997 self .token = None
10098 self ._load_props ()
101- self ._load_token ()
99+ self .load_or_store_token ()
102100
103101 self .domain_conf = dict ()
104102 self .enable_dynamic_domain = enable_dynamic_domain
105- if enable_dynamic_domain :
103+ if self ._sandbox_debug :
104+ self ._tiger_public_key = SANDBOX_TIGER_PUBLIC_KEY
105+ self ._server_url = SANDBOX_SERVER_URL
106+ self ._quote_server_url = SANDBOX_SERVER_URL
107+ self ._socket_host_port = SANDBOX_SOCKET_HOST_PORT
108+ if self .enable_dynamic_domain :
106109 self .domain_conf = self .query_domains ()
107110 self .refresh_server_info ()
108111
@@ -239,11 +242,12 @@ def token(self, value):
239242 self ._token = value
240243
241244 def _get_props_path (self , filename ):
242- if self .props_path is not None and os . path . exists ( self . props_path ) :
245+ if self .props_path is not None :
243246 if os .path .isdir (self .props_path ):
244247 full_path = os .path .join (self .props_path , filename )
245248 else :
246- full_path = self .props_path
249+ dirname = os .path .dirname (self .props_path )
250+ full_path = os .path .join (dirname , filename )
247251 return full_path
248252 return None
249253
@@ -262,21 +266,37 @@ def _load_props(self):
262266 self .account = getattr (p .get ('account' ), 'data' , '' )
263267 if not self .license :
264268 self .license = getattr (p .get ('license' ), 'data' , '' )
269+ if not self ._sandbox_debug :
270+ is_sandbox_env = getattr (p .get ('env' ), 'data' , '' ).upper () == 'SANDBOX'
271+ self ._sandbox_debug = is_sandbox_env
265272 except Exception as e :
266273 logging .error (e , exc_info = True )
267274
268- def _load_token (self ):
275+ def load_or_store_token (self , token = None ):
269276 full_path = self ._get_props_path (DEFAULT_TOKEN_FILE )
270277 if full_path and os .path .exists (full_path ):
271278 try :
272279 p = Properties ()
273280 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' , '' )
281+ if token :
282+ self .token = token
283+ p ['token' ] = token
284+ p .store (f , encoding = 'utf-8' )
285+ else :
286+ p .load (f , "utf-8" )
287+ if not self .token :
288+ self .token = getattr (p .get ('token' ), 'data' , '' )
277289 except Exception as e :
278290 logging .error (e , exc_info = True )
279291
292+ def should_token_refresh (self , duration = TOKEN_REFRESH_DURATION ):
293+ if self .token :
294+ tokeninfo = base64 .b64decode (self .token )
295+ gen_ts , expire_ts = tokeninfo [:27 ].decode ('utf-8' ).split (',' )
296+ if (int (time .time ()) - int (gen_ts ) // 1000 ) > duration :
297+ return True
298+ return False
299+
280300 def refresh_server_info (self ):
281301 if self .enable_dynamic_domain and self .domain_conf :
282302 if self .license :
0 commit comments