From f461cf07b45aff63a48e5861b88991a07633c4b2 Mon Sep 17 00:00:00 2001 From: Christian Hofstaedtler Date: Mon, 28 Dec 2015 01:54:19 +0100 Subject: [PATCH] API: port /api/v1/servers{/localhost} to json11 --- pdns/ws-api.cc | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/pdns/ws-api.cc b/pdns/ws-api.cc index a24b7774e9..80a283c17c 100644 --- a/pdns/ws-api.cc +++ b/pdns/ws-api.cc @@ -36,6 +36,7 @@ #include extern string s_programname; +using json11::Json; #ifndef HAVE_STRCASESTR @@ -80,29 +81,23 @@ strcasestr(const char *s1, const char *s2) using namespace rapidjson; -static void fillServerDetail(Value& out, Value::AllocatorType& allocator) -{ - Value jdaemonType(productTypeApiType().c_str(), allocator); - out.SetObject(); - out.AddMember("type", "Server", allocator); - out.AddMember("id", "localhost", allocator); - out.AddMember("url", "/api/v1/servers/localhost", allocator); - out.AddMember("daemon_type", jdaemonType, allocator); - Value jversion(getPDNSVersion().c_str(), allocator); - out.AddMember("version", jversion, allocator); - out.AddMember("config_url", "/api/v1/servers/localhost/config{/config_setting}", allocator); - out.AddMember("zones_url", "/api/v1/servers/localhost/zones{/zone}", allocator); +static Json getServerDetail() { + return Json::object { + { "type", "Server" }, + { "id", "localhost" }, + { "url", "/api/v1/servers/localhost" }, + { "daemon_type", productTypeApiType() }, + { "version", getPDNSVersion() }, + { "config_url", "/api/v1/servers/localhost/config{/config_setting}" }, + { "zones_url", "/api/v1/servers/localhost/zones{/zone}" } + }; } void apiServer(HttpRequest* req, HttpResponse* resp) { if(req->method != "GET") throw HttpMethodNotAllowedException(); - Document doc; - doc.SetArray(); - Value server; - fillServerDetail(server, doc.GetAllocator()); - doc.PushBack(server, doc.GetAllocator()); + Json doc = Json::array {getServerDetail()}; resp->setBody(doc); } @@ -110,9 +105,7 @@ void apiServerDetail(HttpRequest* req, HttpResponse* resp) { if(req->method != "GET") throw HttpMethodNotAllowedException(); - Document doc; - fillServerDetail(doc, doc.GetAllocator()); - resp->setBody(doc); + resp->setBody(getServerDetail()); } void apiServerConfig(HttpRequest* req, HttpResponse* resp) { -- 2.47.2