Skip to content

Commit

Permalink
refactor: move SendRequest from Register to http_client
Browse files Browse the repository at this point in the history
  • Loading branch information
jr0me committed Aug 7, 2024
1 parent d87e110 commit 6fa7170
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 61 deletions.
8 changes: 8 additions & 0 deletions src/agent/communicator/include/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ namespace http_client
const std::string& body = "",
const std::string& user_pass = "");

boost::beast::http::response<boost::beast::http::dynamic_body> SendRequest(const std::string& managerIp,
const std::string& port,
const boost::beast::http::verb& method,
const std::string& url,
const std::string& token,
const std::string& body,
const std::string& user_pass);

boost::asio::awaitable<void>
Co_PerformHttpRequest(boost::asio::ip::tcp::socket& socket,
boost::beast::http::request<boost::beast::http::string_body>& req,
Expand Down
41 changes: 41 additions & 0 deletions src/agent/communicator/src/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,47 @@ namespace http_client
return req;
}

boost::beast::http::response<boost::beast::http::dynamic_body> SendRequest(const std::string& managerIp,
const std::string& port,
const boost::beast::http::verb& method,
const std::string& url,
const std::string& token,
const std::string& body,
const std::string& user_pass)
{
boost::beast::http::response<boost::beast::http::dynamic_body> res;

try
{
boost::asio::io_context io_context;
boost::asio::ip::tcp::resolver resolver(io_context);
auto const results = resolver.resolve(managerIp, port);

boost::asio::ip::tcp::socket socket(io_context);
boost::asio::connect(socket, results.begin(), results.end());

auto req = CreateHttpRequest(method, url, managerIp, token, body, user_pass);

boost::beast::http::write(socket, req);

boost::beast::flat_buffer buffer;

boost::beast::http::read(socket, buffer, res);

std::cout << "Response code: " << res.result_int() << std::endl;
std::cout << "Response body: " << boost::beast::buffers_to_string(res.body().data()) << std::endl;
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
res.result(boost::beast::http::status::internal_server_error);
boost::beast::ostream(res.body()) << "Internal server error: " << e.what();
res.prepare_payload();
}

return res;
}

boost::asio::awaitable<void>
Co_PerformHttpRequest(boost::asio::ip::tcp::socket& socket,
boost::beast::http::request<boost::beast::http::string_body>& req,
Expand Down
64 changes: 3 additions & 61 deletions src/agent/src/register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <agent_info.hpp>
#include <configuration_parser.hpp>
#include <http_client.hpp>

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
Expand All @@ -19,70 +20,11 @@ using json = nlohmann::json;

namespace registration
{
http::response<http::dynamic_body> sendRequest(const std::string& managerIp,
const std::string& port,
const http::verb& method,
const std::string& url,
const std::string& token,
const std::string& body,
const std::string& user_pass)
{
http::response<http::dynamic_body> res;
try
{
boost::asio::io_context io_context;
tcp::resolver resolver(io_context);
auto const results = resolver.resolve(managerIp, port);

tcp::socket socket(io_context);
boost::asio::connect(socket, results.begin(), results.end());

http::request<http::string_body> req {method, url, 11};
req.set(http::field::host, managerIp);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req.set(http::field::accept, "application/json");

if (!token.empty())
{
req.set(http::field::authorization, "Bearer " + token);
}

if (!user_pass.empty())
{
req.set(http::field::authorization, "Basic " + user_pass);
}

if (!body.empty())
{
req.set(http::field::content_type, "application/json");
req.body() = body;
req.prepare_payload();
}

http::write(socket, req);

beast::flat_buffer buffer;

http::read(socket, buffer, res);

std::cout << "Response code: " << res.result_int() << std::endl;
std::cout << "Response body: " << beast::buffers_to_string(res.body().data()) << std::endl;
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
res.result(http::status::internal_server_error);
beast::ostream(res.body()) << "Internal server error: " << e.what();
res.prepare_payload();
}
return res;
}

std::pair<http::status, std::string>
SendAuthenticationRequest(const std::string& managerIp, const std::string& port, const std::string& user_pass)
{
const http::response<http::dynamic_body> res =
sendRequest(managerIp, port, http::verb::post, "/authenticate", "", "", user_pass);
http_client::SendRequest(managerIp, port, http::verb::post, "/authenticate", "", "", user_pass);
const auto token = beast::buffers_to_string(res.body().data());

return {res.result(), token};
Expand All @@ -108,7 +50,7 @@ namespace registration
}

const http::response<http::dynamic_body> res =
sendRequest(managerIp, port, http::verb::post, "/agents", token, bodyJson.dump(), "");
http_client::SendRequest(managerIp, port, http::verb::post, "/agents", token, bodyJson.dump(), "");
return res.result();
}

Expand Down

0 comments on commit 6fa7170

Please sign in to comment.