constantly.at

home

A small REST API

01 Aug 2016

The toy at work grew a small REST API in the last week. Where by “REST API” I don’t mean REST, but more “listens to HTTP requests and spits back JSON at you”.

The basic idea is that if you create an object with a RESTName annotation, like so:

[RESTName: "foo"] new C();

then a GET to /o/foo/attr will give you the value of the attr field of that object, at whatever point in time the system gets around to answering the request. Similarly, GETting /o/foo spits out a map of all the fields and their values as a JSON thingy, and just /o gives a list of registered object tags.

Over the weekend and today, I implemented method calls in a similar way. Methods need to be preregistered in the interface definition:

[RESTCallable] List<String> doit(Int count);

whereby the type checker will complain if any argument dares to be something besides a String, Int or Bool since I did not want to implement a full-blown parser right now. The return type can be anything it wants since it gets shunted through toString() before being delivered to the caller.

GET /call/foo/doit?count=15 will call the method; GET /call/foo sends back a list of callable methods, with return type and list of parameter names + types.

This all is courtesy of the quite nice cowboy web server and jsx JSON library, who did the job with a minimum of fuss. Now that that’s out of the way, I can get back to fixing exception handling, which has been broken for ever..