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