Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafRestClient.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) 2023- Kontur AS
5 //
6 // GNU Lesser General Public License Usage
7 // This library is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation; either version 2.1 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
17 // for more details.
18 //
19 #pragma once
20 
21 #include "cafNotNull.h"
22 #include "cafRpcClient.h"
23 #include "cafSession.h"
24 
25 #include <boost/beast/core.hpp>
26 #include <boost/beast/http/status.hpp>
27 #include <boost/beast/http/verb.hpp>
28 
29 #include <future>
30 #include <memory>
31 #include <mutex>
32 #include <string>
33 #include <thread>
34 #include <utility>
35 
36 namespace caffa
37 {
38 struct AppInfo;
39 class ObjectHandle;
40 } // namespace caffa
41 
42 namespace http = boost::beast::http; // from <boost/beast/http.hpp>
43 
44 namespace boost::asio
45 {
46 class io_context;
47 }
48 
49 namespace caffa::rpc
50 {
51 class RestClient : public Client
52 {
53 public:
54  RestClient( const std::string& hostname, int port = 50000 );
55 
56  ~RestClient() override;
57 
58  caffa::AppInfo appInfo() const override;
59  std::shared_ptr<caffa::ObjectHandle> document( const std::string& documentId ) const override;
60  std::vector<std::shared_ptr<caffa::ObjectHandle>> documents() const override;
61  std::string execute( caffa::not_null<const caffa::ObjectHandle*> selfObject,
62  const std::string& methodName,
63  const std::string& jsonArguments ) const override;
64  bool stopServer() override;
65  void sendKeepAlive() override;
66  bool isReady( caffa::Session::Type type ) const override;
67 
68  void createSession( caffa::Session::Type type, const std::string& username = "", const std::string& password = "" ) override;
69 
75  caffa::Session::Type checkSession() const override;
76  void changeSession( caffa::Session::Type newType ) override;
77  void destroySession() override;
78  const std::string& sessionUuid() const override;
79  void startKeepAliveThread() override;
80 
81  std::shared_ptr<caffa::ObjectHandle> getChildObject( const caffa::ObjectHandle* objectHandle,
82  const std::string& fieldName ) const override;
83 
84  std::vector<std::shared_ptr<caffa::ObjectHandle>> getChildObjects( const caffa::ObjectHandle* objectHandle,
85  const std::string& fieldName ) const override;
86 
87  void setChildObject( const caffa::ObjectHandle* objectHandle,
88  const std::string& fieldName,
89  const caffa::ObjectHandle* childObject ) override;
90 
91  void removeChildObject( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, size_t index ) override;
92 
93  void clearChildObjects( const caffa::ObjectHandle* objectHandle, const std::string& fieldName ) override;
94 
95  void insertChildObject( const caffa::ObjectHandle* objectHandle,
96  const std::string& fieldName,
97  size_t index,
98  const caffa::ObjectHandle* childObject ) override;
99 
100 private:
101  void setJson( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, const nlohmann::json& value ) override;
102  nlohmann::json getJson( const caffa::ObjectHandle*, const std::string& fieldName ) const override;
103 
104  std::pair<http::status, std::string> performRequest( http::verb verb,
105  const std::string& hostname,
106  int port,
107  const std::string& target,
108  const std::string& body,
109  const std::string& username = "",
110  const std::string& password = "" ) const;
111  std::pair<http::status, std::string> performGetRequest( const std::string& hostname,
112  int port,
113  const std::string& target,
114  const std::string& username = "",
115  const std::string& password = "" ) const;
116 
117 private:
118  std::string m_sessionUuid;
119  std::unique_ptr<std::thread> m_keepAliveThread;
120  mutable std::mutex m_sessionMutex;
121 
122  // The io_context is required for all I/O
123  std::shared_ptr<boost::asio::io_context> m_ioc;
124  mutable std::shared_ptr<boost::beast::tcp_stream> m_stream;
125 };
126 
127 } // namespace caffa::rpc
Definition: cafRestClient.h:44
Basic Application Information.
Definition: cafApplication.h:38
Definition: cafRpcClient.h:37
Definition: cafObjectHandle.h:47
Definition: cafRestClient.h:51
Main Caffa namespace.
Definition: cafApplication.h:30
Definition: cafRestAppService.h:28