algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
Loading...
Searching...
No Matches
config.hpp
1
#pragma once
2
3
#include "../../network/oauth.hpp"
4
#include <ctime>
5
#include <boost/json.hpp>
6
#include <cstdlib>
7
#include <fstream>
8
#include <iterator>
9
#include <stdexcept>
10
#include <string_view>
11
12
namespace
algotradelib
{
13
namespace
tossinvest
{
14
15
struct
TossConfig
;
16
struct
TossRest
;
17
struct
TossWebsocket;
18
24
struct
TossConfig
:
public
network::ClientCredentials
<TossConfig> {
25
// std::string grant_type = "client_credentials"; // length = 18
26
// std::string client_id_;
27
// std::string client_secret_;
28
34
void
from_env_impl
() {
35
const
char
*
key
= std::getenv(
"TOSS_CLIENT_ID"
);
36
const
char
*
secret
= std::getenv(
"TOSS_CLIENT_SECRET"
);
37
if
(!
key
|| !
secret
) {
38
throw
std::runtime_error(
"Add TOSS_CLIENT_ID and TOSS_CLIENT_SECRET environment variables."
);
39
}
40
key_
=
key
;
41
secret_
=
secret
;
42
}
43
51
void
from_json_impl
(
const
std::string& filename) {
52
std::ifstream ifs(filename);
53
if
(!ifs.is_open()) {
54
throw
std::runtime_error(
"Cannot open Toss Invest credential file: "
+ filename);
55
}
56
const
std::string content(
57
std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>{}
58
);
59
boost::system::error_code ec;
60
const
auto
jv = json::parse(content, ec);
61
if
(ec) {
62
throw
std::runtime_error(
63
"Failed to parse Toss Invest credentials file: "
+ filename +
" — "
+ ec.message()
64
);
65
}
66
const
auto
& doc = jv.as_object();
67
if
(!doc.contains(
"client_id"
) || !doc.contains(
"client_secret"
)) {
68
throw
std::runtime_error(
"Credential file should contain client_id and client_secret"
);
69
}
70
key_
= doc.at(
"client_id"
).as_string().c_str();
71
secret_
= doc.at(
"client_secret"
).as_string().c_str();
72
}
73
78
static
constexpr
std::string_view
key_field
() noexcept {
return
"client_id"
; }
79
84
static
constexpr
std::string_view
secret_field
() noexcept {
return
"client_secret"
; }
85
};
86
88
struct
TossRest
:
public
network::AuthBody
<TossRest> {
93
static
constexpr
std::string_view
token_field
() noexcept {
return
"access_token"
; }
94
96
void
from_json_impl
(
const
json::object&) {}
97
98
// bool valid() const noexcept {
99
// const int64_t now = std::chrono::duration_cast<std::chrono::seconds>(
100
// std::chrono::system_clock::now().time_since_epoch()
101
// ).count();
102
// return !access_token_.empty() && (expires_in_ - now) > 300; // 5min
103
// }
104
};
105
106
}
// namespace tossinvest
107
}
// namespace algotradelib
algotradelib::network::ClientCredentials
OAuth client credentials (an app key and secret), the base of each broker's credentials class.
Definition
oauth.hpp:36
algotradelib::network::ClientCredentials< TossConfig >::key
const std::string & key() const noexcept
Definition
oauth.hpp:62
algotradelib::network::ClientCredentials< TossConfig >::key_
std::string key_
Definition
oauth.hpp:94
algotradelib::network::ClientCredentials< TossConfig >::secret_
std::string secret_
Definition
oauth.hpp:96
algotradelib::network::ClientCredentials< TossConfig >::secret
const std::string & secret() const noexcept
Definition
oauth.hpp:68
algotradelib::tossinvest
The Toss Securities Open API client.
Definition
auth.hpp:9
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib::network::AuthBody
An OAuth access token and its expiry, the base of each broker's token-response class.
Definition
oauth.hpp:131
algotradelib::tossinvest::TossConfig
Toss Securities credentials (client id and secret). Toss has no demo environment.
Definition
config.hpp:24
algotradelib::tossinvest::TossConfig::from_env_impl
void from_env_impl()
Reads the credentials from TOSS_CLIENT_ID and TOSS_CLIENT_SECRET; called by from_env().
Definition
config.hpp:34
algotradelib::tossinvest::TossConfig::from_json_impl
void from_json_impl(const std::string &filename)
Reads the credentials from a JSON file, { "client_id": "...", "client_secret": ".....
Definition
config.hpp:51
algotradelib::tossinvest::TossConfig::secret_field
static constexpr std::string_view secret_field() noexcept
Returns the token request's field for the client secret.
Definition
config.hpp:84
algotradelib::tossinvest::TossConfig::key_field
static constexpr std::string_view key_field() noexcept
Returns the token request's field for the client id.
Definition
config.hpp:78
algotradelib::tossinvest::TossRest
A Toss REST access token and its expiry.
Definition
config.hpp:88
algotradelib::tossinvest::TossRest::from_json_impl
void from_json_impl(const json::object &)
Reads nothing more from a token response (the argument is unused; Toss states expires_in); called by ...
Definition
config.hpp:96
algotradelib::tossinvest::TossRest::token_field
static constexpr std::string_view token_field() noexcept
Returns the token response's field for the token.
Definition
config.hpp:93
algotradelib
src
brokers
tossinvest
config.hpp
Generated by
1.18.0