From ef8f24798b2bc8e1a0ce80dfedd7f2af53a03a85 Mon Sep 17 00:00:00 2001 From: Jaycee Lock Date: Wed, 12 Jul 2023 18:03:51 +0100 Subject: [PATCH] Made provision for HTTP GET/POST in the HTTP client --- .../smacc2/client_bases/smacc_http_client.hpp | 29 +++++++++++-------- .../client_behaviors/cb_http_request.hpp | 3 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/smacc2/include/smacc2/client_bases/smacc_http_client.hpp b/smacc2/include/smacc2/client_bases/smacc_http_client.hpp index 7cafa06c8..0d42bae37 100644 --- a/smacc2/include/smacc2/client_bases/smacc_http_client.hpp +++ b/smacc2/include/smacc2/client_bases/smacc_http_client.hpp @@ -26,19 +26,16 @@ #include #include #include +#include #include #include #include #include +#include namespace smacc2 { namespace client_bases { class SmaccHttpClient : public smacc2::ISmaccClient { - enum class HTTP_REQUEST_TYPE { - GET = 0, - POST, - }; - class http_session : public std::enable_shared_from_this { boost::asio::ip::tcp::resolver resolver_; boost::beast::tcp_stream stream_; @@ -59,11 +56,12 @@ class SmaccHttpClient : public smacc2::ISmaccClient { onResponse{response} {} // Start the asynchronous operation - void run(const std::string &host, const std::string &port, - const std::string &target, const int &version) { - // Set up an HTTP GET request message + void run(const std::string &host, const std::string &target, + const std::string &port, + const boost::beast::http::verb http_method, const int &version) { + // Set up an HTTP request req_.version(version); - req_.method(boost::beast::http::verb::get); + req_.method(http_method); req_.target(target); req_.set(boost::beast::http::field::host, host); req_.set(boost::beast::http::field::user_agent, @@ -147,6 +145,11 @@ class SmaccHttpClient : public smacc2::ISmaccClient { }; public: + enum class kHttpRequestMethod { + GET = static_cast(boost::beast::http::verb::get), + POST = static_cast(boost::beast::http::verb::post), + }; + explicit SmaccHttpClient(const std::string &server, const int &timeout = 1500) : worker_guard_{boost::asio::make_work_guard(io_context_)}, initialized_{false}, @@ -179,9 +182,11 @@ class SmaccHttpClient : public smacc2::ISmaccClient { callback, object); } - void makeRequest(const std::string &path = "/") { + void makeRequest(const kHttpRequestMethod http_method, + const std::string &path = "/") { std::make_shared(io_context_, callbackHandler) - ->run(server_name_, is_ssl_ ? "443" : "80", path, HTTP_VERSION); + ->run(server_name_, path, is_ssl_ ? "443" : "80", + static_cast(http_method), HTTP_VERSION); } private: @@ -199,7 +204,7 @@ class SmaccHttpClient : public smacc2::ISmaccClient { std::function callbackHandler; - smacc2::SmaccSignal onResponseReceived_; + smacc2::SmaccSignal onResponseReceived_; }; } // namespace client_bases } // namespace smacc2 diff --git a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/clients/client_behaviors/cb_http_request.hpp b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/clients/client_behaviors/cb_http_request.hpp index 47aba7360..efc2e33eb 100644 --- a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/clients/client_behaviors/cb_http_request.hpp +++ b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/clients/client_behaviors/cb_http_request.hpp @@ -28,7 +28,8 @@ class CbHttpRequest : public smacc2::SmaccClientBehavior { void onEntry() override { RCLCPP_INFO(getLogger(), "On Entry!"); - cl_http_->makeRequest(); + cl_http_->makeRequest( + smacc2::client_bases::SmaccHttpClient::kHttpRequestMethod::GET); } void onExit() override { RCLCPP_INFO(getLogger(), "Cb on exit!"); }