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