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/http/status.hpp>
26 #include <boost/beast/http/verb.hpp>
27 
28 #include <memory>
29 #include <mutex>
30 #include <string>
31 #include <thread>
32 #include <utility>
33 
34 namespace caffa
35 {
36 struct AppInfo;
37 class ObjectHandle;
38 } // namespace caffa
39 
40 namespace http = boost::beast::http; // from <boost/beast/http.hpp>
41 
42 namespace caffa::rpc
43 {
44 class RestClient : public Client
45 {
46 public:
47  RestClient( caffa::Session::Type sessionType,
48  const std::string& hostname,
49  int port = 50000,
50  const std::string& username = "",
51  const std::string& password = "" );
52  ~RestClient() override;
53 
54  caffa::AppInfo appInfo() const override;
55  std::shared_ptr<caffa::ObjectHandle> document( const std::string& documentId ) const override;
56  std::vector<std::shared_ptr<caffa::ObjectHandle>> documents() const;
57  std::string execute( caffa::not_null<const caffa::ObjectHandle*> selfObject,
58  const std::string& methodName,
59  const std::string& jsonArguments ) const override;
60  bool stopServer() override;
61  void sendKeepAlive() override;
62 
68  caffa::Session::Type checkSession() const override;
69  void changeSession( caffa::Session::Type newType ) override;
70  void destroySession() override;
71  const std::string& sessionUuid() const override;
72  void startKeepAliveThread() override;
73 
74  std::shared_ptr<caffa::ObjectHandle> getShallowCopyOfChildObject( const caffa::ObjectHandle* objectHandle,
75  const std::string& fieldName ) const override;
76 
77  std::shared_ptr<caffa::ObjectHandle> getDeepCopyOfChildObject( const caffa::ObjectHandle* objectHandle,
78  const std::string& fieldName ) const override;
79 
80  void deepCopyChildObjectFrom( const caffa::ObjectHandle* objectHandle,
81  const std::string& fieldName,
82  const caffa::ObjectHandle* childObject ) 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 createSession( caffa::Session::Type type );
102 
103  void setJson( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, const nlohmann::json& value ) override;
104  nlohmann::json getJson( const caffa::ObjectHandle*, const std::string& fieldName ) const override;
105 
106  std::pair<http::status, std::string> performRequest( http::verb verb,
107  const std::string& hostname,
108  int port,
109  const std::string& target,
110  const std::string& body ) const;
111  std::pair<http::status, std::string>
112  performGetRequest( const std::string& hostname, int port, const std::string& target ) const;
113 
114 private:
115  std::string m_sessionUuid;
116  std::unique_ptr<std::thread> m_keepAliveThread;
117  mutable std::mutex m_sessionMutex;
118 
119  std::string m_username;
120  std::string m_password;
121 };
122 
123 } // namespace caffa::rpc
Definition: cafObjectHandle.h:55
Definition: cafRpcClient.h:38
Definition: cafRestClient.h:45
caffa::AppInfo appInfo() const override
Retrieve Application information.
Definition: cafRestClient.cpp:258
bool stopServer() override
Tell the server to stop operation. Returns a simple boolean status where true is ok.
Definition: cafRestClient.cpp:356
std::string execute(caffa::not_null< const caffa::ObjectHandle * > selfObject, const std::string &methodName, const std::string &jsonArguments) const override
Execute a general non-streaming method.
Definition: cafRestClient.cpp:333
std::vector< std::shared_ptr< caffa::ObjectHandle > > documents() const
Retrieve all top level documents.
Definition: cafRestClient.cpp:300
void setChildObject(const caffa::ObjectHandle *objectHandle, const std::string &fieldName, const caffa::ObjectHandle *childObject) override
TODO: add support for index and replace.
Definition: cafRestClient.cpp:681
std::shared_ptr< caffa::ObjectHandle > document(const std::string &documentId) const override
Retrieve a top level document (project)
Definition: cafRestClient.cpp:274
caffa::Session::Type checkSession() const override
Check the session. Will return a session type (including possibly INVALID) if the session exists....
Definition: cafRestClient.cpp:467
Main Caffa namespace.
Definition: __init__.py:1
Basic Application Information.
Definition: cafApplication.h:39