@@ -19,8 +19,8 @@ using namespace std;
1919namespace TIGER_API {
2020 class OPENAPI_EXPORT ClientConfig {
2121 public:
22- explicit ClientConfig (bool sandbox_debug = false ) :
23- sandbox_debug(sandbox_debug) {
22+ explicit ClientConfig (bool sandbox_debug = false ) :
23+ sandbox_debug(sandbox_debug) {
2424 if (sandbox_debug) {
2525 LOG (WARNING) << U (" SANDBOX IS NOT SUPPORTED" ) << endl;
2626 // server_url = SANDBOX_TIGER_SERVER_URL;
@@ -31,17 +31,17 @@ namespace TIGER_API {
3131 // ClientConfig() : sandbox_debug(false) {};
3232
3333 ClientConfig (utility::string_t tiger_id, utility::string_t private_key, utility::string_t account) :
34- tiger_id (std::move(tiger_id)),
35- private_key (std::move(private_key)),
36- account (std::move(account)) {};
34+ tiger_id (std::move(tiger_id)),
35+ private_key (std::move(private_key)),
36+ account (std::move(account)) {};
3737
3838 ClientConfig (utility::string_t tiger_id, utility::string_t private_key, utility::string_t account,
39- bool sandbox_debug = false , utility::string_t lang = U(" en_US" )) :
40- tiger_id (std::move(tiger_id)),
41- private_key (std::move(private_key)),
42- account (std::move(account)),
43- sandbox_debug (sandbox_debug),
44- lang (lang) {
39+ bool sandbox_debug = false , utility::string_t lang = U(" en_US" )) :
40+ tiger_id (std::move(tiger_id)),
41+ private_key (std::move(private_key)),
42+ account (std::move(account)),
43+ sandbox_debug (sandbox_debug),
44+ lang (lang) {
4545 if (sandbox_debug) {
4646 LOG (WARNING) << U (" SANDBOX IS NOT SUPPORTED" ) << endl;
4747 // server_url = SANDBOX_TIGER_SERVER_URL;
@@ -51,10 +51,11 @@ namespace TIGER_API {
5151 }
5252 };
5353
54- explicit ClientConfig (bool sandbox_debug, const utility::string_t props_path) :
55- sandbox_debug(sandbox_debug),
56- props_path(props_path) {
54+ explicit ClientConfig (bool sandbox_debug, const utility::string_t props_path) :
55+ sandbox_debug(sandbox_debug),
56+ props_path(props_path) {
5757 load_props ();
58+ load_token ();
5859 };
5960
6061 utility::string_t tiger_id;
@@ -66,7 +67,7 @@ namespace TIGER_API {
6667 utility::string_t device_id = Utils::get_device_id();
6768 utility::string_t secret_key;
6869 bool use_full_tick = false ;
69- utility::string_t socket_ca_certs;
70+ utility::string_t socket_ca_certs;
7071 unsigned int send_interval = 10 * 1000 ;
7172 unsigned int receive_interval = 10 * 1000 ;
7273 utility::string_t license;
@@ -107,23 +108,26 @@ namespace TIGER_API {
107108 this ->server_public_key = key;
108109 }
109110
110- const utility::string_t & get_server_url () {
111+ void set_token (const utility::string_t &token) {
112+ this ->token = token;
113+ }
114+
115+ const utility::string_t &get_server_url () {
111116 return this ->server_url ;
112117 }
113118
114- const utility::string_t & get_server_pub_key () {
119+ const utility::string_t & get_server_pub_key () {
115120 return this ->server_public_key ;
116121 }
117122
118- const utility::string_t & get_socket_url () {
123+ const utility::string_t & get_socket_url () {
119124 return this ->socket_url ;
120125 }
121126
122- const utility::string_t & get_socket_port () {
127+ const utility::string_t & get_socket_port () {
123128 return this ->socket_port ;
124129 }
125130
126-
127131
128132 private:
129133 bool sandbox_debug = false ;
@@ -154,7 +158,7 @@ namespace TIGER_API {
154158 }
155159 if (private_key.empty ()) {
156160 private_key = props.get_property (U (" private_key_pk1" ));
157- }
161+ }
158162 if (account.empty ()) {
159163 account = props.get_property (U (" account" ));
160164 }
@@ -171,14 +175,15 @@ namespace TIGER_API {
171175 socket_url = SANDBOX_TIGER_SOCKET_HOST;
172176 socket_port = SANDBOX_TIGER_SOCKET_PORT;
173177 }
174-
175- } catch (const std::exception& e) {
178+
179+ } catch (const std::exception & e) {
176180 LOG (ERROR) << U (" Failed to load properties file: " ) << Utils::str8to16 (e.what ()) << endl;
177181 }
178- LOG (INFO) << U (" Loaded properties file successfully, tiger_id: " ) << tiger_id << " account: " << account << endl;
182+ LOG (INFO) << U (" Loaded properties file successfully, tiger_id: " ) << tiger_id << " account: " << account
183+ << endl;
179184 }
180185
181- utility::string_t get_props_path (const utility::string_t & filename) const {
186+ utility::string_t get_props_path (const utility::string_t & filename) const {
182187 if (!props_path.empty ()) {
183188 if (Utils::is_directory (props_path)) {
184189 return Utils::path_join (props_path, filename);
@@ -195,45 +200,52 @@ namespace TIGER_API {
195200
196201 void load_token () {
197202 utility::string_t full_path = get_token_path ();
198- if (!full_path.empty ()) {
199- try {
200- std::ifstream file (Utils::str16to8 (full_path));
201- if (!file.is_open ()) {
202- LOG (ERROR) << U (" Failed to open token file: " ) << full_path << endl;
203- return ;
204- }
205-
206- Properties props;
207- props.load (file);
208-
209- // get token value
210- token = props.get_property (U (" token" ));
211-
212- } catch (const std::exception& e) {
213- LOG (ERROR) << U (" Failed to load token file: " ) << Utils::str8to16 (e.what ()) << endl;
203+ if (full_path.empty ()) {
204+ return ;
205+ }
206+ // if not exist, return
207+ if (!Utils::is_file (full_path)) {
208+ LOG (DEBUG) << U (" Token file not found: " ) << full_path << endl;
209+ return ;
210+ }
211+ try {
212+ std::ifstream file (Utils::str16to8 (full_path));
213+ if (!file.is_open ()) {
214+ LOG (ERROR) << U (" Failed to open token file: " ) << full_path << endl;
215+ return ;
214216 }
217+
218+ Properties props;
219+ props.load (file);
220+
221+ // get token value
222+ token = props.get_property (U (" token" ));
223+ LOG (INFO) << U (" Loaded token successfully, token: " ) << token << endl;
224+ } catch (const std::exception &e) {
225+ LOG (ERROR) << U (" Failed to load token file: " ) << Utils::str8to16 (e.what ()) << endl;
215226 }
216227 }
217228
218- void save_token (const utility::string_t & new_token) {
229+ void save_token (const utility::string_t & new_token) {
219230 utility::string_t full_path = get_token_path ();
220- if (!full_path.empty ()) {
221- try {
222- Properties props;
231+ if (full_path.empty ()) {
232+ return ;
233+ }
234+ try {
235+ Properties props;
223236 props.set_property (U (" token" ), new_token);
224-
237+
225238 std::ofstream file (Utils::str16to8 (full_path));
226239 if (!file.is_open ()) {
227240 LOG (ERROR) << U (" Failed to open token file for writing: " ) << full_path << endl;
228241 return ;
229242 }
230-
231- props.store (file);
232- token = new_token;
233-
234- } catch (const std::exception& e) {
235- LOG (ERROR) << U (" Failed to save token file: " ) << Utils::str8to16 (e.what ()) << endl;
236- }
243+
244+ props.store (file);
245+ token = new_token;
246+ LOG (INFO) << U (" Saved token successfully, token: " ) << token << endl;
247+ } catch (const std::exception &e) {
248+ LOG (ERROR) << U (" Failed to save token file: " ) << Utils::str8to16 (e.what ()) << endl;
237249 }
238250 }
239251 };
0 commit comments