]> granicus.if.org Git - pdns/commitdiff
API: port /api/v1/servers{/localhost} to json11
authorChristian Hofstaedtler <christian.hofstaedtler@deduktiva.com>
Mon, 28 Dec 2015 00:54:19 +0000 (01:54 +0100)
committerChristian Hofstaedtler <christian.hofstaedtler@deduktiva.com>
Tue, 29 Dec 2015 22:29:16 +0000 (23:29 +0100)
pdns/ws-api.cc

index a24b7774e9b4a449253067b8eab9c11b48109a96..80a283c17c5604826e3110897d31b264109a66d8 100644 (file)
@@ -36,6 +36,7 @@
 #include <iomanip>
 
 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) {