Mercurial > hg > orthanc-stone
comparison Framework/Deprecated/Toolbox/OrthancApiClient.cpp @ 732:c35e98d22764
move Deprecated classes to a separate folder
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 21 May 2019 14:27:35 +0200 |
parents | Framework/Toolbox/OrthancApiClient.cpp@4f2416d519b4 |
children | 4fe4b221a31f |
comparison
equal
deleted
inserted
replaced
729:529189f399ec | 732:c35e98d22764 |
---|---|
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-2019 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 #include "OrthancApiClient.h" | |
22 | |
23 #include "../../Toolbox/MessagingToolbox.h" | |
24 | |
25 #include <Core/OrthancException.h> | |
26 | |
27 namespace Deprecated | |
28 { | |
29 const Orthanc::IDynamicObject& OrthancApiClient::JsonResponseReadyMessage::GetPayload() const | |
30 { | |
31 if (HasPayload()) | |
32 { | |
33 return *payload_; | |
34 } | |
35 else | |
36 { | |
37 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
38 } | |
39 } | |
40 | |
41 | |
42 const Orthanc::IDynamicObject& OrthancApiClient::BinaryResponseReadyMessage::GetPayload() const | |
43 { | |
44 if (HasPayload()) | |
45 { | |
46 return *payload_; | |
47 } | |
48 else | |
49 { | |
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
51 } | |
52 } | |
53 | |
54 | |
55 const Orthanc::IDynamicObject& OrthancApiClient::EmptyResponseReadyMessage::GetPayload() const | |
56 { | |
57 if (HasPayload()) | |
58 { | |
59 return *payload_; | |
60 } | |
61 else | |
62 { | |
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
64 } | |
65 } | |
66 | |
67 | |
68 class OrthancApiClient::WebServicePayload : public Orthanc::IDynamicObject | |
69 { | |
70 private: | |
71 std::auto_ptr< OrthancStone::MessageHandler<EmptyResponseReadyMessage> > emptyHandler_; | |
72 std::auto_ptr< OrthancStone::MessageHandler<JsonResponseReadyMessage> > jsonHandler_; | |
73 std::auto_ptr< OrthancStone::MessageHandler<BinaryResponseReadyMessage> > binaryHandler_; | |
74 std::auto_ptr< OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage> > failureHandler_; | |
75 std::auto_ptr< Orthanc::IDynamicObject > userPayload_; | |
76 | |
77 void NotifyConversionError(const IWebService::HttpRequestSuccessMessage& message) const | |
78 { | |
79 if (failureHandler_.get() != NULL) | |
80 { | |
81 failureHandler_->Apply(IWebService::HttpRequestErrorMessage | |
82 (message.GetUri(), userPayload_.get())); | |
83 } | |
84 } | |
85 | |
86 public: | |
87 WebServicePayload(OrthancStone::MessageHandler<EmptyResponseReadyMessage>* handler, | |
88 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler, | |
89 Orthanc::IDynamicObject* userPayload) : | |
90 emptyHandler_(handler), | |
91 failureHandler_(failureHandler), | |
92 userPayload_(userPayload) | |
93 { | |
94 if (handler == NULL) | |
95 { | |
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
97 } | |
98 } | |
99 | |
100 WebServicePayload(OrthancStone::MessageHandler<BinaryResponseReadyMessage>* handler, | |
101 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler, | |
102 Orthanc::IDynamicObject* userPayload) : | |
103 binaryHandler_(handler), | |
104 failureHandler_(failureHandler), | |
105 userPayload_(userPayload) | |
106 { | |
107 if (handler == NULL) | |
108 { | |
109 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
110 } | |
111 } | |
112 | |
113 WebServicePayload(OrthancStone::MessageHandler<JsonResponseReadyMessage>* handler, | |
114 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler, | |
115 Orthanc::IDynamicObject* userPayload) : | |
116 jsonHandler_(handler), | |
117 failureHandler_(failureHandler), | |
118 userPayload_(userPayload) | |
119 { | |
120 if (handler == NULL) | |
121 { | |
122 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
123 } | |
124 } | |
125 | |
126 void HandleSuccess(const IWebService::HttpRequestSuccessMessage& message) const | |
127 { | |
128 if (emptyHandler_.get() != NULL) | |
129 { | |
130 emptyHandler_->Apply(OrthancApiClient::EmptyResponseReadyMessage | |
131 (message.GetUri(), userPayload_.get())); | |
132 } | |
133 else if (binaryHandler_.get() != NULL) | |
134 { | |
135 binaryHandler_->Apply(OrthancApiClient::BinaryResponseReadyMessage | |
136 (message.GetUri(), message.GetAnswer(), | |
137 message.GetAnswerSize(), userPayload_.get())); | |
138 } | |
139 else if (jsonHandler_.get() != NULL) | |
140 { | |
141 Json::Value response; | |
142 if (OrthancStone::MessagingToolbox::ParseJson(response, message.GetAnswer(), message.GetAnswerSize())) | |
143 { | |
144 jsonHandler_->Apply(OrthancApiClient::JsonResponseReadyMessage | |
145 (message.GetUri(), response, userPayload_.get())); | |
146 } | |
147 else | |
148 { | |
149 NotifyConversionError(message); | |
150 } | |
151 } | |
152 else | |
153 { | |
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
155 } | |
156 } | |
157 | |
158 void HandleFailure(const IWebService::HttpRequestErrorMessage& message) const | |
159 { | |
160 if (failureHandler_.get() != NULL) | |
161 { | |
162 failureHandler_->Apply(IWebService::HttpRequestErrorMessage | |
163 (message.GetUri(), userPayload_.get())); | |
164 } | |
165 } | |
166 }; | |
167 | |
168 | |
169 OrthancApiClient::OrthancApiClient(OrthancStone::MessageBroker& broker, | |
170 IWebService& web, | |
171 const std::string& baseUrl) : | |
172 IObservable(broker), | |
173 IObserver(broker), | |
174 web_(web), | |
175 baseUrl_(baseUrl) | |
176 { | |
177 } | |
178 | |
179 | |
180 void OrthancApiClient::GetJsonAsync( | |
181 const std::string& uri, | |
182 OrthancStone::MessageHandler<JsonResponseReadyMessage>* successCallback, | |
183 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
184 Orthanc::IDynamicObject* payload) | |
185 { | |
186 IWebService::HttpHeaders emptyHeaders; | |
187 web_.GetAsync(baseUrl_ + uri, | |
188 emptyHeaders, | |
189 new WebServicePayload(successCallback, failureCallback, payload), | |
190 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> | |
191 (*this, &OrthancApiClient::NotifyHttpSuccess), | |
192 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> | |
193 (*this, &OrthancApiClient::NotifyHttpError)); | |
194 } | |
195 | |
196 | |
197 void OrthancApiClient::GetBinaryAsync( | |
198 const std::string& uri, | |
199 const std::string& contentType, | |
200 OrthancStone::MessageHandler<BinaryResponseReadyMessage>* successCallback, | |
201 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
202 Orthanc::IDynamicObject* payload) | |
203 { | |
204 IWebService::HttpHeaders headers; | |
205 headers["Accept"] = contentType; | |
206 GetBinaryAsync(uri, headers, successCallback, failureCallback, payload); | |
207 } | |
208 | |
209 void OrthancApiClient::GetBinaryAsync( | |
210 const std::string& uri, | |
211 const IWebService::HttpHeaders& headers, | |
212 OrthancStone::MessageHandler<BinaryResponseReadyMessage>* successCallback, | |
213 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
214 Orthanc::IDynamicObject* payload) | |
215 { | |
216 // printf("GET [%s] [%s]\n", baseUrl_.c_str(), uri.c_str()); | |
217 | |
218 web_.GetAsync(baseUrl_ + uri, headers, | |
219 new WebServicePayload(successCallback, failureCallback, payload), | |
220 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> | |
221 (*this, &OrthancApiClient::NotifyHttpSuccess), | |
222 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> | |
223 (*this, &OrthancApiClient::NotifyHttpError)); | |
224 } | |
225 | |
226 | |
227 void OrthancApiClient::PostBinaryAsyncExpectJson( | |
228 const std::string& uri, | |
229 const std::string& body, | |
230 OrthancStone::MessageHandler<JsonResponseReadyMessage>* successCallback, | |
231 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
232 Orthanc::IDynamicObject* payload) | |
233 { | |
234 web_.PostAsync(baseUrl_ + uri, IWebService::HttpHeaders(), body, | |
235 new WebServicePayload(successCallback, failureCallback, payload), | |
236 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> | |
237 (*this, &OrthancApiClient::NotifyHttpSuccess), | |
238 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> | |
239 (*this, &OrthancApiClient::NotifyHttpError)); | |
240 | |
241 } | |
242 | |
243 void OrthancApiClient::PostBinaryAsync( | |
244 const std::string& uri, | |
245 const std::string& body) | |
246 { | |
247 web_.PostAsync(baseUrl_ + uri, IWebService::HttpHeaders(), body, NULL, NULL, NULL); | |
248 } | |
249 | |
250 void OrthancApiClient::PostBinaryAsync( | |
251 const std::string& uri, | |
252 const std::string& body, | |
253 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback, | |
254 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
255 Orthanc::IDynamicObject* payload /* takes ownership */) | |
256 { | |
257 web_.PostAsync(baseUrl_ + uri, IWebService::HttpHeaders(), body, | |
258 new WebServicePayload(successCallback, failureCallback, payload), | |
259 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> | |
260 (*this, &OrthancApiClient::NotifyHttpSuccess), | |
261 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> | |
262 (*this, &OrthancApiClient::NotifyHttpError)); | |
263 } | |
264 | |
265 void OrthancApiClient::PostJsonAsyncExpectJson( | |
266 const std::string& uri, | |
267 const Json::Value& data, | |
268 OrthancStone::MessageHandler<JsonResponseReadyMessage>* successCallback, | |
269 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
270 Orthanc::IDynamicObject* payload) | |
271 { | |
272 std::string body; | |
273 OrthancStone::MessagingToolbox::JsonToString(body, data); | |
274 return PostBinaryAsyncExpectJson(uri, body, successCallback, failureCallback, payload); | |
275 } | |
276 | |
277 void OrthancApiClient::PostJsonAsync( | |
278 const std::string& uri, | |
279 const Json::Value& data) | |
280 { | |
281 std::string body; | |
282 OrthancStone::MessagingToolbox::JsonToString(body, data); | |
283 return PostBinaryAsync(uri, body); | |
284 } | |
285 | |
286 void OrthancApiClient::PostJsonAsync( | |
287 const std::string& uri, | |
288 const Json::Value& data, | |
289 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback, | |
290 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
291 Orthanc::IDynamicObject* payload /* takes ownership */) | |
292 { | |
293 std::string body; | |
294 OrthancStone::MessagingToolbox::JsonToString(body, data); | |
295 return PostBinaryAsync(uri, body, successCallback, failureCallback, payload); | |
296 } | |
297 | |
298 void OrthancApiClient::DeleteAsync( | |
299 const std::string& uri, | |
300 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback, | |
301 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
302 Orthanc::IDynamicObject* payload) | |
303 { | |
304 web_.DeleteAsync(baseUrl_ + uri, IWebService::HttpHeaders(), | |
305 new WebServicePayload(successCallback, failureCallback, payload), | |
306 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> | |
307 (*this, &OrthancApiClient::NotifyHttpSuccess), | |
308 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> | |
309 (*this, &OrthancApiClient::NotifyHttpError)); | |
310 } | |
311 | |
312 | |
313 void OrthancApiClient::NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message) | |
314 { | |
315 if (message.HasPayload()) | |
316 { | |
317 dynamic_cast<const WebServicePayload&>(message.GetPayload()).HandleSuccess(message); | |
318 } | |
319 else | |
320 { | |
321 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
322 } | |
323 } | |
324 | |
325 void OrthancApiClient::NotifyHttpError(const IWebService::HttpRequestErrorMessage& message) | |
326 { | |
327 if (message.HasPayload()) | |
328 { | |
329 dynamic_cast<const WebServicePayload&>(message.GetPayload()).HandleFailure(message); | |
330 } | |
331 else | |
332 { | |
333 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
334 } | |
335 } | |
336 } |