Electroneum
Loading...
Searching...
No Matches
rpc_client.h
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#pragma once
31
32#include <boost/optional/optional.hpp>
33
38#include "net/http_auth.h"
39#include "net/http_client.h"
40#include "net/net_ssl.h"
41#include "string_tools.h"
42
43namespace tools
44{
45 class t_rpc_client final
46 {
47 private:
49 public:
51 uint32_t ip
52 , uint16_t port
53 , boost::optional<epee::net_utils::http::login> user
55 )
56 : m_http_client{}
57 {
58 m_http_client.set_server(
59 epee::string_tools::get_ip_string_from_int32(ip), std::to_string(port), std::move(user), std::move(ssl_options)
60 );
61 }
62
63 template <typename T_req, typename T_res>
65 T_req & req
66 , T_res & res
67 , std::string const & method_name
68 )
69 {
70 t_http_connection connection(&m_http_client);
71
72 bool ok = connection.is_open();
73 if (!ok)
74 {
75 fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
76 return false;
77 }
78 ok = epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
79 if (!ok)
80 {
81 fail_msg_writer() << "basic_json_rpc_request: Daemon request failed";
82 return false;
83 }
84 else
85 {
86 return true;
87 }
88 }
89
90 template <typename T_req, typename T_res>
92 T_req & req
93 , T_res & res
94 , std::string const & method_name
95 , std::string const & fail_msg
96 )
97 {
98 t_http_connection connection(&m_http_client);
99
100 bool ok = connection.is_open();
101 if (!ok)
102 {
103 fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
104 return false;
105 }
106 ok = epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
107 if (!ok || res.status != CORE_RPC_STATUS_OK) // TODO - handle CORE_RPC_STATUS_BUSY ?
108 {
109 fail_msg_writer() << fail_msg << " -- json_rpc_request: " << res.status;
110 return false;
111 }
112 else
113 {
114 return true;
115 }
116 }
117
118 template <typename T_req, typename T_res>
120 T_req & req
121 , T_res & res
122 , std::string const & relative_url
123 , std::string const & fail_msg
124 )
125 {
126 t_http_connection connection(&m_http_client);
127
128 bool ok = connection.is_open();
129 if (!ok)
130 {
131 fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
132 return false;
133 }
134 ok = epee::net_utils::invoke_http_json(relative_url, req, res, m_http_client, t_http_connection::TIMEOUT());
135 if (!ok || res.status != CORE_RPC_STATUS_OK) // TODO - handle CORE_RPC_STATUS_BUSY ?
136 {
137 fail_msg_writer() << fail_msg << "-- rpc_request: " << res.status;
138 return false;
139 }
140 else
141 {
142 return true;
143 }
144 }
145
147 {
148 t_http_connection connection(&m_http_client);
149 return connection.is_open();
150 }
151 };
152}
connection(typename TProtocol::config_type &ref_config)
static constexpr std::chrono::seconds TIMEOUT()
t_rpc_client(uint32_t ip, uint16_t port, boost::optional< epee::net_utils::http::login > user, epee::net_utils::ssl_options_t ssl_options)
Definition rpc_client.h:50
bool basic_json_rpc_request(T_req &req, T_res &res, std::string const &method_name)
Definition rpc_client.h:64
bool json_rpc_request(T_req &req, T_res &res, std::string const &method_name, std::string const &fail_msg)
Definition rpc_client.h:91
bool rpc_request(T_req &req, T_res &res, std::string const &relative_url, std::string const &fail_msg)
Definition rpc_client.h:119
#define CORE_RPC_STATUS_OK
const char * res
http_simple_client_template< blocked_mode_client > http_simple_client
bool invoke_http_json(const boost::string_ref uri, const t_request &out_struct, t_response &result_struct, t_transport &transport, std::chrono::milliseconds timeout=std::chrono::seconds(15), const boost::string_ref method="GET")
bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request &out_struct, t_response &result_struct, t_transport &transport, std::chrono::milliseconds timeout=std::chrono::seconds(15), const boost::string_ref http_method="GET", const std::string &req_id="0")
std::string get_ip_string_from_int32(uint32_t ip)
Various Tools.
Definition tools.cpp:31
scoped_message_writer fail_msg_writer()
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126