diff --git a/Modules/CppRestSdk/include/mitkRESTClientMicroService.h b/Modules/CppRestSdk/include/mitkRESTClientMicroService.h index 500dc80359..377d5e0afa 100644 --- a/Modules/CppRestSdk/include/mitkRESTClientMicroService.h +++ b/Modules/CppRestSdk/include/mitkRESTClientMicroService.h @@ -1,39 +1,78 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + #ifndef mitkRESTClientMicroService_h #define mitkRESTClientMicroService_h #include "cpprest/asyncrt_utils.h" #include "cpprest/containerstream.h" -//#include "cpprest/filestream.h" #include "cpprest/http_client.h" #include "cpprest/json.h" #include "cpprest/producerconsumerstream.h" #include "cpprest/uri.h" #include "MitkCppRestSdkExports.h" #include #include #include #include typedef web::http::client::http_client MitkClient; typedef web::http::http_request MitkRequest; typedef web::http::http_response MitkResponse; typedef web::http::methods MitkRESTMethods; typedef web::http::uri_builder MitkUriBuilder; typedef web::http::status_codes MitkRestStatusCodes; typedef web::json::json_exception MitkJsonException; namespace mitk { class RESTClientMicroService { public: RESTClientMicroService(); ~RESTClientMicroService(); - pplx::task Get(web::uri); - pplx::task PUT(web::uri, web::json::value content); - pplx::task POST(web::uri, web::json::value content); + /** + * @brief Executes a HTTP GET request with the given uri and returns a task waiting for a json object + * + * @param uri the URI resulting the target of the HTTP request + * @return task with to wait for with resulting json object + */ + pplx::task Get(web::uri uri); + + /** + * @brief Executes a HTTP PUT request with given uri and the content given as json + * + * @param uri defines the URI resulting the target of the HTTP request + * @param content the content as json value which should be the body of the request and thus the content of the + * created resources + * @return task to wait for with resulting json object + */ + pplx::task PUT(web::uri uri, web::json::value content); + + /** + * @brief Executes a HTTP POST request with given uri and the content given as json + * + * @param uri defines the URI resulting the target of the HTTP request + * @param content the content as json value which should be the body of the request and thus the content of the created resource + * @return task to wait for with resulting json object + */ + pplx::task POST(web::uri uri, web::json::value content); + }; } // namespace mitk #endif // !mitkRESTClientMicroService_h diff --git a/Modules/CppRestSdk/include/mitkRESTServerMicroService.h b/Modules/CppRestSdk/include/mitkRESTServerMicroService.h index e38984b43e..6f9c7723e3 100644 --- a/Modules/CppRestSdk/include/mitkRESTServerMicroService.h +++ b/Modules/CppRestSdk/include/mitkRESTServerMicroService.h @@ -1,47 +1,66 @@ #ifndef mitkRESTServerMicroService_h #define mitkRESTServerMicroService_h #include #include "cpprest/asyncrt_utils.h" #include "cpprest/containerstream.h" #include "cpprest/filestream.h" #include "cpprest/http_listener.h" #include "cpprest/json.h" #include "cpprest/producerconsumerstream.h" #include "cpprest/uri.h" #include "MitkCppRestSdkExports.h" #include #include #include #include typedef web::http::experimental::listener::http_listener MitkListener; typedef web::http::http_request MitkRequest; typedef web::http::http_response MitkResponse; typedef web::http::methods MitkRESTMethods; typedef web::http::status_codes MitkRestStatusCodes; typedef web::json::json_exception MitkJsonException; namespace mitk { class RESTServerMicroService : public QObject { Q_OBJECT public: + /** + * @brief Creates an server listening to the given URI + * + * @param uri the URI at which the server is listening for requests + */ RESTServerMicroService(web::uri uri); ~RESTServerMicroService(); + web::uri GetUri(); private: + /** + * @brief Handle for incoming GET requests + * + * @param MitkRequest incoming request object + */ void HandleGet(MitkRequest request); + MitkListener m_Listener; web::uri m_Uri; public slots: + /** + * @brief Opens the listener and starts the listening process + */ void OpenListener(); + + /** + * @brief Closes the listener and stops the listening process + */ void CloseListener(); }; } // namespace mitk #endif \ No newline at end of file diff --git a/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp b/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp index e179efa84a..c678de2062 100644 --- a/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp +++ b/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp @@ -1,86 +1,86 @@ #include "mitkRESTClientMicroService.h" #include "mitkRESTUtil.h" #include mitk::RESTClientMicroService::RESTClientMicroService() {} mitk::RESTClientMicroService::~RESTClientMicroService() {} pplx::task mitk::RESTClientMicroService::Get(web::uri uri) { MitkClient *client = new MitkClient(uri); MITK_INFO << "Calling GET with " << utility::conversions::to_utf8string(uri.path()) << " on client " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); MitkRequest getRequest(MitkRESTMethods::GET); // getRequest.set_request_uri(uri); - return client->request(getRequest).then([=](MitkResponse response) { + return client->request(getRequest).then([=](pplx::task responseTask) { + MitkResponse response = responseTask.get(); auto status = response.status_code(); MITK_INFO << " status: " << status; - if (status != web::http::status_codes::OK) + if (status != MitkRestStatusCodes::OK) { mitkThrow() << "response was not OK"; } + return response.extract_json().get(); }); // answer.wait(); // return answer.get(); } pplx::task mitk::RESTClientMicroService::PUT(web::uri uri, web::json::value content) { MitkClient *client = new MitkClient(uri); MITK_INFO << "Calling PUT with " << utility::conversions::to_utf8string(uri.path()) << " on client " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); MitkRequest putRequest(MitkRESTMethods::PUT); - // putRequest.set_request_uri(uri); putRequest.set_body(content); + return client->request(putRequest).then([=](pplx::task responseTask) { + try + { + MitkResponse response = responseTask.get(); + auto status = response.status_code(); + MITK_INFO << " status: " << status; + if (status != MitkRestStatusCodes::OK) + { + mitkThrow() << "response was not OK"; + } - return client->request(putRequest).then([=](MitkResponse response) { - auto status = response.status_code(); - MITK_INFO << " status: " << status; - - if (status != web::http::status_codes::OK) + return response.extract_json().get(); + } + catch (...) { - mitkThrow() << "response was not OK"; } - - return response.extract_json().get(); }); } pplx::task mitk::RESTClientMicroService::POST(web::uri uri, web::json::value content) { MitkClient *client = new MitkClient(uri); MITK_INFO << "Calling POST with " << utility::conversions::to_utf8string(uri.path()) << " on client " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); MitkRequest postRequest(MitkRESTMethods::POST); if (content != NULL) { postRequest.set_body(content); } - return client->request(postRequest).then([=](MitkResponse response) { - try - { - auto status = response.status_code(); - MITK_INFO << " status: " << status; - - if (status != web::http::status_codes::Created) - { - mitkThrow() << "response was not OK"; - } + return client->request(postRequest).then([=](pplx::task responseTask) { + MitkResponse response = responseTask.get(); + auto status = response.status_code(); + MITK_INFO << " status: " << status; - return response.extract_json().get(); - } - catch (...) + if (status != MitkRestStatusCodes::Created) { - MITK_WARN << "Exception in Client Class"; + mitkThrow() << "response was not Created"; } + + return response.extract_json().get(); }); } diff --git a/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.cpp b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.cpp index 85440f77e4..a1ad2ee048 100644 --- a/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.cpp +++ b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.cpp @@ -1,153 +1,188 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkClientView.h" #include #include "cpprest/json.h" #include #include #include #include #include #include #include const std::string QmitkClientView::VIEW_ID = "org.mitk.views.clientview"; QmitkClientView::QmitkClientView() : m_Ui(new Ui::QmitkClientView) { mitk::ForceLinkage(); } QmitkClientView::~QmitkClientView() { delete m_Ui; } void QmitkClientView::CreateQtPartControl(QWidget *parent) { m_Ui->setupUi(parent); connect(m_Ui->getPushButton, &QPushButton::clicked, this, &QmitkClientView::OnGetButtonClicked); connect(m_Ui->putPushButton, &QPushButton::clicked, this, &QmitkClientView::OnPutButtonClicked); connect(m_Ui->postPushButton, &QPushButton::clicked, this, &QmitkClientView::OnPostButtonClicked); connect(this, &QmitkClientView::UpdateProgressBar, this, &QmitkClientView::OnUpdateProgressBar); connect(this, SIGNAL(UpdateLabel(QString)), this, SLOT(OnUpdateLabel(QString))); m_Ui->progressBar->setValue(0); } void QmitkClientView::OnUpdateProgressBar() { m_Ui->progressBar->setValue(m_Ui->progressBar->value() + 5); } -void QmitkClientView::OnUpdateLabel(QString text) +void QmitkClientView::OnUpdateLabel(QString text) { m_Ui->responseLabel->setText(text); } void QmitkClientView::SetFocus() {} void QmitkClientView::OnGetButtonClicked() { m_Ui->progressBar->setValue(0); m_Ui->getPushButton->setDisabled(true); us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); auto managerRef = context->GetServiceReference(); if (managerRef) { auto managerService = context->GetService(managerRef); if (managerService) { - std::vector < pplx::task> tasks; + std::vector> tasks; for (int i = 0; i < 20; i++) { - pplx::task singleTask = - managerService->SendRequest(L"https://jsonplaceholder.typicode.com/photos") - .then([=](web::json::value result) - { - emit UpdateProgressBar(); - }); + pplx::task singleTask = managerService->SendRequest(L"https://jsonplaceholder.typicode.com/photos") + .then([=](pplx::task resultTask) { + try + { + resultTask.get(); + emit UpdateProgressBar(); + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }); tasks.push_back(singleTask); } auto joinTask = pplx::when_all(begin(tasks), end(tasks)); - joinTask.then([=]() { emit UpdateLabel("All tasks finished"); - }); + joinTask.then([=](pplx::task resultTask) { + try + { + resultTask.get(); + emit UpdateLabel("All tasks finished"); + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }); m_Ui->responseLabel->setText("Waiting for change"); } } m_Ui->getPushButton->setEnabled(true); } -void QmitkClientView::OnPutButtonClicked() +void QmitkClientView::OnPutButtonClicked() { m_Ui->putPushButton->setDisabled(true); us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); auto managerRef = context->GetServiceReference(); if (managerRef) { auto managerService = context->GetService(managerRef); if (managerService) { web::json::value data; data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a changed title")); data[L"body"] = web::json::value(U("and the body is changed as well")); - managerService->SendRequest(L"https://jsonplaceholder.typicode.com/posts/1", mitk::IRESTManager::RequestType::put, data) - .then([=](web::json::value result) - { - utility::string_t stringT = result.to_string(); - std::string stringStd(stringT.begin(), stringT.end()); - QString stringQ = QString::fromStdString(stringStd); - emit UpdateLabel(stringQ); - }); + managerService->SendRequest( + L"https://jsonplaceholder.typicode.com/posts/1", mitk::IRESTManager::RequestType::put, data) + .then([=](pplx::task resultTask) { + try + { + web::json::value result = resultTask.get(); + utility::string_t stringT = result.to_string(); + std::string stringStd(stringT.begin(), stringT.end()); + QString stringQ = QString::fromStdString(stringStd); + emit UpdateLabel(stringQ); + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }); } } m_Ui->putPushButton->setEnabled(true); } void QmitkClientView::OnPostButtonClicked() { m_Ui->postPushButton->setDisabled(true); us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); auto managerRef = context->GetServiceReference(); if (managerRef) { auto managerService = context->GetService(managerRef); if (managerService) { web::json::value data; data[L"userId"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a new title")); data[L"body"] = web::json::value(U("this is a new body")); - managerService - ->SendRequest(L"https://jsonplaceholder.typicode.com/posts", mitk::IRESTManager::RequestType::post, data) - .then([=](web::json::value result) { - utility::string_t stringT = result.to_string(); - std::string stringStd(stringT.begin(), stringT.end()); - QString stringQ = QString::fromStdString(stringStd); - emit UpdateLabel(stringQ); - }); + managerService + ->SendRequest(L"https://jsonplaceholder.typicode.com/posts", mitk::IRESTManager::RequestType::post, data) + .then([=](pplx::task resultTask) { + try + { + web::json::value result = resultTask.get(); + utility::string_t stringT = result.to_string(); + std::string stringStd(stringT.begin(), stringT.end()); + QString stringQ = QString::fromStdString(stringStd); + emit UpdateLabel(stringQ); + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }); } } m_Ui->postPushButton->setEnabled(true); }