Skip to content

Commit 0442678

Browse files
committed
token support
1 parent ec33d74 commit 0442678

File tree

6 files changed

+162
-22
lines changed

6 files changed

+162
-22
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.0)
1+
cmake_minimum_required(VERSION 3.0)
22

33

44
# Find the source file that contains the version information
@@ -17,7 +17,7 @@ project(tigerapi VERSION ${PROJECT_VERSION})
1717
# Configure required libraries ...
1818
if(UNIX) # Darwing or Linux
1919

20-
find_package(Boost REQUIRED COMPONENTS system thread log program_options chrono)
20+
find_package(Boost REQUIRED COMPONENTS system thread log program_options chrono filesystem)
2121
find_package(Threads REQUIRED)
2222
find_package(absl REQUIRED)
2323

include/tigerapi/client_config.h

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ namespace TIGER_API {
6060
utility::string_t socket_ca_certs;
6161
unsigned int send_interval = 10 * 1000;
6262
unsigned int receive_interval = 10 * 1000;
63+
utility::string_t license;
6364
utility::string_t token;
6465
utility::string_t props_path;
6566

@@ -109,6 +110,8 @@ namespace TIGER_API {
109110
return this->socket_port;
110111
}
111112

113+
114+
112115
private:
113116
bool sandbox_debug = false;
114117
utility::string_t server_url = TIGER_SERVER_URL;
@@ -117,14 +120,15 @@ namespace TIGER_API {
117120
utility::string_t socket_port = TIGER_SOCKET_PORT;
118121

119122
void load_props() {
120-
if (props_path.empty()) {
123+
utility::string_t full_path = get_props_path(DEFAULT_PROPS_FILE);
124+
if (full_path.empty()) {
121125
return;
122126
}
123127

124128
try {
125-
std::ifstream file(props_path);
129+
std::ifstream file(Utils::str16to8(full_path));
126130
if (!file.is_open()) {
127-
LOG(ERROR) << U("Failed to open properties file: ") << props_path << endl;
131+
LOG(ERROR) << U("Failed to open properties file: ") << full_path << endl;
128132
return;
129133
}
130134

@@ -141,21 +145,82 @@ namespace TIGER_API {
141145
if (account.empty()) {
142146
account = props.get_property(U("account"));
143147
}
148+
if (license.empty()) {
149+
license = props.get_property(U("license"));
150+
}
144151

145-
// 检查是否为沙箱环境
146-
if (!sandbox_debug) {
147-
utility::string_t env = props.get_property(U("env"));
148-
std::transform(env.begin(), env.end(), env.begin(), ::toupper);
149-
if (env == U("SANDBOX")) {
150-
sandbox_debug = true;
151-
server_url = SANDBOX_TIGER_SERVER_URL;
152-
server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
153-
socket_url = SANDBOX_TIGER_SOCKET_HOST;
154-
socket_port = SANDBOX_TIGER_SOCKET_PORT;
155-
}
152+
utility::string_t env = props.get_property(U("env"));
153+
std::transform(env.begin(), env.end(), env.begin(), ::toupper);
154+
if (env == U("SANDBOX")) {
155+
sandbox_debug = true;
156+
server_url = SANDBOX_TIGER_SERVER_URL;
157+
server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
158+
socket_url = SANDBOX_TIGER_SOCKET_HOST;
159+
socket_port = SANDBOX_TIGER_SOCKET_PORT;
156160
}
161+
157162
} catch (const std::exception& e) {
158-
LOG(ERROR) << U("Failed to load properties file: ") << e.what() << endl;
163+
LOG(ERROR) << U("Failed to load properties file: ") << Utils::str8to16(e.what()) << endl;
164+
}
165+
}
166+
167+
utility::string_t get_props_path(const utility::string_t& filename) const {
168+
if (!props_path.empty()) {
169+
if (Utils::is_directory(props_path)) {
170+
return Utils::path_join(props_path, filename);
171+
} else {
172+
utility::string_t dirname = Utils::path_dirname(props_path);
173+
return Utils::path_join(dirname, filename);
174+
}
175+
}
176+
return utility::string_t();
177+
}
178+
179+
utility::string_t get_token_path() const {
180+
return get_props_path(DEFAULT_TOKEN_FILE);
181+
}
182+
183+
void load_token() {
184+
utility::string_t full_path = get_token_path();
185+
if (!full_path.empty()) {
186+
try {
187+
std::ifstream file(Utils::str16to8(full_path));
188+
if (!file.is_open()) {
189+
LOG(ERROR) << U("Failed to open token file: ") << full_path << endl;
190+
return;
191+
}
192+
193+
Properties props;
194+
props.load(file);
195+
196+
// 获取token值
197+
token = props.get_property(U("token"));
198+
199+
} catch (const std::exception& e) {
200+
LOG(ERROR) << U("Failed to load token file: ") << Utils::str8to16(e.what()) << endl;
201+
}
202+
}
203+
}
204+
205+
void save_token(const utility::string_t& new_token) {
206+
utility::string_t full_path = get_token_path();
207+
if (!full_path.empty()) {
208+
try {
209+
Properties props;
210+
props.set_property(U("token"), new_token);
211+
212+
std::ofstream file(Utils::str16to8(full_path));
213+
if (!file.is_open()) {
214+
LOG(ERROR) << U("Failed to open token file for writing: ") << full_path << endl;
215+
return;
216+
}
217+
218+
props.store(file);
219+
token = new_token;
220+
221+
} catch (const std::exception& e) {
222+
LOG(ERROR) << U("Failed to save token file: ") << Utils::str8to16(e.what()) << endl;
223+
}
159224
}
160225
}
161226
};

include/tigerapi/constants.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
#ifndef CPPSDK_CONSTANTS_H
1+
#ifndef CPPSDK_CONSTANTS_H
32
#define CPPSDK_CONSTANTS_H
43
#include "cpprest/details/basic_types.h"
54

@@ -84,4 +83,7 @@ static utility::string_t SANDBOX_TIGER_SOCKET_HOST = U("openapi-sandbox.tigerfin
8483
static utility::string_t TIGER_SOCKET_PORT = U("9883");
8584
static utility::string_t SANDBOX_TIGER_SOCKET_PORT = U("9885");
8685

86+
#define DEFAULT_TOKEN_FILE U("tiger_openapi_token.properties")
87+
#define DEFAULT_PROPS_FILE U("tiger_openapi_config.properties")
88+
8789
#endif //CPPSDK_CONSTANTS_H

include/tigerapi/properties.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ namespace TIGER_API {
4141
}
4242
}
4343

44+
void set_property(const utility::string_t& key, const utility::string_t& value) {
45+
properties[key] = value;
46+
}
47+
4448
utility::string_t get_property(const utility::string_t& key) const {
4549
auto it = properties.find(key);
4650
if (it != properties.end()) {

include/tigerapi/utils.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
2-
3-
#ifndef CPPSDK_UTILS_H
1+
#ifndef CPPSDK_UTILS_H
42
#define CPPSDK_UTILS_H
53

64
#include <time.h>
@@ -45,6 +43,10 @@ namespace TIGER_API {
4543
static utility::string_t fill_public_key_marker(utility::string_t &public_key);
4644

4745
static utility::string_t double_to_string(double num, int precision = 1);
46+
47+
static bool is_directory(const utility::string_t& path);
48+
static utility::string_t path_dirname(const utility::string_t& path);
49+
static utility::string_t path_join(const utility::string_t& path1, const utility::string_t& path2);
4850
};
4951
}
5052
#endif //CPPSDK_UTILS_H

src/utils.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,26 @@
55
#include <algorithm>
66
#include <regex>
77
#include "../include/tigerapi/sign_util.h"
8+
#include <boost/filesystem.hpp>
9+
10+
#ifdef _WIN32
11+
#include <direct.h>
12+
#include <io.h>
13+
#define ACCESS _access
14+
#define STAT _stat
15+
struct stat {
16+
unsigned short st_mode;
17+
};
18+
#define S_ISDIR(mode) ((mode) & _S_IFDIR)
19+
#else
20+
#include <unistd.h>
21+
#include <sys/stat.h>
22+
#define ACCESS access
23+
#endif
824

925
using namespace std;
1026
using namespace web;
27+
namespace fs = boost::filesystem;
1128

1229
namespace TIGER_API {
1330
utility::string_t Utils::str8to16(std::string s) {
@@ -193,4 +210,54 @@ namespace TIGER_API {
193210
std::string result = stream.str();
194211
return Utils::str8to16(result);
195212
}
213+
214+
bool Utils::is_directory(const utility::string_t& path) {
215+
try {
216+
#ifdef _WIN32
217+
fs::path fs_path(path);
218+
#else
219+
fs::path fs_path(utility::conversions::to_utf8string(path));
220+
#endif
221+
return fs::is_directory(fs_path);
222+
} catch (const std::exception&) {
223+
return false;
224+
}
225+
}
226+
227+
utility::string_t Utils::path_dirname(const utility::string_t& path) {
228+
try {
229+
#ifdef _WIN32
230+
fs::path fs_path(path);
231+
#else
232+
fs::path fs_path(utility::conversions::to_utf8string(path));
233+
#endif
234+
235+
auto parent_path = fs_path.parent_path();
236+
237+
#ifdef _WIN32
238+
return parent_path.wstring();
239+
#else
240+
return utility::conversions::to_string_t(parent_path.string());
241+
#endif
242+
} catch (const std::exception&) {
243+
return utility::string_t();
244+
}
245+
}
246+
247+
utility::string_t Utils::path_join(const utility::string_t& path1, const utility::string_t& path2) {
248+
try {
249+
#ifdef _WIN32
250+
fs::path p1(path1);
251+
fs::path p2(path2);
252+
auto result = (p1 / p2).wstring();
253+
#else
254+
fs::path p1(utility::conversions::to_utf8string(path1));
255+
fs::path p2(utility::conversions::to_utf8string(path2));
256+
auto result = utility::conversions::to_string_t((p1 / p2).string());
257+
#endif
258+
return result;
259+
} catch (const std::exception&) {
260+
return utility::string_t();
261+
}
262+
}
196263
}

0 commit comments

Comments
 (0)