Rename api.cpp/api.hpp to apiclient.cpp/apiclient.hpp

refs #10042
This commit is contained in:
Gunnar Beutner 2015-09-30 08:49:30 +02:00
parent 7e4953dd35
commit f570875c8b
5 changed files with 92 additions and 100 deletions

View 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)

View File

@ -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 <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;
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<ApiType::Ptr> 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<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;
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<ApiObject::Ptr> 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<ApiObject::Ptr>());
}
}

View File

@ -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 <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 */
#endif /* APICLIENT_H */

View 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)

View 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 */
#endif /* MAINFORM_H */