From: Christian Hofstaedtler Date: Wed, 2 Mar 2016 12:44:26 +0000 (-0300) Subject: Add /api discovery endpoint X-Git-Tag: dnsdist-1.0.0-beta1~108^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e6d203301597f77bf1077e66bf2b9e0b3c66101;p=thirdparty%2Fpdns.git Add /api discovery endpoint Closes #3253. --- diff --git a/docs/markdown/httpapi/api_spec.md b/docs/markdown/httpapi/api_spec.md index 2bf00b655f..ebaa41aa17 100644 --- a/docs/markdown/httpapi/api_spec.md +++ b/docs/markdown/httpapi/api_spec.md @@ -96,6 +96,21 @@ Common Error Causes 3. For requests that operate on a zone, the `zone_id` URL part was invalid. To get a valid `zone_id`, list the zones with the `/api/v1/servers/:server_id/zones` endpoint. +URL: /api +--------- + +Version discovery endpoint. + +Allowed methods: `GET` + + [ + { + "url": "/api/v1", + "version": 1 + } + ] + + URL: /api/v1 ------------ @@ -103,7 +118,7 @@ Allowed methods: `GET` { "server_url": "/api/v1/servers{/server}", - "api_features": [], + "api_features": [] } **TODO**: diff --git a/pdns/ws-api.cc b/pdns/ws-api.cc index 12d3f7417c..bf8f35e7ed 100644 --- a/pdns/ws-api.cc +++ b/pdns/ws-api.cc @@ -91,6 +91,22 @@ static Json getServerDetail() { }; } +/* Return information about the supported API versions. + * The format of this MUST NEVER CHANGE at it's not versioned. + */ +void apiDiscovery(HttpRequest* req, HttpResponse* resp) { + if(req->method != "GET") + throw HttpMethodNotAllowedException(); + + Json version1 = Json::object { + { "version", 1 }, + { "url", "/api/v1" } + }; + Json doc = Json::array { version1 }; + + resp->setBody(doc); +} + void apiServer(HttpRequest* req, HttpResponse* resp) { if(req->method != "GET") throw HttpMethodNotAllowedException(); diff --git a/pdns/ws-api.hh b/pdns/ws-api.hh index 8c94076975..e9dbbf5abc 100644 --- a/pdns/ws-api.hh +++ b/pdns/ws-api.hh @@ -25,6 +25,7 @@ #include #include "webserver.hh" +void apiDiscovery(HttpRequest* req, HttpResponse* resp); void apiServer(HttpRequest* req, HttpResponse* resp); void apiServerDetail(HttpRequest* req, HttpResponse* resp); void apiServerConfig(HttpRequest* req, HttpResponse* resp); diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index a3cf2fce02..c47505ddd2 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -1211,6 +1211,7 @@ void AuthWebServer::webThread() d_ws->registerApiHandler("/api/v1/servers/localhost/zones", &apiServerZones); d_ws->registerApiHandler("/api/v1/servers/localhost", &apiServerDetail); d_ws->registerApiHandler("/api/v1/servers", &apiServer); + d_ws->registerApiHandler("/api", &apiDiscovery); } d_ws->registerWebHandler("/style.css", boost::bind(&AuthWebServer::cssfunction, this, _1, _2)); d_ws->registerWebHandler("/", boost::bind(&AuthWebServer::indexfunction, this, _1, _2)); diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index 5b57957627..8eb17597f8 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -426,6 +426,7 @@ RecursorWebServer::RecursorWebServer(FDMultiplexer* fdm) d_ws->registerApiHandler("/api/v1/servers/localhost/zones", &apiServerZones); d_ws->registerApiHandler("/api/v1/servers/localhost", &apiServerDetail); d_ws->registerApiHandler("/api/v1/servers", &apiServer); + d_ws->registerApiHandler("/api", &apiDiscovery); for(const auto& u : g_urlmap) d_ws->registerWebHandler("/"+u.first, serveStuff); diff --git a/regression-tests.api/test_Discovery.py b/regression-tests.api/test_Discovery.py new file mode 100644 index 0000000000..80d65ad734 --- /dev/null +++ b/regression-tests.api/test_Discovery.py @@ -0,0 +1,10 @@ +from test_helper import ApiTestCase + + +class DiscoveryTest(ApiTestCase): + + def test_discovery(self): + r = self.session.get(self.url("/api")) + self.assert_success_json(r) + lst = r.json() + self.assertEquals(lst, [{'version': 1, 'url': '/api/v1'}])