GoPxL SDK
GoRestClient.h
Go to the documentation of this file.
1 
10 #ifndef GO_PXL_SDK_GORESTCLIENT_H
11 #define GO_PXL_SDK_GORESTCLIENT_H
12 
13 #include <kApi/Io/kNetwork.h>
14 
15 #include <GoPxLSdk/GoJson.h>
16 #include <GoPxLSdk/GoResponse.h>
17 #include <GoPxLSdk/GoTransaction.h>
20 
21 class GoRestClientTests;
22 
23 namespace GoPxLSdk
24 {
29 using GoNotificationHandler = std::function<void(const std::shared_ptr<GoNotificationResponse>&)>;
30 
35 using GoStreamHandler = std::function<void(const std::shared_ptr<GoStreamResponse>&)>;
36 
38 {
39 public:
46  GoRestClient();
47 
48  ~GoRestClient() = default;
49 
59  void Connect(kIpAddress address, k16u port = 0);
60 
68  void Disconnect();
69 
77  bool IsConnected() const;
78 
91  GoTransaction Read(const std::string& uri, const GoJson& content = {}, const GoJson& args = {});
92 
105  GoTransaction Update(const std::string& uri, const GoJson& content = {}, const GoJson& args = {});
106 
118  GoTransaction Create(const std::string& uri, const GoJson& content = {}, const GoJson& args = {});
119 
132  GoTransaction Delete(const std::string& uri, const GoJson& content = {}, const GoJson& args = {});
133 
145  GoTransaction Call(const std::string& uri, const GoJson& content = {}, const GoJson& args = {});
146 
160  GoTransaction Sub(const std::string& uri, const GoJson& content = {}, const GoJson& args = {});
161 
173  GoTransaction UnSub(const std::string& uri, const GoJson& content = {}, const GoJson& args = {});
174 
182  void SetSubHandler(const GoNotificationHandler& callback);
183 
197  GoTransaction StartStream(const std::string & uri, const GoJson& content = {}, const GoJson& args = {});
198 
210  GoTransaction StopStream(const std::string& uri, const GoJson& content = {}, const GoJson& args = {});
211 
219  void SetStreamHandler(const GoStreamHandler& callback);
220 
228  void SetReadErrorHandler(const GoCtrlChannelReadErrorHandler& callback);
229 
230 private:
231  void ResponseHandler(std::shared_ptr<GoResponse> response);
232 
233  void WriteErrorHandler(k64u requestId, const std::exception& e);
234 
235  void Callback(std::shared_ptr<GoResponse> response);
236 
240  std::mutex commandMutex;
241 
242  // Using std::deque instead of std::queue, because it provides iterator used by `HandleReadError` and `HandleWriteError`.
243  std::deque<GoTransaction> transactionQueue;
244 
245  std::shared_ptr<GoCtrlChannel> channel;
246 
247  GoStreamHandler streamHandler;
248  GoNotificationHandler notificationHandler;
249 
250  friend class ::GoRestClientTests;
251 };
252 
253 }
254 
255 #endif
Simplify working with json data.
Definition: GoJson.h:39
std::function< void(const std::shared_ptr< GoStreamResponse > &)> GoStreamHandler
Data handler used in GoRestClient::StartStream.
Definition: GoRestClient.h:35
#define GoPxLSdkClass
Definition: Def.h:35
Declares the GoPxLSdk.GoCtrlChannel class.
Declares the GoPxLSdk.GoResponse class.
std::function< void(const std::shared_ptr< GoNotificationResponse > &)> GoNotificationHandler
Notification handler used in GoRestClient::Sub.
Definition: GoRestClient.h:29
Declares the GoPxLSdk.GoCtrlChannelV1 class.
Definition: GoRestClient.h:37
Declares the GoPxLSdk.GoTransaction class.
Declares the GoPxLSdk.GoJson class.
Definition: GoTransaction.h:24
Definition: Def.h:46
std::function< void(ReadErrorType readErrorType, const std::exception &e)> GoCtrlChannelReadErrorHandler
Function to receive read thread errors.
Definition: GoCtrlChannel.h:45