blob: d7676329324c088fb7b3d6aaad00dc47d7596a54 [file] [log] [blame]
#ifndef PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_HTTP_CONNECTION_H_
#define PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_HTTP_CONNECTION_H_
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include "absl/functional/any_invocable.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
// NOLINTBEGIN(readability/boost)
#include "boost/asio/ip/tcp.hpp"
#include "boost/beast/core.hpp"
#include "boost/beast/core/tcp_stream.hpp"
#include "boost/beast/http.hpp"
#include "boost/beast/http/string_body.hpp"
#include "boost/beast/version.hpp"
// NOLINTEND(readability/boost)
#include "nlohmann/json_fwd.hpp"
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:
explicit 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",
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_