doc.Accept(w);
return string(output.GetString(), output.Size());
}
-
-string returnJsonError(const string& error)
-{
- Document doc;
- doc.SetObject();
- Value jerror(error.c_str(), doc.GetAllocator()); // copy
- doc.AddMember("error", jerror, doc.GetAllocator());
- return makeStringFromDocument(doc);
-}
-
-/* success response */
-string returnJsonMessage(const string& message)
-{
- Document doc;
- doc.SetObject();
- Value jmessage;
- jmessage.SetString(message.c_str());
- doc.AddMember("result", jmessage, doc.GetAllocator());
- return makeStringFromDocument(doc);
-}
#include "rapidjson/document.h"
#include "json11.hpp"
-std::string returnJsonError(const std::string& error);
-std::string returnJsonMessage(const std::string& message);
std::string makeStringFromDocument(const rapidjson::Document& doc);
int intFromJson(const rapidjson::Value& container, const char* key);
int intFromJson(const rapidjson::Value& container, const char* key, const int default_value);
document.dump(this->body);
}
+void HttpResponse::setErrorResult(const std::string& message, const int status)
+{
+ setBody(json11::Json::object { { "error", message } });
+ this->status = status;
+}
+
+void HttpResponse::setSuccessResult(const std::string& message, const int status)
+{
+ setBody(json11::Json::object { { "result", message } });
+ this->status = status;
+}
+
static void bareHandlerWrapper(WebServer::HandlerFunction handler, YaHTTP::Request* req, YaHTTP::Response* resp)
{
// wrapper to convert from YaHTTP::* to our subclasses
resp->status = 200;
handler(req, resp);
} catch (ApiException &e) {
- resp->body = returnJsonError(e.what());
- resp->status = 422;
+ resp->setErrorResult(e.what(), 422);
return;
} catch (JsonException &e) {
- resp->body = returnJsonError(e.what());
- resp->status = 422;
+ resp->setErrorResult(e.what(), 422);
return;
}
resp.body = "<!html><title>" + what + "</title><h1>" + what + "</h1>";
} else if (req.accept_json) {
resp.headers["Content-Type"] = "application/json";
- resp.body = returnJsonError(what);
+ resp.setErrorResult(what, resp.status);
} else {
resp.headers["Content-Type"] = "text/plain; charset=utf-8";
resp.body = what;
void setBody(rapidjson::Document& document);
void setBody(const json11::Json& document);
+ void setErrorResult(const std::string& message, const int status);
+ void setSuccessResult(const std::string& message, const int status = 200);
};
random_shuffle(di.masters.begin(), di.masters.end());
Communicator.addSuckRequest(zonename, di.masters.front());
- resp->body = returnJsonMessage("Added retrieval request for '"+zonename.toString()+"' from master "+di.masters.front());
+ resp->setSuccessResult("Added retrieval request for '"+zonename.toString()+"' from master "+di.masters.front());
}
static void apiServerZoneNotify(HttpRequest* req, HttpResponse* resp) {
if(!Communicator.notifyDomain(zonename))
throw ApiException("Failed to add to the queue - see server log");
- resp->body = returnJsonMessage("Notification queued");
+ resp->setSuccessResult("Notification queued");
}
static void makePtr(const DNSResourceRecord& rr, DNSResourceRecord* ptr) {
resp->setBody(Json::object { { "entries", entries } });
return;
} else {
- resp->status = 404;
- resp->body = returnJsonError("Command '"+command+"' not found");
+ resp->setErrorResult("Command '"+command+"' not found", 404);
}
}