forked from dmikushin/binance-cxx-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinance_server.cpp
168 lines (129 loc) · 3.67 KB
/
binance_server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Author: tensaix2j
Date : 2017/10/15
C++ library for Binance API.
*/
#include "binance.h"
#include "binance_logger.h"
#include "binance_utils.h"
using namespace binance;
using namespace std;
binance::Server::Server(const char* hostname_, bool simulation_) : hostname(hostname_), simulation(simulation_) { }
const std::string& binance::Server::getHostname() const { return hostname; }
bool binance::Server::isSimulator() const { return simulation; }
// GET /api/v3/time
binanceError_t binance::Server::getTime(Json::Value &json_result)
{
binanceError_t status = binanceSuccess;
Logger::write_log("<get_serverTime>");
string url(hostname);
url += "/api/v3/time";
string str_result;
getCurl(str_result, url);
if (str_result.size() == 0)
status = binanceErrorEmptyServerResponse;
else
{
try
{
Json::Reader reader;
json_result.clear();
reader.parse(str_result, json_result);
CHECK_SERVER_ERR(json_result);
}
catch (exception &e)
{
Logger::write_log("<get_serverTime> Error ! %s", e.what());
status = binanceErrorParsingServerResponse;
}
}
Logger::write_log("<get_serverTime> Done.");
return status;
}
// Curl's callback
static size_t getCurlCb(void *content, size_t size, size_t nmemb, std::string *buffer)
{
Logger::write_log("<curl_cb> ");
size_t newLength = size * nmemb;
size_t oldLength = buffer->size();
buffer->resize(oldLength + newLength);
std::copy((char*)content, (char*)content + newLength, buffer->begin() + oldLength);
Logger::write_log("<curl_cb> Done.");
return newLength;
}
binanceError_t binance::Server::getCurl(string& result_json, const string& url)
{
vector<string> v;
string action = "GET";
string post_data = "";
return getCurlWithHeader(result_json, url, v, post_data, action);
}
class SmartCURL
{
CURL* curl;
public :
CURL* get() { return curl; }
SmartCURL()
{
curl = curl_easy_init();
}
~SmartCURL()
{
curl_easy_cleanup(curl);
}
};
// Do the curl
binanceError_t binance::Server::getCurlWithHeader(string& str_result,
const string& url, const vector<string>& extra_http_header, const string& post_data, const string& action)
{
binanceError_t status = binanceSuccess;
Logger::write_log("<curl_api>");
SmartCURL curl;
while (curl.get())
{
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, getCurlCb);
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &str_result);
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, false);
if (curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, true) != CURLE_OK)
{
Logger::write_log("<curl_api> curl_easy_setopt(CURLOPT_SSL_VERIFYPEER) is not supported");
status = binanceErrorCurlFailed;
break;
}
if (extra_http_header.size() > 0)
{
struct curl_slist *chunk = NULL;
for (int i = 0; i < extra_http_header.size(); i++)
chunk = curl_slist_append(chunk, extra_http_header[i].c_str());
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, chunk);
}
if (post_data.size() > 0 || action == "POST" || action == "PUT" || action == "DELETE")
{
if (action == "PUT" || action == "DELETE")
curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, action.c_str());
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, post_data.c_str());
}
CURLcode res;
try
{
res = curl_easy_perform(curl.get());
}
catch (std::bad_alloc &e)
{
status = binanceErrorCurlOutOfMemory;
}
if (status == binanceSuccess)
{
// Check for errors.
if (res != CURLE_OK)
{
Logger::write_log("<curl_api> curl_easy_perform() failed: %s", curl_easy_strerror(res));
status = binanceErrorCurlFailed;
}
}
break;
}
Logger::write_log("<curl_api> Done.");
return status;
}