Updates to epee HTTP client code

- http_simple_client now uses std::chrono for timeouts
  - http_simple_client accepts timeouts per connect / invoke call
  - shortened names of epee http invoke functions
  - invoke command functions only take relative path, connection
    is not automatically performed
This commit is contained in:
Lee Clagett 2017-01-25 00:16:05 -05:00
parent ad91ffe7e5
commit c02e1cb943
15 changed files with 172 additions and 323 deletions

View file

@ -28,6 +28,7 @@
#pragma once
#include <chrono>
#include "string_tools.h"
#include "net/http_client.h"
@ -38,20 +39,16 @@ private:
epee::net_utils::http::http_simple_client * mp_http_client;
bool m_ok;
public:
static unsigned int const TIMEOUT = 200000;
static constexpr std::chrono::seconds TIMEOUT()
{
return std::chrono::minutes(3) + std::chrono::seconds(30);
}
t_http_connection(
epee::net_utils::http::http_simple_client * p_http_client
, uint32_t ip
, uint16_t port
)
t_http_connection(epee::net_utils::http::http_simple_client* p_http_client)
: mp_http_client(p_http_client)
, m_ok(false)
{
// TODO fix http client so that it accepts properly typed arguments
std::string ip_str = epee::string_tools::get_ip_string_from_int32(ip);
std::string port_str = boost::lexical_cast<std::string>(port);
m_ok = mp_http_client->connect(ip_str, port_str, TIMEOUT);
m_ok = mp_http_client->connect(TIMEOUT());
}
~t_http_connection()

View file

@ -34,7 +34,6 @@
#include "storages/http_abstract_invoke.h"
#include "net/http_client.h"
#include "string_tools.h"
#include <boost/lexical_cast.hpp>
namespace tools
{
@ -42,27 +41,16 @@ namespace tools
{
private:
epee::net_utils::http::http_simple_client m_http_client;
uint32_t m_ip;
uint16_t m_port;
public:
t_rpc_client(
uint32_t ip
, uint16_t port
)
: m_http_client{}
, m_ip{ip}
, m_port{port}
{}
std::string build_url(std::string const & relative_url) const
{
std::string result =
"http://"
+ epee::string_tools::get_ip_string_from_int32(m_ip)
+ ":"
+ boost::lexical_cast<std::string>(m_port)
+ relative_url;
return result;
m_http_client.set_server(
epee::string_tools::get_ip_string_from_int32(ip), std::to_string(port)
);
}
template <typename T_req, typename T_res>
@ -72,8 +60,7 @@ namespace tools
, std::string const & method_name
)
{
std::string rpc_url = build_url("/json_rpc");
t_http_connection connection(&m_http_client, m_ip, m_port);
t_http_connection connection(&m_http_client);
bool ok = connection.is_open();
if (!ok)
@ -81,7 +68,7 @@ namespace tools
fail_msg_writer() << "Couldn't connect to daemon";
return false;
}
ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client);
ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
if (!ok)
{
fail_msg_writer() << "Daemon request failed";
@ -101,11 +88,10 @@ namespace tools
, std::string const & fail_msg
)
{
std::string rpc_url = build_url("/json_rpc");
t_http_connection connection(&m_http_client, m_ip, m_port);
t_http_connection connection(&m_http_client);
bool ok = connection.is_open();
ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client);
ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
if (!ok)
{
fail_msg_writer() << "Couldn't connect to daemon";
@ -130,11 +116,10 @@ namespace tools
, std::string const & fail_msg
)
{
std::string rpc_url = build_url(relative_url);
t_http_connection connection(&m_http_client, m_ip, m_port);
t_http_connection connection(&m_http_client);
bool ok = connection.is_open();
ok = ok && epee::net_utils::invoke_http_json_remote_command2(rpc_url, req, res, m_http_client);
ok = ok && epee::net_utils::invoke_http_json(relative_url, req, res, m_http_client, t_http_connection::TIMEOUT());
if (!ok)
{
fail_msg_writer() << "Couldn't connect to daemon";
@ -153,7 +138,7 @@ namespace tools
bool check_connection()
{
t_http_connection connection(&m_http_client, m_ip, m_port);
t_http_connection connection(&m_http_client);
return connection.is_open();
}
};