Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/OrthancApiClient.h @ 377:8eb4fe74000f
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 05 Nov 2018 16:18:27 +0100 |
parents | c2e040ea8fbe |
children | dd4c7e82b4be |
rev | line source |
---|---|
270 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #pragma once | |
23 | |
24 #include <boost/shared_ptr.hpp> | |
25 #include <json/json.h> | |
26 | |
27 #include "IWebService.h" | |
28 #include "../Messages/IObservable.h" | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
29 #include "../Messages/Promise.h" |
270 | 30 |
31 namespace OrthancStone | |
32 { | |
377 | 33 class OrthancApiClient : public IObservable |
270 | 34 { |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
35 public: |
377 | 36 class JsonResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready> |
270 | 37 { |
377 | 38 private: |
39 const std::string& uri_; | |
40 const Json::Value& json_; | |
41 const Orthanc::IDynamicObject* payload_; | |
270 | 42 |
377 | 43 public: |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
44 JsonResponseReadyMessage(const std::string& uri, |
377 | 45 const Json::Value& json, |
46 const Orthanc::IDynamicObject* payload) : | |
47 uri_(uri), | |
48 json_(json), | |
49 payload_(payload) | |
270 | 50 { |
51 } | |
52 | |
377 | 53 const std::string& GetUri() const |
54 { | |
55 return uri_; | |
56 } | |
57 | |
58 const Json::Value& GetJson() const | |
59 { | |
60 return json_; | |
61 } | |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
62 |
377 | 63 bool HasPayload() const |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
64 { |
377 | 65 return payload_ != NULL; |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
66 } |
377 | 67 |
68 const Orthanc::IDynamicObject& GetPayload() const; | |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
69 }; |
377 | 70 |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
71 |
377 | 72 class BinaryResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready> |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
73 { |
377 | 74 private: |
75 const std::string& uri_; | |
76 const void* answer_; | |
77 size_t answerSize_; | |
78 const Orthanc::IDynamicObject* payload_; | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
79 |
377 | 80 public: |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
81 BinaryResponseReadyMessage(const std::string& uri, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
82 const void* answer, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
83 size_t answerSize, |
377 | 84 const Orthanc::IDynamicObject* payload) : |
85 uri_(uri), | |
86 answer_(answer), | |
87 answerSize_(answerSize), | |
88 payload_(payload) | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
89 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
90 } |
377 | 91 |
92 const std::string& GetUri() const | |
93 { | |
94 return uri_; | |
95 } | |
96 | |
97 const void* GetAnswer() const | |
98 { | |
99 return answer_; | |
100 } | |
101 | |
102 size_t GetAnswerSize() const | |
103 { | |
104 return answerSize_; | |
105 } | |
106 | |
107 bool HasPayload() const | |
108 { | |
109 return payload_ != NULL; | |
110 } | |
111 | |
112 const Orthanc::IDynamicObject& GetPayload() const; | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
113 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
114 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
115 |
377 | 116 class EmptyResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericEmptyResponse_Ready> |
117 { | |
118 private: | |
119 const std::string& uri_; | |
120 const Orthanc::IDynamicObject* payload_; | |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
121 |
377 | 122 public: |
123 EmptyResponseReadyMessage(const std::string& uri, | |
124 const Orthanc::IDynamicObject* payload) : | |
125 uri_(uri), | |
126 payload_(payload) | |
127 { | |
128 } | |
129 | |
130 const std::string& GetUri() const | |
131 { | |
132 return uri_; | |
133 } | |
134 | |
135 bool HasPayload() const | |
136 { | |
137 return payload_ != NULL; | |
138 } | |
139 | |
140 const Orthanc::IDynamicObject& GetPayload() const; | |
141 }; | |
270 | 142 |
377 | 143 |
144 class HttpErrorMessage : public BaseMessage<MessageType_OrthancApi_GenericHttpError_Ready> | |
270 | 145 { |
377 | 146 private: |
147 const std::string& uri_; | |
148 const Orthanc::IDynamicObject* payload_; | |
149 | |
150 public: | |
151 HttpErrorMessage(const std::string& uri, | |
152 const Orthanc::IDynamicObject* payload) : | |
153 uri_(uri), | |
154 payload_(payload) | |
155 { | |
156 } | |
157 | |
158 const std::string& GetUri() const | |
159 { | |
160 return uri_; | |
161 } | |
162 | |
163 bool HasPayload() const | |
164 { | |
165 return payload_ != NULL; | |
166 } | |
167 | |
168 const Orthanc::IDynamicObject& GetPayload() const; | |
270 | 169 }; |
170 | |
377 | 171 |
270 | 172 protected: |
173 IWebService& orthanc_; | |
174 | |
175 public: | |
176 OrthancApiClient(MessageBroker& broker, | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
177 IWebService& orthanc); |
377 | 178 |
179 virtual ~OrthancApiClient() | |
180 { | |
181 } | |
270 | 182 |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
183 // schedule a GET request expecting a JSON response. |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
184 void GetJsonAsync(const std::string& uri, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
185 MessageHandler<JsonResponseReadyMessage>* successCallback, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
186 MessageHandler<HttpErrorMessage>* failureCallback = NULL, |
377 | 187 Orthanc::IDynamicObject* payload = NULL /* takes ownership */); |
270 | 188 |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
189 // schedule a GET request expecting a binary response. |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
190 void GetBinaryAsync(const std::string& uri, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
191 const std::string& contentType, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
192 MessageHandler<BinaryResponseReadyMessage>* successCallback, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
193 MessageHandler<HttpErrorMessage>* failureCallback = NULL, |
377 | 194 Orthanc::IDynamicObject* payload = NULL /* takes ownership */) |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
195 { |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
196 IWebService::Headers headers; |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
197 headers["Accept"] = contentType; |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
198 GetBinaryAsync(uri, headers, successCallback, failureCallback, payload); |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
199 } |
270 | 200 |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
201 // schedule a GET request expecting a binary response. |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
202 void GetBinaryAsync(const std::string& uri, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
203 const IWebService::Headers& headers, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
204 MessageHandler<BinaryResponseReadyMessage>* successCallback, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
205 MessageHandler<HttpErrorMessage>* failureCallback = NULL, |
377 | 206 Orthanc::IDynamicObject* payload = NULL /* takes ownership */); |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
207 |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
208 // schedule a POST request expecting a JSON response. |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
209 void PostBinaryAsyncExpectJson(const std::string& uri, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
210 const std::string& body, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
211 MessageHandler<JsonResponseReadyMessage>* successCallback, |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
299
diff
changeset
|
212 MessageHandler<HttpErrorMessage>* failureCallback = NULL, |
377 | 213 Orthanc::IDynamicObject* payload = NULL /* takes ownership */); |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
214 |
309 | 215 // schedule a POST request expecting a JSON response. |
216 void PostJsonAsyncExpectJson(const std::string& uri, | |
217 const Json::Value& data, | |
218 MessageHandler<JsonResponseReadyMessage>* successCallback, | |
219 MessageHandler<HttpErrorMessage>* failureCallback = NULL, | |
377 | 220 Orthanc::IDynamicObject* payload = NULL /* takes ownership */); |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
271
diff
changeset
|
221 |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
222 // schedule a DELETE request expecting an empty response. |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
223 void DeleteAsync(const std::string& uri, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
224 MessageHandler<EmptyResponseReadyMessage>* successCallback, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
225 MessageHandler<HttpErrorMessage>* failureCallback = NULL, |
377 | 226 Orthanc::IDynamicObject* payload = NULL /* takes ownership */); |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
227 |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
228 |
270 | 229 }; |
230 } |