From f570875c8b6e9026818cad37f74655fb0155e0ac Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 30 Sep 2015 08:49:30 +0200 Subject: [PATCH] Rename api.cpp/api.hpp to apiclient.cpp/apiclient.hpp refs #10042 --- icinga-studio/CMakeLists.txt | 2 +- icinga-studio/apiclient.cpp | 169 +++++++++++++++++------------------ icinga-studio/apiclient.hpp | 11 +-- icinga-studio/mainform.cpp | 6 +- icinga-studio/mainform.hpp | 4 +- 5 files changed, 92 insertions(+), 100 deletions(-) diff --git a/icinga-studio/CMakeLists.txt b/icinga-studio/CMakeLists.txt index 40394a24c..d05076de6 100644 --- a/icinga-studio/CMakeLists.txt +++ b/icinga-studio/CMakeLists.txt @@ -27,7 +27,7 @@ endif() add_executable(icinga-studio MACOSX_BUNDLE WIN32 icinga-studio.cpp forms.cpp aboutform.cpp connectform.cpp mainform.cpp - icinga.icns api.cpp ${WindowsSources}) + icinga.icns apiclient.cpp ${WindowsSources}) include_directories(${Boost_INCLUDE_DIRS}) target_link_libraries(icinga-studio ${Boost_LIBRARIES} ${wxWidgets_LIBRARIES} base remote) diff --git a/icinga-studio/apiclient.cpp b/icinga-studio/apiclient.cpp index 2d0747efc..4be3e241b 100644 --- a/icinga-studio/apiclient.cpp +++ b/icinga-studio/apiclient.cpp @@ -17,14 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#include "icinga-studio/api.hpp" +#include "icinga-studio/apiclient.hpp" #include "remote/base64.hpp" #include "base/json.hpp" #include "base/logger.hpp" #include "base/exception.hpp" #include "base/convert.hpp" #include -#include using namespace icinga; @@ -46,13 +45,7 @@ void ApiClient::GetTypes(const TypesCompletionCallback& callback) const req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password)); m_Connection->SubmitRequest(req, boost::bind(TypesHttpCompletionCallback, _1, _2, callback)); } catch (const std::exception& ex) { - callback(std::vector()); - - std::string message = "HTTP request (" + url->Format() + ") failed."; - wxMessageBox(message); - - Log(LogCritical, "ApiClient") - << "HTTP request failed: " << DiagnosticInformation(ex); + callback(boost::current_exception(), std::vector()); } } @@ -68,36 +61,38 @@ void ApiClient::TypesHttpCompletionCallback(HttpRequest& request, HttpResponse& while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0) body += String(buffer, buffer + count); - std::vector types; + try { + if (response.StatusCode < 200 || response.StatusCode > 299) { + std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body; - if (response.StatusCode < 200 || response.StatusCode > 299) { - std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body; - - wxMessageBox(message); - } else { - try { - result = JsonDecode(body); - - Array::Ptr results = result->Get("results"); - - ObjectLock olock(results); - BOOST_FOREACH(const Dictionary::Ptr typeInfo, results) - { - ApiType::Ptr type = new ApiType();; - type->Abstract = typeInfo->Get("abstract"); - type->BaseName = typeInfo->Get("base"); - type->Name = typeInfo->Get("name"); - type->PluralName = typeInfo->Get("plural_name"); - // TODO: attributes - types.push_back(type); - } - } catch (const std::exception& ex) { - Log(LogCritical, "ApiClient") - << "Error while decoding response: " << DiagnosticInformation(ex); + BOOST_THROW_EXCEPTION(ScriptError(message)); } + + std::vector types; + + result = JsonDecode(body); + + Array::Ptr results = result->Get("results"); + + ObjectLock olock(results); + BOOST_FOREACH(const Dictionary::Ptr typeInfo, results) + { + ApiType::Ptr type = new ApiType();; + type->Abstract = typeInfo->Get("abstract"); + type->BaseName = typeInfo->Get("base"); + type->Name = typeInfo->Get("name"); + type->PluralName = typeInfo->Get("plural_name"); + // TODO: attributes + types.push_back(type); + } + + callback(boost::exception_ptr(), types); + } catch (const std::exception& ex) { + Log(LogCritical, "ApiClient") + << "Error while decoding response: " << DiagnosticInformation(ex); + callback(boost::current_exception(), std::vector()); } - callback(types); } void ApiClient::GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback, @@ -129,13 +124,7 @@ void ApiClient::GetObjects(const String& pluralType, const ObjectsCompletionCall req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password)); m_Connection->SubmitRequest(req, boost::bind(ObjectsHttpCompletionCallback, _1, _2, callback)); } catch (const std::exception& ex) { - callback(std::vector()); - - std::string message = "HTTP request (" + pUrl->Format() + ") failed."; - wxMessageBox(message); - - Log(LogCritical, "ApiClient") - << "HTTP request failed: " << DiagnosticInformation(ex); + callback(boost::current_exception(), std::vector()); } } @@ -151,54 +140,56 @@ void ApiClient::ObjectsHttpCompletionCallback(HttpRequest& request, while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0) body += String(buffer, buffer + count); - std::vector objects; + try { + if (response.StatusCode < 200 || response.StatusCode > 299) { + std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body; - if (response.StatusCode < 200 || response.StatusCode > 299) { - Log(LogCritical, "ApiClient") - << "Failed HTTP request; Code: " << response.StatusCode << "; Body: " << body; - } else { - try { - result = JsonDecode(body); - - Array::Ptr results = result->Get("results"); - - if (results) { - ObjectLock olock(results); - BOOST_FOREACH(const Dictionary::Ptr objectInfo, results) - { - ApiObject::Ptr object = new ApiObject(); - - Dictionary::Ptr attrs = objectInfo->Get("attrs"); - - { - ObjectLock olock(attrs); - BOOST_FOREACH(const Dictionary::Pair& kv, attrs) - { - object->Attrs[kv.first] = kv.second; - } - } - - Array::Ptr used_by = objectInfo->Get("used_by"); - - { - ObjectLock olock(used_by); - BOOST_FOREACH(const Dictionary::Ptr& refInfo, used_by) - { - ApiObjectReference ref; - ref.Name = refInfo->Get("name"); - ref.Type = refInfo->Get("type"); - object->UsedBy.push_back(ref); - } - } - - objects.push_back(object); - } - } - } catch (const std::exception& ex) { - Log(LogCritical, "ApiClient") - << "Error while decoding response: " << DiagnosticInformation(ex); + BOOST_THROW_EXCEPTION(ScriptError(message)); } - } - callback(objects); + std::vector objects; + + result = JsonDecode(body); + + Array::Ptr results = result->Get("results"); + + if (results) { + ObjectLock olock(results); + BOOST_FOREACH(const Dictionary::Ptr objectInfo, results) + { + ApiObject::Ptr object = new ApiObject(); + + Dictionary::Ptr attrs = objectInfo->Get("attrs"); + + { + ObjectLock olock(attrs); + BOOST_FOREACH(const Dictionary::Pair& kv, attrs) + { + object->Attrs[kv.first] = kv.second; + } + } + + Array::Ptr used_by = objectInfo->Get("used_by"); + + { + ObjectLock olock(used_by); + BOOST_FOREACH(const Dictionary::Ptr& refInfo, used_by) + { + ApiObjectReference ref; + ref.Name = refInfo->Get("name"); + ref.Type = refInfo->Get("type"); + object->UsedBy.push_back(ref); + } + } + + objects.push_back(object); + } + } + + callback(boost::exception_ptr(), objects); + } catch (const std::exception& ex) { + Log(LogCritical, "ApiClient") + << "Error while decoding response: " << DiagnosticInformation(ex); + callback(boost::current_exception(), std::vector()); + } } diff --git a/icinga-studio/apiclient.hpp b/icinga-studio/apiclient.hpp index c3bb0351a..a4ab2283c 100644 --- a/icinga-studio/apiclient.hpp +++ b/icinga-studio/apiclient.hpp @@ -17,11 +17,12 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#ifndef API_H -#define API_H +#ifndef APICLIENT_H +#define APICLIENT_H #include "remote/httpclientconnection.hpp" #include "base/value.hpp" +#include "base/exception.hpp" #include namespace icinga @@ -87,10 +88,10 @@ public: ApiClient(const String& host, const String& port, const String& user, const String& password); - typedef boost::function&)> TypesCompletionCallback; + typedef boost::function&)> TypesCompletionCallback; void GetTypes(const TypesCompletionCallback& callback) const; - typedef boost::function&)> ObjectsCompletionCallback; + typedef boost::function&)> ObjectsCompletionCallback; void GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback, const std::vector& names = std::vector(), const std::vector& attrs = std::vector()) const; @@ -108,4 +109,4 @@ private: } -#endif /* API_H */ \ No newline at end of file +#endif /* APICLIENT_H */ diff --git a/icinga-studio/mainform.cpp b/icinga-studio/mainform.cpp index 7763e4f90..3d1437cda 100644 --- a/icinga-studio/mainform.cpp +++ b/icinga-studio/mainform.cpp @@ -39,7 +39,7 @@ MainForm::MainForm(wxWindow *parent, const Url::Ptr& url) port = "5665"; m_ApiClient = new ApiClient(url->GetHost(), port, url->GetUsername(), url->GetPassword()); - m_ApiClient->GetTypes(boost::bind(&MainForm::TypesCompletionHandler, this, _1, true)); + m_ApiClient->GetTypes(boost::bind(&MainForm::TypesCompletionHandler, this, _2, true)); std::string title = url->Format() + " - Icinga Studio"; SetTitle(title); @@ -101,7 +101,7 @@ void MainForm::OnTypeSelected(wxTreeEvent& event) std::vector attrs; attrs.push_back(type->Name.ToLower() + ".__name"); - m_ApiClient->GetObjects(type->PluralName, boost::bind(&MainForm::ObjectsCompletionHandler, this, _1, true), + m_ApiClient->GetObjects(type->PluralName, boost::bind(&MainForm::ObjectsCompletionHandler, this, _2, true), std::vector(), attrs); } @@ -150,7 +150,7 @@ void MainForm::OnObjectSelected(wxListEvent& event) std::vector names; names.push_back(objectName); - m_ApiClient->GetObjects(type->PluralName, boost::bind(&MainForm::ObjectDetailsCompletionHandler, this, _1, true), names); + m_ApiClient->GetObjects(type->PluralName, boost::bind(&MainForm::ObjectDetailsCompletionHandler, this, _2, true), names); } wxPGProperty *MainForm::ValueToProperty(const String& name, const Value& value) diff --git a/icinga-studio/mainform.hpp b/icinga-studio/mainform.hpp index a961edba0..2837e7f0b 100644 --- a/icinga-studio/mainform.hpp +++ b/icinga-studio/mainform.hpp @@ -20,7 +20,7 @@ #ifndef MAINFORM_H #define MAINFORM_H -#include "icinga-studio/api.hpp" +#include "icinga-studio/apiclient.hpp" #include "remote/url.hpp" #include "icinga-studio/forms.h" @@ -50,4 +50,4 @@ private: } -#endif /* MAINFORM_H */ \ No newline at end of file +#endif /* MAINFORM_H */