Skip to content

Commit

Permalink
Merge pull request #3495 from zeha/forjeeves
Browse files Browse the repository at this point in the history
Add /api discovery endpoint
  • Loading branch information
Habbie committed Mar 15, 2016
2 parents 4f2dc4f + 9e6d203 commit 80ace7a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
17 changes: 16 additions & 1 deletion docs/markdown/httpapi/api_spec.md
Expand Up @@ -96,14 +96,29 @@ 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
------------

Allowed methods: `GET`

{
"server_url": "/api/v1/servers{/server}",
"api_features": [],
"api_features": []
}

**TODO**:
Expand Down
16 changes: 16 additions & 0 deletions pdns/ws-api.cc
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions pdns/ws-api.hh
Expand Up @@ -25,6 +25,7 @@
#include <map>
#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);
Expand Down
1 change: 1 addition & 0 deletions pdns/ws-auth.cc
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions pdns/ws-recursor.cc
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions 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'}])

0 comments on commit 80ace7a

Please sign in to comment.