-
Notifications
You must be signed in to change notification settings - Fork 202
/
main.cpp
101 lines (100 loc) · 4.51 KB
/
main.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
#include "ccapi_cpp/ccapi_session.h"
namespace ccapi {
Logger* Logger::logger = nullptr; // This line is needed.
class MyEventHandler : public EventHandler {
public:
bool processEvent(const Event& event, Session* session) override {
std::cout << "Received an event:\n" + event.toStringPretty(2, 2) << std::endl;
return true;
}
};
class ExecutionManagementServiceCoinbaseCustom : public ExecutionManagementServiceCoinbase {
public:
ExecutionManagementServiceCoinbaseCustom(std::function<void(Event&, Queue<Event>*)> eventHandler, SessionOptions sessionOptions,
SessionConfigs sessionConfigs, ServiceContextPtr serviceContextPtr)
: ExecutionManagementServiceCoinbase(eventHandler, sessionOptions, sessionConfigs, serviceContextPtr) {}
protected:
void convertRequestForRestCustom(http::request<http::string_body>& req, const Request& request, const TimePoint& now, const std::string& symbolId,
const std::map<std::string, std::string>& credential) override {
switch (request.getOperation()) {
case Request::Operation::CUSTOM: {
if (request.getParamList().at(0).at("CUSTOM_OPERATION") == "LIST_PROFILES") {
req.method(http::verb::get);
req.target("/profiles");
this->signRequest(req, "", credential);
}
} break;
default:
ExecutionManagementServiceCoinbase::convertRequestForRestCustom(req, request, now, symbolId, credential);
}
}
void processSuccessfulTextMessageRest(int statusCode, const Request& request, const std::string& textMessage, const TimePoint& timeReceived,
Queue<Event>* eventQueuePtr) override {
switch (request.getOperation()) {
case Request::Operation::CUSTOM: {
if (request.getParamList().at(0).at("CUSTOM_OPERATION") == "LIST_PROFILES") {
rj::Document document;
document.Parse(textMessage.c_str());
Message message;
message.setTimeReceived(timeReceived);
message.setCorrelationIdList({request.getCorrelationId()});
message.setType(Message::Type::CUSTOM);
std::vector<Element> elementList;
for (const auto& x : document.GetArray()) {
Element element;
element.insert("PROFILE_ID", std::string(x["id"].GetString()));
elementList.push_back(element);
}
message.setElementList(elementList);
std::vector<Message> messageList;
messageList.push_back(std::move(message));
Event event;
event.setType(Event::Type::RESPONSE);
event.addMessages(messageList);
this->eventHandler(event, eventQueuePtr);
}
} break;
default:
ExecutionManagementServiceCoinbase::processSuccessfulTextMessageRest(statusCode, request, textMessage, timeReceived, eventQueuePtr);
}
}
};
} /* namespace ccapi */
using ::ccapi::ExecutionManagementServiceCoinbaseCustom;
using ::ccapi::MyEventHandler;
using ::ccapi::Request;
using ::ccapi::Session;
using ::ccapi::SessionConfigs;
using ::ccapi::SessionOptions;
using ::ccapi::UtilSystem;
int main(int argc, char** argv) {
if (UtilSystem::getEnvAsString("COINBASE_API_KEY").empty()) {
std::cerr << "Please set environment variable COINBASE_API_KEY" << std::endl;
return EXIT_FAILURE;
}
if (UtilSystem::getEnvAsString("COINBASE_API_SECRET").empty()) {
std::cerr << "Please set environment variable COINBASE_API_SECRET" << std::endl;
return EXIT_FAILURE;
}
if (UtilSystem::getEnvAsString("COINBASE_API_PASSPHRASE").empty()) {
std::cerr << "Please set environment variable COINBASE_API_PASSPHRASE" << std::endl;
return EXIT_FAILURE;
}
SessionOptions sessionOptions;
SessionConfigs sessionConfigs;
MyEventHandler eventHandler;
Session session(sessionOptions, sessionConfigs, &eventHandler);
session.serviceByServiceNameExchangeMap[CCAPI_EXECUTION_MANAGEMENT][CCAPI_EXCHANGE_NAME_COINBASE] =
std::make_shared<ExecutionManagementServiceCoinbaseCustom>(session.internalEventHandler, session.sessionOptions, session.sessionConfigs,
session.serviceContextPtr);
Request request(Request::Operation::CUSTOM, "coinbase");
request.serviceName = CCAPI_EXECUTION_MANAGEMENT;
request.appendParam({
{"CUSTOM_OPERATION", "LIST_PROFILES"},
});
session.sendRequest(request);
std::this_thread::sleep_for(std::chrono::seconds(10));
session.stop();
std::cout << "Bye" << std::endl;
return EXIT_SUCCESS;
}