Mercurial > hg > orthanc-stone
view Framework/Toolbox/OrthancApiClient.cpp @ 301:547e1cf7aa7b am-callable-and-promise
cleanup
author | am@osimis.io |
---|---|
date | Tue, 18 Sep 2018 15:34:28 +0200 |
parents | b4abaeb783b1 |
children | 14ef1227120f |
line wrap: on
line source
/** * Stone of Orthanc * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics * Department, University Hospital of Liege, Belgium * Copyright (C) 2017-2018 Osimis S.A., Belgium * * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU Affero General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. **/ #include "OrthancApiClient.h" #include "MessagingToolbox.h" #include <Core/OrthancException.h> namespace OrthancStone { OrthancApiClient::OrthancApiClient(MessageBroker &broker, IWebService &orthanc) : IObservable(broker), orthanc_(orthanc) { } // performs the translation between IWebService messages and OrthancApiClient messages // TODO: handle destruction of this object (with shared_ptr ?::delete_later ???) class HttpResponseToJsonConverter : public IObserver, IObservable { std::auto_ptr<MessageHandler<OrthancApiClient::JsonResponseReadyMessage>> orthancApiSuccessCallback_; std::auto_ptr<MessageHandler<OrthancApiClient::HttpErrorMessage>> orthancApiFailureCallback_; public: HttpResponseToJsonConverter(MessageBroker& broker, MessageHandler<OrthancApiClient::JsonResponseReadyMessage>* orthancApiSuccessCallback, MessageHandler<OrthancApiClient::HttpErrorMessage>* orthancApiFailureCallback) : IObserver(broker), IObservable(broker), orthancApiSuccessCallback_(orthancApiSuccessCallback), orthancApiFailureCallback_(orthancApiFailureCallback) { } void ConvertResponseToJson(const IWebService::HttpRequestSuccessMessage& message) { Json::Value response; if (MessagingToolbox::ParseJson(response, message.answer_, message.answerSize_)) { if (orthancApiSuccessCallback_.get() != NULL) { orthancApiSuccessCallback_->Apply(OrthancApiClient::JsonResponseReadyMessage(message.uri_, response, message.payload_)); } } else if (orthancApiFailureCallback_.get() != NULL) { orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_, message.payload_)); } delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) } void ConvertError(const IWebService::HttpRequestErrorMessage& message) { if (orthancApiFailureCallback_.get() != NULL) { orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_)); } delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) } }; // performs the translation between IWebService messages and OrthancApiClient messages // TODO: handle destruction of this object (with shared_ptr ?::delete_later ???) class HttpResponseToBinaryConverter : public IObserver, IObservable { std::auto_ptr<MessageHandler<OrthancApiClient::BinaryResponseReadyMessage>> orthancApiSuccessCallback_; std::auto_ptr<MessageHandler<OrthancApiClient::HttpErrorMessage>> orthancApiFailureCallback_; public: HttpResponseToBinaryConverter(MessageBroker& broker, MessageHandler<OrthancApiClient::BinaryResponseReadyMessage>* orthancApiSuccessCallback, MessageHandler<OrthancApiClient::HttpErrorMessage>* orthancApiFailureCallback) : IObserver(broker), IObservable(broker), orthancApiSuccessCallback_(orthancApiSuccessCallback), orthancApiFailureCallback_(orthancApiFailureCallback) { } void ConvertResponseToBinary(const IWebService::HttpRequestSuccessMessage& message) { if (orthancApiSuccessCallback_.get() != NULL) { orthancApiSuccessCallback_->Apply(OrthancApiClient::BinaryResponseReadyMessage(message.uri_, message.answer_, message.answerSize_, message.payload_)); } else if (orthancApiFailureCallback_.get() != NULL) { orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_, message.payload_)); } delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) } void ConvertError(const IWebService::HttpRequestErrorMessage& message) { if (orthancApiFailureCallback_.get() != NULL) { orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_)); } delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) } }; void OrthancApiClient::GetJsonAsync(const std::string& uri, MessageHandler<JsonResponseReadyMessage>* successCallback, MessageHandler<HttpErrorMessage>* failureCallback, Orthanc::IDynamicObject* payload) { HttpResponseToJsonConverter* converter = new HttpResponseToJsonConverter(broker_, successCallback, failureCallback); // it is currently deleting itself after being used orthanc_.GetAsync(uri, IWebService::Headers(), payload, new Callable<HttpResponseToJsonConverter, IWebService::HttpRequestSuccessMessage>(*converter, &HttpResponseToJsonConverter::ConvertResponseToJson), new Callable<HttpResponseToJsonConverter, IWebService::HttpRequestErrorMessage>(*converter, &HttpResponseToJsonConverter::ConvertError)); } void OrthancApiClient::GetBinaryAsync(const std::string& uri, const IWebService::Headers& headers, MessageHandler<BinaryResponseReadyMessage>* successCallback, MessageHandler<HttpErrorMessage>* failureCallback, Orthanc::IDynamicObject* payload) { HttpResponseToBinaryConverter* converter = new HttpResponseToBinaryConverter(broker_, successCallback, failureCallback); // it is currently deleting itself after being used orthanc_.GetAsync(uri, headers, payload, new Callable<HttpResponseToBinaryConverter, IWebService::HttpRequestSuccessMessage>(*converter, &HttpResponseToBinaryConverter::ConvertResponseToBinary), new Callable<HttpResponseToBinaryConverter, IWebService::HttpRequestErrorMessage>(*converter, &HttpResponseToBinaryConverter::ConvertError)); } void OrthancApiClient::PostBinaryAsyncExpectJson(const std::string& uri, const std::string& body, MessageHandler<JsonResponseReadyMessage>* successCallback, MessageHandler<HttpErrorMessage>* failureCallback, Orthanc::IDynamicObject* payload) { HttpResponseToJsonConverter* converter = new HttpResponseToJsonConverter(broker_, successCallback, failureCallback); // it is currently deleting itself after being used orthanc_.PostAsync(uri, IWebService::Headers(), body, payload, new Callable<HttpResponseToJsonConverter, IWebService::HttpRequestSuccessMessage>(*converter, &HttpResponseToJsonConverter::ConvertResponseToJson), new Callable<HttpResponseToJsonConverter, IWebService::HttpRequestErrorMessage>(*converter, &HttpResponseToJsonConverter::ConvertError)); } }