* 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;
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>());
}
}
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,
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>());
}
}
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>());
+ }
}
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);
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);
}
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)