88#include < string>
99#include < utility>
1010#include " constants.h"
11- #include " easylogging++ .h"
11+ #include " logger .h"
1212#include " utils.h"
1313#include " win32.h"
14- #include < fstream>
15- #include " properties.h"
1614
1715using namespace std ;
1816
1917namespace TIGER_API {
2018 class OPENAPI_EXPORT ClientConfig {
2119 public:
22- explicit ClientConfig (bool sandbox_debug = false ) :
23- sandbox_debug(sandbox_debug) {
24- if (sandbox_debug) {
25- LOG (WARNING) << U (" SANDBOX IS NOT SUPPORTED" ) << endl;
26- // server_url = SANDBOX_TIGER_SERVER_URL;
27- // server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
28- }
29- };
30-
31- // ClientConfig() : sandbox_debug(false) {};
32-
33- 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)) {};
37-
20+ ClientConfig (bool sandbox_debug = false );
21+ ClientConfig (utility::string_t tiger_id, utility::string_t private_key, utility::string_t account);
3822 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) {
45- if (sandbox_debug) {
46- LOG (WARNING) << U (" SANDBOX IS NOT SUPPORTED" ) << endl;
47- // server_url = SANDBOX_TIGER_SERVER_URL;
48- // server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
49- // socket_url = SANDBOX_TIGER_SOCKET_HOST;
50- // socket_port = SANDBOX_TIGER_SOCKET_PORT;
51- }
52- };
53-
54- explicit ClientConfig (bool sandbox_debug, const utility::string_t props_path) :
55- sandbox_debug(sandbox_debug),
56- props_path(props_path) {
57- load_props ();
58- load_token ();
59- };
23+ bool sandbox_debug = false , utility::string_t lang = U(" en_US" ));
24+ ClientConfig (bool sandbox_debug, const utility::string_t props_path);
6025
6126 utility::string_t tiger_id;
6227 utility::string_t private_key;
@@ -74,60 +39,26 @@ namespace TIGER_API {
7439 utility::string_t token;
7540 utility::string_t props_path;
7641
77- void check () {
78- if (this ->tiger_id .empty ()) {
79- LOG (ERROR) << U (" Client Config error: tiger_id can't be empty" ) << endl;
80- throw std::runtime_error (" Client Config error: tiger_id can't be empty" );
81- }
82- if (this ->private_key .empty ()) {
83- LOG (ERROR) << U (" Client Config error: private_key can't be empty" ) << endl;
84- throw std::runtime_error (" Client Config error: private_key can't be empty" );
85- }
86- }
87-
88- void check_account () {
89- if (this ->account .empty ()) {
90- LOG (ERROR) << U (" Client Config error: account can't be empty" ) << endl;
91- throw std::runtime_error (" Client Config error: account can't be empty" );
92- }
93- }
94-
95- void set_server_url (const utility::string_t &url) {
96- this ->server_url = url;
97- }
98-
99- void set_socket_url (const utility::string_t &url) {
100- this ->socket_url = url;
101- }
102-
103- void set_socket_port (const utility::string_t &port) {
104- this ->socket_port = port;
105- }
106-
107- void set_server_public_key (const utility::string_t &key) {
108- this ->server_public_key = key;
109- }
110-
111- void set_token (const utility::string_t &token) {
112- this ->token = token;
113- }
114-
115- const utility::string_t &get_server_url () {
116- return this ->server_url ;
117- }
118-
119- const utility::string_t &get_server_pub_key () {
120- return this ->server_public_key ;
121- }
122-
123- const utility::string_t &get_socket_url () {
124- return this ->socket_url ;
125- }
126-
127- const utility::string_t &get_socket_port () {
128- return this ->socket_port ;
129- }
42+ void check ();
43+ void check_account ();
44+
45+ void set_server_url (const utility::string_t & url);
46+
47+ void set_socket_url (const utility::string_t & url);
48+
49+ void set_socket_port (const utility::string_t & port);
50+
51+ void set_server_public_key (const utility::string_t & key);
52+
53+ void set_token (const utility::string_t & token);
54+
55+ const utility::string_t & get_server_url ();
13056
57+ const utility::string_t & get_server_pub_key ();
58+
59+ const utility::string_t & get_socket_url ();
60+
61+ const utility::string_t & get_socket_port ();
13162
13263 private:
13364 bool sandbox_debug = false ;
@@ -136,113 +67,15 @@ namespace TIGER_API {
13667 utility::string_t socket_url = TIGER_SOCKET_HOST;
13768 utility::string_t socket_port = TIGER_SOCKET_PORT;
13869
139- void load_props () {
140- utility::string_t full_path = get_props_path (DEFAULT_PROPS_FILE);
141- if (full_path.empty ()) {
142- return ;
143- }
144- LOG (INFO) << U (" Loading properties file from: " ) << full_path << endl;
145-
146- try {
147- std::ifstream file (Utils::str16to8 (full_path));
148- if (!file.is_open ()) {
149- LOG (ERROR) << U (" Failed to open properties file: " ) << full_path << endl;
150- return ;
151- }
152-
153- Properties props;
154- props.load (file);
155-
156- if (tiger_id.empty ()) {
157- tiger_id = props.get_property (U (" tiger_id" ));
158- }
159- if (private_key.empty ()) {
160- private_key = props.get_property (U (" private_key_pk1" ));
161- }
162- if (account.empty ()) {
163- account = props.get_property (U (" account" ));
164- }
165- if (license.empty ()) {
166- license = props.get_property (U (" license" ));
167- }
168-
169- utility::string_t env = props.get_property (U (" env" ));
170- std::transform (env.begin (), env.end (), env.begin (), ::toupper);
171- if (env == U (" SANDBOX" )) {
172- sandbox_debug = true ;
173- server_url = SANDBOX_TIGER_SERVER_URL;
174- server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
175- socket_url = SANDBOX_TIGER_SOCKET_HOST;
176- socket_port = SANDBOX_TIGER_SOCKET_PORT;
177- }
178-
179- } catch (const std::exception &e) {
180- LOG (ERROR) << U (" Failed to load properties file: " ) << Utils::str8to16 (e.what ()) << endl;
181- }
182- LOG (INFO) << U (" Loaded properties file successfully, tiger_id: " ) << tiger_id << " account: " << account
183- << endl;
184- }
185-
186- utility::string_t get_props_path (const utility::string_t &filename) const {
187- if (!props_path.empty ())
188- {
189- if (Utils::is_directory (props_path)) {
190- return props_path + filename;
191- }
192- }
193- return U (" " );
194- }
195-
196- utility::string_t get_token_path () const {
197- return get_props_path (DEFAULT_TOKEN_FILE);
198- }
199-
200- void load_token () {
201- utility::string_t full_path = get_token_path ();
202- if (full_path.empty ()) {
203- return ;
204- }
205- try {
206- std::ifstream file (Utils::str16to8 (full_path));
207- if (!file.is_open ()) {
208- LOG (ERROR) << U (" Failed to open token file: " ) << full_path << endl;
209- return ;
210- }
211-
212- Properties props;
213- props.load (file);
214-
215- // get token value
216- token = props.get_property (U (" token" ));
217- LOG (INFO) << U (" Loaded token successfully, token: " ) << token << endl;
218- } catch (const std::exception &e) {
219- LOG (ERROR) << U (" Failed to load token file: " ) << Utils::str8to16 (e.what ()) << endl;
220- }
221- }
222-
223- void save_token (const utility::string_t &new_token) {
224- utility::string_t full_path = get_token_path ();
225- if (full_path.empty ()) {
226- return ;
227- }
228- try {
229- Properties props;
230- props.set_property (U (" token" ), new_token);
231-
232- std::ofstream file (Utils::str16to8 (full_path));
233- if (!file.is_open ()) {
234- LOG (ERROR) << U (" Failed to open token file for writing: " ) << full_path << endl;
235- return ;
236- }
237-
238- props.store (file);
239- token = new_token;
240- LOG (INFO) << U (" Saved token successfully, token: " ) << token << endl;
241- } catch (const std::exception &e) {
242- LOG (ERROR) << U (" Failed to save token file: " ) << Utils::str8to16 (e.what ()) << endl;
243- }
244- }
70+ void load_props ();
71+
72+ utility::string_t get_props_path (const utility::string_t & filename) const ;
73+
74+ utility::string_t get_token_path () const ;
75+
76+ void load_token ();
77+
78+ void save_token (const utility::string_t & new_token);
24579 };
24680}
247-
24881#endif // TIGERAPI_CLIENT_CONFIG_H
0 commit comments