while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
body += String(buffer, buffer + count);
+ if (response.StatusCode < 200 || response.StatusCode > 299) {
+ Log(LogCritical, "ApiClient")
+ << "Failed HTTP request; Code: " << response.StatusCode << "; Body: " << body;
+ return;
+ }
+
std::vector<ApiType::Ptr> types;
try {
void ApiClient::GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback,
const std::vector<String>& names, const std::vector<String>& attrs) const
{
- String url = "https://" + m_Connection->GetHost() + ":" + m_Connection->GetPort() + "/v1/" + pluralType;
+ String url = "https://" + m_Connection->GetHost() + ":" + m_Connection->GetPort() + "/v1/objects/" + pluralType;
String qp;
BOOST_FOREACH(const String& name, names) {
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
body += String(buffer, buffer + count);
+ if (response.StatusCode < 200 || response.StatusCode > 299) {
+ Log(LogCritical, "ApiClient")
+ << "Failed HTTP request; Code: " << response.StatusCode << "; Body: " << body;
+ return;
+ }
+
std::vector<ApiObject::Ptr> objects;
try {
Array::Ptr results = result->Get("results");
- ObjectLock olock(results);
- BOOST_FOREACH(const Dictionary::Ptr objectInfo, results)
- {
- ApiObject::Ptr object = new ApiObject();
+ if (results) {
+ ObjectLock olock(results);
+ BOOST_FOREACH(const Dictionary::Ptr objectInfo, results)
+ {
+ ApiObject::Ptr object = new ApiObject();
- Dictionary::Ptr attrs = objectInfo->Get("attrs");
+ Dictionary::Ptr attrs = objectInfo->Get("attrs");
- {
- ObjectLock olock(attrs);
- BOOST_FOREACH(const Dictionary::Pair& kv, attrs)
{
- object->Attrs[kv.first] = kv.second;
+ ObjectLock olock(attrs);
+ BOOST_FOREACH(const Dictionary::Pair& kv, attrs)
+ {
+ 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)
{
- ApiObjectReference ref;
- ref.Name = refInfo->Get("name");
- ref.Type = refInfo->Get("type");
- object->UsedBy.push_back(ref);
+ 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);
+ objects.push_back(object);
+ }
}
} catch (const std::exception& ex) {
Log(LogCritical, "ApiClient")
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
#endif /* _WIN32 */
- String host, port, user, pass;
+ String port = url->GetPort();
- std::string authority = url->GetAuthority();
-
- std::vector<std::string> tokens;
- boost::algorithm::split(tokens, authority, boost::is_any_of("@"));
-
- if (tokens.size() > 1) {
- std::vector<std::string> userinfo;
- boost::algorithm::split(userinfo, tokens[0], boost::is_any_of(":"));
-
- user = userinfo[0];
- pass = userinfo[1];
- }
-
- std::vector<std::string> hostport;
- boost::algorithm::split(hostport, tokens.size() > 1 ? tokens[1] : tokens[0], boost::is_any_of(":"));
-
- host = hostport[0];
-
- if (hostport.size() > 1)
- port = hostport[1];
- else
+ if (port.IsEmpty())
port = "5665";
- m_ApiClient = new ApiClient(host, port, user, pass);
+ m_ApiClient = new ApiClient(url->GetHost(), port, url->GetUsername(), url->GetPassword());
m_ApiClient->GetTypes(boost::bind(&MainForm::TypesCompletionHandler, this, _1, true));
- std::string title = host;
-
- if (port != "5665")
- title += +":" + port;
-
- title += " - Icinga Studio";
+ std::string title = url->Format() + " - Icinga Studio";
SetTitle(title);
m_ObjectsList->InsertColumn(0, "Name", 0, 300);
return false;
}
- m_Host = auth.SubStr(0, pos-1);
+ m_Host = auth.SubStr(0, pos);
return ValidateToken(m_Host, ACHOST);
}
bool Url::ParseUserinfo(const String& userinfo)
{
size_t pos = userinfo.Find(":");
- m_Username = userinfo.SubStr(0, pos-1);
+ m_Username = userinfo.SubStr(0, pos);
if (!ValidateToken(m_Username, ACUSERINFO))
return false;
m_Username = Utility::UnescapeString(m_Username);
if (pos != String::NPos && pos != userinfo.GetLength() - 1) {
- //Password
m_Password = userinfo.SubStr(pos+1);
if (!ValidateToken(m_Username, ACUSERINFO))
return false;
String key = token.SubStr(0, pHelper);
String value = Empty;
- if (pHelper != token.GetLength()-1)
+ if (pHelper != token.GetLength() - 1)
value = token.SubStr(pHelper+1);
if (!ValidateToken(value, ACQUERY))