]> granicus.if.org Git - icinga2/commitdiff
Rename api.cpp/api.hpp to apiclient.cpp/apiclient.hpp
authorGunnar Beutner <gunnar@beutner.name>
Wed, 30 Sep 2015 06:49:30 +0000 (08:49 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Wed, 30 Sep 2015 06:49:30 +0000 (08:49 +0200)
refs #10042

icinga-studio/CMakeLists.txt
icinga-studio/apiclient.cpp
icinga-studio/apiclient.hpp
icinga-studio/mainform.cpp
icinga-studio/mainform.hpp

index 40394a24c64a344e767b3dda1b79850513370944..d05076de6882847d418a668de5e9ee7a4e8c0dec 100644 (file)
@@ -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)
index 2d0747efc5342af7bb46306d7bbbada6d464d6f3..4be3e241b4b2ef48bbd5ef0d5eba2c7a27a2d38e 100644 (file)
  * 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 <boost/foreach.hpp>
-#include <wx/msgdlg.h>
 
 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<ApiType::Ptr>());
-
-               std::string message = "HTTP request (" + url->Format() + ") failed.";
-               wxMessageBox(message);
-
-               Log(LogCritical, "ApiClient")
-                   << "HTTP request failed: " << DiagnosticInformation(ex);
+               callback(boost::current_exception(), std::vector<ApiType::Ptr>());
        }
 }
 
@@ -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<ApiType::Ptr> 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;
+                       BOOST_THROW_EXCEPTION(ScriptError(message));
+               }
 
-               wxMessageBox(message);
-       } else {
-               try {
-                       result = JsonDecode(body);
+               std::vector<ApiType::Ptr> types;
 
-                       Array::Ptr results = result->Get("results");
+               result = JsonDecode(body);
 
-                       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);
+               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<ApiType::Ptr>());
        }
 
-       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<ApiObject::Ptr>());
-
-               std::string message = "HTTP request (" + pUrl->Format() + ") failed.";
-               wxMessageBox(message);
-
-               Log(LogCritical, "ApiClient")
-                   << "HTTP request failed: " << DiagnosticInformation(ex);
+               callback(boost::current_exception(), std::vector<ApiObject::Ptr>());
        }
 }
 
@@ -151,54 +140,56 @@ void ApiClient::ObjectsHttpCompletionCallback(HttpRequest& request,
        while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
                body += String(buffer, buffer + count);
 
-       std::vector<ApiObject::Ptr> objects;
+       try {
+               if (response.StatusCode < 200 || response.StatusCode > 299) {
+                       std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body;
+
+                       BOOST_THROW_EXCEPTION(ScriptError(message));
+               }
 
-       if (response.StatusCode < 200 || response.StatusCode > 299) {
-               Log(LogCritical, "ApiClient")
-                   <<  "Failed HTTP request; Code: " << response.StatusCode << "; Body: " << body;
-       } else {
-               try {
-                       result = JsonDecode(body);
+               std::vector<ApiObject::Ptr> objects;
 
-                       Array::Ptr results = result->Get("results");
+               result = JsonDecode(body);
 
-                       if (results) {
-                               ObjectLock olock(results);
-                               BOOST_FOREACH(const Dictionary::Ptr objectInfo, results)
-                               {
-                                       ApiObject::Ptr object = new ApiObject();
+               Array::Ptr results = result->Get("results");
 
-                                       Dictionary::Ptr attrs = objectInfo->Get("attrs");
+               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)
                                        {
-                                               ObjectLock olock(attrs);
-                                               BOOST_FOREACH(const Dictionary::Pair& kv, attrs)
-                                               {
-                                                       object->Attrs[kv.first] = kv.second;
-                                               }
+                                               object->Attrs[kv.first] = kv.second;
                                        }
+                               }
 
-                                       Array::Ptr used_by = objectInfo->Get("used_by");
+                               Array::Ptr used_by = objectInfo->Get("used_by");
 
+                               {
+                                       ObjectLock olock(used_by);
+                                       BOOST_FOREACH(const Dictionary::Ptr& refInfo, 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);
-                                               }
+                                               ApiObjectReference ref;
+                                               ref.Name = refInfo->Get("name");
+                                               ref.Type = refInfo->Get("type");
+                                               object->UsedBy.push_back(ref);
                                        }
-
-                                       objects.push_back(object);
                                }
+
+                               objects.push_back(object);
                        }
-               } catch (const std::exception& ex) {
-                       Log(LogCritical, "ApiClient")
-                               << "Error while decoding response: " << DiagnosticInformation(ex);
                }
-       }
 
-       callback(objects);
+               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<ApiObject::Ptr>());
+       }
 }
index c3bb0351a0d10563a0bddfc8ee3eeb0b8e68028f..a4ab2283c3e3de8eb47c3aa0681fc1d914ac62ea 100644 (file)
  * 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 <vector>
 
 namespace icinga
@@ -87,10 +88,10 @@ public:
        ApiClient(const String& host, const String& port,
            const String& user, const String& password);
 
-       typedef boost::function<void(const std::vector<ApiType::Ptr>&)> TypesCompletionCallback;
+       typedef boost::function<void(boost::exception_ptr, const std::vector<ApiType::Ptr>&)> TypesCompletionCallback;
        void GetTypes(const TypesCompletionCallback& callback) const;
 
-       typedef boost::function<void(const std::vector<ApiObject::Ptr>&)> ObjectsCompletionCallback;
+       typedef boost::function<void(boost::exception_ptr, const std::vector<ApiObject::Ptr>&)> ObjectsCompletionCallback;
        void GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback,
            const std::vector<String>& names = std::vector<String>(),
            const std::vector<String>& attrs = std::vector<String>()) const;
@@ -108,4 +109,4 @@ private:
 
 }
 
-#endif /* API_H */
\ No newline at end of file
+#endif /* APICLIENT_H */
index 7763e4f908561894de7a85bf0258fa1cd2c66075..3d1437cda3d6bfe1392c4a989c938058a9eeb12f 100644 (file)
@@ -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<String> 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<String>(), attrs);
 }
 
@@ -150,7 +150,7 @@ void MainForm::OnObjectSelected(wxListEvent& event)
        std::vector<String> 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)
index a961edba0501bf19ebc72d8c4aadc671bb12a76d..2837e7f0bcc1e9b92fcd4cb01e86d4d82b747c80 100644 (file)
@@ -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 */