algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
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
12namespace algotradelib {
13namespace tossinvest {
14
15struct TossConfig;
16struct TossRest;
17struct TossWebsocket;
18
24struct 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
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;
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
88struct 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
OAuth client credentials (an app key and secret), the base of each broker's credentials class.
Definition oauth.hpp:36
const std::string & key() const noexcept
Definition oauth.hpp:62
const std::string & secret() const noexcept
Definition oauth.hpp:68
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:131
Toss Securities credentials (client id and secret). Toss has no demo environment.
Definition config.hpp:24
void from_env_impl()
Reads the credentials from TOSS_CLIENT_ID and TOSS_CLIENT_SECRET; called by from_env().
Definition config.hpp:34
void from_json_impl(const std::string &filename)
Reads the credentials from a JSON file, { "client_id": "...", "client_secret": ".....
Definition config.hpp:51
static constexpr std::string_view secret_field() noexcept
Returns the token request's field for the client secret.
Definition config.hpp:84
static constexpr std::string_view key_field() noexcept
Returns the token request's field for the client id.
Definition config.hpp:78
A Toss REST access token and its expiry.
Definition config.hpp:88
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
static constexpr std::string_view token_field() noexcept
Returns the token response's field for the token.
Definition config.hpp:93