blob: 3a06bf656ddb0afaea783db60959efba245c0325 [file]
#ifndef PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_HTTP_CONNECTION_H_
#define PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_HTTP_CONNECTION_H_
#include <nlohmann/json.hpp>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/functional/any_invocable.h"
#include "boost/asio/ip/tcp.hpp" // NOLINT
#include "boost/asio/ssl.hpp" // NOLINT
#include "boost/asio/ssl/stream.hpp" // NOLINT
#include "boost/beast/core.hpp" // NOLINT
#include "boost/beast/core/tcp_stream.hpp" // NOLINT
#include "boost/beast/http.hpp" // NOLINT
#include "boost/beast/version.hpp" // NOLINT
namespace safepower_agent {
namespace beast = boost::beast;
namespace http = beast::http;
using boost::beast::tcp_stream;
class HttpConnection : public std::enable_shared_from_this<HttpConnection> {
public:
HttpConnection(absl::Duration connection_timeout =
absl::Seconds(10),
absl::Duration write_timeout =
absl::Seconds(10));
void PerformConnection(http::verb verb,
absl::string_view target,
absl::AnyInvocable
<void(absl::StatusOr<nlohmann::json>) && >
callback,
const nlohmann::json& body,
absl::string_view ip = "127.0.0.1",
const uint16_t port = 80);
void Write(beast::error_code ec);
void Read(beast::error_code ec,
size_t bytes_transferred);
void Callback(beast::error_code ec,
std::size_t bytes_transferred);
private:
http::verb verb_;
std::string target_;
std::string body_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
absl::AnyInvocable<void(absl::StatusOr<nlohmann::json>) &&>
provided_callback_;
http::request<http::string_body> req_;
http::response<http::string_body> res_;
boost::asio::ip::tcp::endpoint end_point_;
absl::Duration connection_timeout_;
absl::Duration write_timeout_;
};
} // namespace safepower_agent
#endif // PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_HTTP_CONNECTION_H_