SMACC2
Loading...
Searching...
No Matches
cl_http_client.hpp
Go to the documentation of this file.
1// Copyright 2023 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Jaycee Lock
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
23#include <boost/asio/executor_work_guard.hpp>
24#include <boost/asio/strand.hpp>
25#include <boost/beast/core.hpp>
26#include <boost/beast/http.hpp>
27#include <boost/beast/ssl.hpp>
28#include <boost/beast/version.hpp>
31#include <smacc2/smacc.hpp>
33#include <string>
34#include <thread>
35
36namespace cl_http
37{
39{
40 class Server
41 {
42 public:
43 explicit Server(const std::string & server_name) : server_name_{server_name}, ssl_{true}
44 {
45 if (!server_name_.substr(0, 7).compare("http://"))
46 {
47 ssl_ = false;
48 server_name_.erase(0, 7);
49 }
50 else if (!server_name_.substr(0, 8).compare("https://"))
51 {
52 server_name_.erase(0, 8);
53 ssl_ = true;
54 }
55 }
56
57 bool isSSL() const { return ssl_; }
58
59 std::string getPort() const { return ssl_ ? "443" : "80"; }
60
61 std::string getServerName() const { return server_name_; }
62
63 private:
64 std::string server_name_;
65 bool ssl_;
66 };
67
68public:
70 {
71 GET = static_cast<int>(boost::beast::http::verb::get),
72 POST = static_cast<int>(boost::beast::http::verb::post),
73 PUT = static_cast<int>(boost::beast::http::verb::put),
74 };
75
77
78 template <typename T>
79 boost::signals2::connection onResponseReceived(void (T::*callback)(const TResponse &), T * object)
80 {
81 return this->getStateMachine()->createSignalConnection(onResponseReceived_, callback, object);
82 }
83
84 explicit ClHttp(const std::string & server, const int & timeout = 1500);
85
86 virtual ~ClHttp();
87
88 void onInitialize() override;
89
90 void makeRequest(
91 const kHttpRequestMethod http_method, const std::string & path = "/",
92 const std::string & body = "",
93 const std::unordered_map<std::string, std::string> & headers = {});
94
95private:
96 const int HTTP_VERSION = 11;
97
99 bool is_ssl_;
101
103
104 boost::asio::io_context io_context_;
105 boost::asio::executor_work_guard<decltype(io_context_)::executor_type> worker_guard_;
107
108 boost::asio::ssl::context ssl_context_;
109
111
112 std::function<void(TResponse)> callbackHandler = [&](const TResponse & res)
113 { onResponseReceived_(res); };
114};
115} // namespace cl_http
std::string getServerName() const
std::string getPort() const
Server(const std::string &server_name)
std::thread tcp_connection_runner_
void makeRequest(const kHttpRequestMethod http_method, const std::string &path="/", const std::string &body="", const std::unordered_map< std::string, std::string > &headers={})
boost::asio::io_context io_context_
http_session_base::TResponse TResponse
boost::asio::executor_work_guard< decltype(io_context_)::executor_type > worker_guard_
std::function< void(TResponse)> callbackHandler
void onInitialize() override
boost::asio::ssl::context ssl_context_
boost::signals2::connection onResponseReceived(void(T::*callback)(const TResponse &), T *object)
smacc2::SmaccSignal< void(const TResponse &)> onResponseReceived_
boost::beast::http::response< boost::beast::http::string_body > TResponse
ISmaccStateMachine * getStateMachine()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)