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 <boost/json.hpp>
4
#include <cstdlib>
5
#include <ctime>
6
#include <fstream>
7
#include <iterator>
8
#include <stdexcept>
9
#include <string>
10
#include <string_view>
11
#include "../../network/oauth.hpp"
12
#include "../../network/token_store.hpp"
13
14
namespace
algotradelib
{
15
namespace
tossinvest
{
16
17
struct
TossConfig
;
18
struct
TossRest
;
19
struct
TossWebsocket;
20
27
struct
TossConfig
:
public
network::ClientCredentials
<TossConfig> {
28
// std::string grant_type = "client_credentials"; // length = 18
29
// std::string client_id_;
30
// std::string client_secret_;
31
37
void
from_env_impl
() {
38
const
char
*
key
= std::getenv(
"TOSS_CLIENT_ID"
);
39
const
char
*
secret
= std::getenv(
"TOSS_CLIENT_SECRET"
);
40
if
(!
key
|| !
secret
) {
41
throw
std::runtime_error(
"Add TOSS_CLIENT_ID and TOSS_CLIENT_SECRET environment variables."
);
42
}
43
key_
=
key
;
44
secret_
=
secret
;
45
}
46
54
void
from_json_impl
(
const
std::string& filename) {
55
std::ifstream ifs(filename);
56
if
(!ifs.is_open()) {
57
throw
std::runtime_error(
"Cannot open Toss Invest credential file: "
+ filename);
58
}
59
const
std::string content(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>{});
60
from_json_text_impl
(content,
"file "
+ filename);
61
}
62
71
void
from_json_text_impl
(std::string_view text,
const
std::string& origin) {
72
boost::system::error_code ec;
73
const
auto
jv = json::parse(text, ec);
74
if
(ec || !jv.is_object()) {
75
throw
std::runtime_error(
76
"Failed to parse Toss Invest credentials "
+ origin +
77
(ec ?
" — "
+ ec.message() : std::string(
" — not a JSON object"
))
78
);
79
}
80
const
auto
& doc = jv.as_object();
81
const
json::value*
key
= doc.if_contains(
"client_id"
);
82
const
json::value*
secret
= doc.if_contains(
"client_secret"
);
83
if
(!
key
|| !
secret
|| !
key
->is_string() || !
secret
->is_string()) {
84
throw
std::runtime_error(
85
"Toss Invest credential "
+ origin +
" should contain client_id and client_secret as strings"
86
);
87
}
88
// Both are checked before either is assigned, so a refused document leaves these credentials as they
89
// were.
90
key_
=
key
->get_string().c_str();
91
secret_
=
secret
->get_string().c_str();
92
}
93
98
static
constexpr
std::string_view
key_field
() noexcept {
99
return
"client_id"
;
100
}
101
106
static
constexpr
std::string_view
secret_field
() noexcept {
107
return
"client_secret"
;
108
}
109
114
std::string
token_identity
()
const
{
115
return
network::token_identity
(
"toss"
,
"real"
,
key_
);
116
}
117
};
118
120
struct
TossRest
:
public
network::AuthBody
<TossRest> {
125
static
constexpr
std::string_view
token_field
() noexcept {
126
return
"access_token"
;
127
}
128
130
void
from_json_impl
(
const
json::object&) {}
131
132
// bool valid() const noexcept {
133
// const int64_t now = std::chrono::duration_cast<std::chrono::seconds>(
134
// std::chrono::system_clock::now().time_since_epoch()
135
// ).count();
136
// return !access_token_.empty() && (expires_in_ - now) > 300; // 5min
137
// }
138
};
139
140
}
// namespace tossinvest
141
}
// namespace algotradelib
algotradelib::network::ClientCredentials
OAuth client credentials (an app key and secret), the base of each broker's credentials class.
Definition
oauth.hpp:37
algotradelib::network::ClientCredentials< TossConfig >::key
const std::string & key() const noexcept
Definition
oauth.hpp:78
algotradelib::network::ClientCredentials< TossConfig >::key_
std::string key_
Definition
oauth.hpp:113
algotradelib::network::ClientCredentials< TossConfig >::secret_
std::string secret_
Definition
oauth.hpp:115
algotradelib::network::ClientCredentials< TossConfig >::secret
const std::string & secret() const noexcept
Definition
oauth.hpp:86
algotradelib::network::token_identity
std::string token_identity(std::string_view broker, std::string_view environment, std::string_view key)
Names what a token may not outlive: the broker, the environment and the app key.
Definition
token_store.hpp:123
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:150
algotradelib::tossinvest::TossConfig
Toss Securities credentials (client id and secret). Toss has no demo environment.
Definition
config.hpp:27
algotradelib::tossinvest::TossConfig::token_identity
std::string token_identity() const
Names what a token of these credentials may not outlive, for a network::TokenStore.
Definition
config.hpp:114
algotradelib::tossinvest::TossConfig::from_json_text_impl
void from_json_text_impl(std::string_view text, const std::string &origin)
Reads the credentials from JSON text in the file's format; called by from_json_text....
Definition
config.hpp:71
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:37
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:54
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:106
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:98
algotradelib::tossinvest::TossRest
A Toss REST access token and its expiry.
Definition
config.hpp:120
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:130
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:125
algotradelib
src
brokers
tossinvest
config.hpp
Generated by
1.18.0