algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
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
14namespace algotradelib {
15namespace tossinvest {
16
17struct TossConfig;
18struct TossRest;
19struct TossWebsocket;
20
27struct 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
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;
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
120struct 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
OAuth client credentials (an app key and secret), the base of each broker's credentials class.
Definition oauth.hpp:37
const std::string & key() const noexcept
Definition oauth.hpp:78
const std::string & secret() const noexcept
Definition oauth.hpp:86
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.
The Toss Securities Open API client.
Definition auth.hpp:9
The library: broker-neutral types (core), and the modules below.
An OAuth access token and its expiry, the base of each broker's token-response class.
Definition oauth.hpp:150
Toss Securities credentials (client id and secret). Toss has no demo environment.
Definition config.hpp:27
std::string token_identity() const
Names what a token of these credentials may not outlive, for a network::TokenStore.
Definition config.hpp:114
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
void from_env_impl()
Reads the credentials from TOSS_CLIENT_ID and TOSS_CLIENT_SECRET; called by from_env().
Definition config.hpp:37
void from_json_impl(const std::string &filename)
Reads the credentials from a JSON file, { "client_id": "...", "client_secret": ".....
Definition config.hpp:54
static constexpr std::string_view secret_field() noexcept
Returns the token request's field for the client secret.
Definition config.hpp:106
static constexpr std::string_view key_field() noexcept
Returns the token request's field for the client id.
Definition config.hpp:98
A Toss REST access token and its expiry.
Definition config.hpp:120
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
static constexpr std::string_view token_field() noexcept
Returns the token response's field for the token.
Definition config.hpp:125