-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinvestapiclient.cpp
86 lines (72 loc) · 2.69 KB
/
investapiclient.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
#include "investapiclient.h"
#include "sandboxservice.h"
#include "marketdataservice.h"
#include "marketdatastreamservice.h"
#include "usersservice.h"
#include "instrumentsservice.h"
#include "operationsservice.h"
#include "ordersservice.h"
#include "ordersstreamservice.h"
#include "stopordersservice.h"
#include "commontypes.h"
#ifdef _WIN32
#include <Windows.h>
#include <wincrypt.h>
std::string utf8Encode(const std::wstring& wstr)
{
if (wstr.empty())
return std::string();
int sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(sizeNeeded, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], sizeNeeded, NULL, NULL);
return strTo;
}
grpc::SslCredentialsOptions getSslOptions()
{
// Fetch root certificate as required on Windows (s. issue 25533).
grpc::SslCredentialsOptions result;
// Open root certificate store.
HANDLE hRootCertStore = CertOpenSystemStoreW(NULL, L"ROOT");
if (!hRootCertStore)
return result;
// Get all root certificates.
PCCERT_CONTEXT pCert = NULL;
while ((pCert = CertEnumCertificatesInStore(hRootCertStore, pCert)) != NULL)
{
// Append this certificate in PEM formatted data.
DWORD size = 0;
CryptBinaryToStringW(pCert->pbCertEncoded, pCert->cbCertEncoded, CRYPT_STRING_BASE64HEADER, NULL, &size);
std::vector<WCHAR> pem(size);
CryptBinaryToStringW(pCert->pbCertEncoded, pCert->cbCertEncoded, CRYPT_STRING_BASE64HEADER, pem.data(), &size);
result.pem_root_certs += utf8Encode(pem.data());
}
CertCloseStore(hRootCertStore, 0);
return result;
}
#endif
InvestApiClient::InvestApiClient(const std::string &host, const std::string &pass)
{
auto channel = grpc::CreateChannel(host, grpc::SslCredentials(
#ifdef _WIN32
getSslOptions()
#else
grpc::SslCredentialsOptions()
#endif
));
m_services["sandbox"] = std::make_shared<Sandbox>(channel, pass);
m_services["users"] = std::make_shared<Users>(channel, pass);
m_services["marketdata"] = std::make_shared<MarketData>(channel, pass);
m_services["instruments"] = std::make_shared<Instruments>(channel, pass);
m_services["operations"] = std::make_shared<Operations>(channel, pass);
m_services["orders"] = std::make_shared<Orders>(channel, pass);
m_services["stoporders"] = std::make_shared<StopOrders>(channel, pass);
m_services["marketdatastream"] = std::make_shared<MarketDataStream>(channel, pass);
m_services["ordersstream"] = std::make_shared<OrdersStream>(channel, pass);
}
InvestApiClient::~InvestApiClient()
{
}
std::shared_ptr<CustomService> InvestApiClient::service(const std::string &serviceName)
{
return m_services.count(serviceName) ? m_services[serviceName] : nullptr;
}