Debian Codegraph @

src/common/rpc_client.h

raw symbols SCIP
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"
34 #include "common/scoped_message_writer.h"
35 #include "rpc/core_rpc_server_commands_defs.h"
36 #include "storages/http_abstract_invoke.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:
47 epee::net_utils::http::http_simple_client m_http_client;
48 public:
49 t_rpc_client(
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 {
57 m_http_client.set_server(
58 epee::string_tools::get_ip_string_from_int32(ip), std::to_string(port), std::move(user), std::move(ssl_options)
59 );
60 }
61
62 template <typename T_req, typename T_res>
63 bool basic_json_rpc_request(
64 T_req & req
65 , T_res & res
66 , std::string const & method_name
67 )
68 {
69 t_http_connection connection(&m_http_client);
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 }
77 ok = epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
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>
90 bool json_rpc_request(
91 T_req & req
92 , T_res & res
93 , std::string const & method_name
94 , std::string const & fail_msg
95 )
96 {
97 t_http_connection connection(&m_http_client);
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 }
105 ok = epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
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>
118 bool rpc_request(
119 T_req & req
120 , T_res & res
121 , std::string const & relative_url
122 , std::string const & fail_msg
123 )
124 {
125 t_http_connection connection(&m_http_client);
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 }
133 ok = epee::net_utils::invoke_http_json(relative_url, req, res, m_http_client, t_http_connection::TIMEOUT());
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
145 bool check_connection()
146 {
147 t_http_connection connection(&m_http_client);
148 return connection.is_open();
149 }
150 };
151 }