-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathbitmexhttp.cpp
77 lines (64 loc) · 2.89 KB
/
bitmexhttp.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
#include "bitmexhttp.h"
BitmexHttp::BitmexHttp(Interfaces::IRateLimitMonitor &monitor)
: client(web::http::client::http_client(web::uri(uri))), monitor(monitor)
{
}
BitmexHttp::BitmexHttp(const std::string &uri, Interfaces::IRateLimitMonitor &monitor)
: client(web::http::client::http_client(web::uri(uri))), uri(uri), monitor(monitor)
{
}
BitmexHttp::BitmexHttp(const std::string &uri, const std::string &api_key, const std::string &api_secret, Interfaces::IRateLimitMonitor &monitor)
: client(web::http::client::http_client(web::uri(uri))), uri(uri), api_key(api_key), api_secret(api_secret), monitor(monitor)
{
}
BitmexHttp::~BitmexHttp()
{
}
http_request BitmexHttp::build_request(const std::string &path, const std::string &verb, const std::string &body)
{
std::string expires = std::to_string(util::get_seconds_timestamp(util::current_time()).count() + 60);
std::string data = verb + path + expires + body;
std::string sign = util::encoding::hmac(api_secret, data);
http_request req(this->method_map.at(verb));
req.set_request_uri(this->uri + path);
req.headers().add("Content-Type", "application/json");
req.headers().add("Accept", "application/json");
req.headers().add("X-Requested-With", "XMLHttpRequest");
req.headers().add("api-expires", expires);
req.headers().add("api-key", this->api_key);
req.headers().add("api-signature", sign);
req.set_body(body);
return req;
}
pplx::task<json> BitmexHttp::call(const std::string &path, const std::string &verb)
{
std::string body = "";
http_request req = this->build_request(path, verb, body);
return this->client.request(req)
.then([this](http_response response) {
int limit = std::stoi(response.headers()["x-ratelimit-limit"]);
int remain = std::stoi(response.headers()["x-ratelimit-remaining"]);
Poco::DateTime next_reset(Poco::Timestamp::fromEpochTime(std::stoi(response.headers()["x-ratelimit-reset"])));
this->monitor.update_rate_limit(limit, remain, next_reset);
return response.extract_string();
})
.then([](std::string str_res) {
return pplx::task_from_result(json::parse(str_res));
});
}
pplx::task<json> BitmexHttp::call(const std::string &path, const std::string &verb, const json &payload)
{
std::string body = payload.dump();
http_request req = this->build_request(path, verb, body);
return this->client.request(req)
.then([this](http_response response) {
int limit = std::stoi(response.headers()["x-ratelimit-limit"]);
int remain = std::stoi(response.headers()["x-ratelimit-remaining"]);
Poco::DateTime next_reset(Poco::Timestamp::fromEpochTime(std::stoi(response.headers()["x-ratelimit-reset"])));
this->monitor.update_rate_limit(limit, remain, next_reset);
return response.extract_string();
})
.then([](std::string str_res) {
return pplx::task_from_result(json::parse(str_res));
});
}