comparison Framework/Toolbox/OrthancApiClient.cpp @ 315:aad37d0b6407 am-2

Added LayerWidget::RemoveLayer + DELETE commands in WebService
author am@osimis.io
date Fri, 05 Oct 2018 10:38:16 +0200
parents 14ef1227120f
children c34784e5f299
comparison
equal deleted inserted replaced
314:97f16214dc5e 315:aad37d0b6407
118 118
119 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) 119 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
120 } 120 }
121 }; 121 };
122 122
123 // performs the translation between IWebService messages and OrthancApiClient messages
124 // TODO: handle destruction of this object (with shared_ptr ?::delete_later ???)
125 class HttpResponseToEmptyConverter : public IObserver, IObservable
126 {
127 std::auto_ptr<MessageHandler<OrthancApiClient::EmptyResponseReadyMessage>> orthancApiSuccessCallback_;
128 std::auto_ptr<MessageHandler<OrthancApiClient::HttpErrorMessage>> orthancApiFailureCallback_;
129 public:
130 HttpResponseToEmptyConverter(MessageBroker& broker,
131 MessageHandler<OrthancApiClient::EmptyResponseReadyMessage>* orthancApiSuccessCallback,
132 MessageHandler<OrthancApiClient::HttpErrorMessage>* orthancApiFailureCallback)
133 : IObserver(broker),
134 IObservable(broker),
135 orthancApiSuccessCallback_(orthancApiSuccessCallback),
136 orthancApiFailureCallback_(orthancApiFailureCallback)
137 {
138 }
139
140 void ConvertResponseToEmpty(const IWebService::HttpRequestSuccessMessage& message)
141 {
142 if (orthancApiSuccessCallback_.get() != NULL)
143 {
144 orthancApiSuccessCallback_->Apply(OrthancApiClient::EmptyResponseReadyMessage(message.uri_, message.payload_));
145 }
146 else if (orthancApiFailureCallback_.get() != NULL)
147 {
148 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_, message.payload_));
149 }
150
151 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
152 }
153
154 void ConvertError(const IWebService::HttpRequestErrorMessage& message)
155 {
156 if (orthancApiFailureCallback_.get() != NULL)
157 {
158 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_));
159 }
160
161 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
162 }
163 };
164
165
123 void OrthancApiClient::GetJsonAsync(const std::string& uri, 166 void OrthancApiClient::GetJsonAsync(const std::string& uri,
124 MessageHandler<JsonResponseReadyMessage>* successCallback, 167 MessageHandler<JsonResponseReadyMessage>* successCallback,
125 MessageHandler<HttpErrorMessage>* failureCallback, 168 MessageHandler<HttpErrorMessage>* failureCallback,
126 Orthanc::IDynamicObject* payload) 169 Orthanc::IDynamicObject* payload)
127 { 170 {
166 std::string body; 209 std::string body;
167 MessagingToolbox::JsonToString(body, data); 210 MessagingToolbox::JsonToString(body, data);
168 return PostBinaryAsyncExpectJson(uri, body, successCallback, failureCallback, payload); 211 return PostBinaryAsyncExpectJson(uri, body, successCallback, failureCallback, payload);
169 } 212 }
170 213
214 void OrthancApiClient::DeleteAsync(const std::string& uri,
215 MessageHandler<EmptyResponseReadyMessage>* successCallback,
216 MessageHandler<HttpErrorMessage>* failureCallback,
217 Orthanc::IDynamicObject* payload)
218 {
219 HttpResponseToEmptyConverter* converter = new HttpResponseToEmptyConverter(broker_, successCallback, failureCallback); // it is currently deleting itself after being used
220 orthanc_.DeleteAsync(uri, IWebService::Headers(), payload,
221 new Callable<HttpResponseToEmptyConverter, IWebService::HttpRequestSuccessMessage>(*converter, &HttpResponseToEmptyConverter::ConvertResponseToEmpty),
222 new Callable<HttpResponseToEmptyConverter, IWebService::HttpRequestErrorMessage>(*converter, &HttpResponseToEmptyConverter::ConvertError));
223 }
224
171 225
172 } 226 }