From a2da05749117b680a31bb17cd447ca4cd4b07277 Mon Sep 17 00:00:00 2001 From: Jaycee Lock <5284743+yassiezar@users.noreply.github.com> Date: Fri, 5 Jul 2024 08:31:48 +0100 Subject: [PATCH] =?UTF-8?q?Moved=20HTTP=20client=20behaviour=20to=20client?= =?UTF-8?q?=20library.=20Also=20added=20GET=20and=20POS=E2=80=A6=20(#549)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Moved HTTP client behaviour to client library. Also added GET and POST-specific versions of the smae HTTP client behaviour to make them easier to use * Added copyrights * Fixed typos --- .../http_client/CMakeLists.txt | 2 +- .../{http_client.hpp => cl_http_client.hpp} | 4 -- .../client_behaviors/cb_http_get_request.hpp | 34 +++++++++++ .../client_behaviors/cb_http_post_request.hpp | 34 +++++++++++ .../client_behaviors/cb_http_request.hpp | 60 +++++++++++++++++++ .../{http_client.cpp => cl_http_client.cpp} | 2 +- .../client_behaviors/cb_http_request.hpp | 40 ++++++------- .../sm_atomic_http/orthogonals/or_http.hpp | 2 +- .../sm_atomic_http/states/st_state_2.hpp | 16 ++--- 9 files changed, 156 insertions(+), 38 deletions(-) rename smacc2_client_library/http_client/include/http_client/{http_client.hpp => cl_http_client.hpp} (97%) create mode 100644 smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_get_request.hpp create mode 100644 smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_post_request.hpp create mode 100644 smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_request.hpp rename smacc2_client_library/http_client/src/http_client/{http_client.cpp => cl_http_client.cpp} (98%) diff --git a/smacc2_client_library/http_client/CMakeLists.txt b/smacc2_client_library/http_client/CMakeLists.txt index 248dde3e8..db0f9219e 100644 --- a/smacc2_client_library/http_client/CMakeLists.txt +++ b/smacc2_client_library/http_client/CMakeLists.txt @@ -27,7 +27,7 @@ add_library(http_session ) add_library(${PROJECT_NAME} - src/http_client/http_client.cpp + src/http_client/cl_http_client.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/smacc2_client_library/http_client/include/http_client/http_client.hpp b/smacc2_client_library/http_client/include/http_client/cl_http_client.hpp similarity index 97% rename from smacc2_client_library/http_client/include/http_client/http_client.hpp rename to smacc2_client_library/http_client/include/http_client/cl_http_client.hpp index 9dbe0c3b8..1b45d41b6 100644 --- a/smacc2_client_library/http_client/include/http_client/http_client.hpp +++ b/smacc2_client_library/http_client/include/http_client/cl_http_client.hpp @@ -28,14 +28,10 @@ #include #include #include -#include -#include -#include #include #include #include #include -#include namespace cl_http { diff --git a/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_get_request.hpp b/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_get_request.hpp new file mode 100644 index 000000000..096950f35 --- /dev/null +++ b/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_get_request.hpp @@ -0,0 +1,34 @@ +// Copyright 2023 RobosoftAI Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/***************************************************************************************************************** + * + * Authors: Jaycee Lock + * + ******************************************************************************************************************/ + +#pragma once + +#include +#include +#include + +namespace cl_http +{ +class CbHttpGetRequest : public CbHttpRequestBase +{ +public: + CbHttpGetRequest() : CbHttpRequestBase(ClHttp::kHttpRequestMethod::GET) {} +}; +} // namespace cl_http diff --git a/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_post_request.hpp b/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_post_request.hpp new file mode 100644 index 000000000..5ce07808e --- /dev/null +++ b/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_post_request.hpp @@ -0,0 +1,34 @@ +// Copyright 2023 RobosoftAI Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/***************************************************************************************************************** + * + * Authors: Jaycee Lock + * + ******************************************************************************************************************/ + +#pragma once + +#include +#include +#include + +namespace cl_http +{ +class CbHttpPostRequest : public CbHttpRequestBase +{ +public: + CbHttpPostRequest() : CbHttpRequestBase(ClHttp::kHttpRequestMethod::POST) {} +}; +} // namespace cl_http diff --git a/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_request.hpp b/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_request.hpp new file mode 100644 index 000000000..689a3338c --- /dev/null +++ b/smacc2_client_library/http_client/include/http_client/client_behaviors/cb_http_request.hpp @@ -0,0 +1,60 @@ +// Copyright 2023 RobosoftAI Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/***************************************************************************************************************** + * + * Authors: Jaycee Lock + * + ******************************************************************************************************************/ + +#pragma once + +#include +#include +#include + +namespace cl_http +{ + +class CbHttpRequestBase : public smacc2::SmaccClientBehavior +{ +public: + CbHttpRequestBase(const ClHttp::kHttpRequestMethod http_request_type) + : kRequestType(http_request_type) + { + } + + template + void onOrthogonalAllocation() + { + } + + virtual void runtimeConfigure() override + { + this->requiresClient(cl_http_); + cl_http_->onResponseReceived(&CbHttpRequestBase::onResponseReceived, this); + } + + virtual void onResponseReceived(const ClHttp::TResponse & response) {} + + virtual void onEntry() override { cl_http_->makeRequest(kRequestType); } + + virtual void onExit() override {} + +private: + const ClHttp::kHttpRequestMethod kRequestType; + + ClHttp * cl_http_; +}; +} // namespace cl_http diff --git a/smacc2_client_library/http_client/src/http_client/http_client.cpp b/smacc2_client_library/http_client/src/http_client/cl_http_client.cpp similarity index 98% rename from smacc2_client_library/http_client/src/http_client/http_client.cpp rename to smacc2_client_library/http_client/src/http_client/cl_http_client.cpp index ea430d4cc..0dcbb0bd6 100644 --- a/smacc2_client_library/http_client/src/http_client/http_client.cpp +++ b/smacc2_client_library/http_client/src/http_client/cl_http_client.cpp @@ -18,7 +18,7 @@ * ******************************************************************************************************************/ -#include +#include namespace cl_http { 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 8523eae18..d6161c35e 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 @@ -21,45 +21,39 @@ #pragma once #include -#include +#include #include -namespace sm_atomic_http { +namespace sm_atomic_http +{ template -struct EvHttp : sc::event> {}; +struct EvHttp : sc::event> +{ +}; -class CbHttpRequest : public smacc2::SmaccClientBehavior { - public: +class CbHttpRequest : public cl_http::CbHttpGetRequest +{ +public: template - void onOrthogonalAllocation() { - triggerTranstition = [this]() { + void onOrthogonalAllocation() + { + triggerTranstition = [this]() + { auto event = new EvHttp(); this->postEvent(event); }; } - void runtimeConfigure() override { - this->requiresClient(cl_http_); - cl_http_->onResponseReceived(&CbHttpRequest::onResponseReceived, this); - } - - void onResponseReceived(const cl_http::ClHttp::TResponse& response) { + void onResponseReceived(const cl_http::ClHttp::TResponse & response) + { RCLCPP_INFO_STREAM(this->getLogger(), "ON RESPONSE"); RCLCPP_INFO_STREAM(this->getLogger(), response.body()); triggerTranstition(); } - void onEntry() override { - RCLCPP_INFO(this->getLogger(), "On Entry!"); - - cl_http_->makeRequest(cl_http::ClHttp::kHttpRequestMethod::GET); - } - - void onExit() override { RCLCPP_INFO(this->getLogger(), "Cb on exit!"); } - - private: - cl_http::ClHttp* cl_http_; +private: + cl_http::ClHttp * cl_http_; std::function triggerTranstition; }; diff --git a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/orthogonals/or_http.hpp b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/orthogonals/or_http.hpp index 136f28a31..f411ceaa6 100644 --- a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/orthogonals/or_http.hpp +++ b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/orthogonals/or_http.hpp @@ -14,7 +14,7 @@ #pragma once -#include +#include #include namespace sm_atomic_http { diff --git a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/states/st_state_2.hpp b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/states/st_state_2.hpp index af4b9934e..8a509b349 100644 --- a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/states/st_state_2.hpp +++ b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/states/st_state_2.hpp @@ -14,20 +14,20 @@ #include -namespace sm_atomic_http { +namespace sm_atomic_http +{ + // STATE DECLARATION -struct State2 : smacc2::SmaccState { +struct State2 : smacc2::SmaccState +{ using SmaccState::SmaccState; // TRANSITION TABLE - typedef mpl::list< - Transition, State1, SUCCESS> > - reactions; + typedef mpl::list, State1, SUCCESS>> + reactions; // STATE FUNCTIONS - static void staticConfigure() { - configure_orthogonal(); - } + static void staticConfigure() { configure_orthogonal(); } void runtimeConfigure() { RCLCPP_INFO(getLogger(), "Entering State2"); }