Merge: Intro of nitrestful a RESTful API generator
authorJean Privat <jean@pryen.org>
Fri, 4 Dec 2015 00:01:47 +0000 (19:01 -0500)
committerJean Privat <jean@pryen.org>
Fri, 4 Dec 2015 00:01:47 +0000 (19:01 -0500)
commit9214a0edea2ff8038911e82c5a3d3b44ea004c5c
treeaac7fdfeb3fe40135a250969b8a0a0c3adeef6c4
parentaa72915522a6e0ef89c8125ff561492ba9234af5
parentf5714f5d20f488b5f3734218bed51b7c5dd7f224
Merge: Intro of nitrestful a RESTful API generator

This tool generate a Nit module which implements `Action::answer` to redirect request to a static Nit method. It checks the presence of args, their types, deserialize objects as needed and calls the target method.

Missing arguments or arguments with errors (wrong type, failed deserialization, etc.) are replaced by `null` when the corresponding parameter is `nullable`. If the parameter is non-nullable, or if there is any other error, the generated code calls `super` from `answer` for the user code to handle exceptions and errors.

With the `restful` annotation we can write a normal method with static types such as: (from the example)
~~~
# User code
class MyAction
super RestfulAction

# Method answering requests like `foo?s=some_string&i=42&b=true`
fun foo(s: String, i: Int, b: Bool): HttpResponse is restful do
var resp = new HttpResponse(200)
resp.body = "foo {s} {i} {b}"
return resp
end
...
~~~

And nitrestful will generate for use the wrapper extracting the args from the request and call the method `foo`:
~~~
# Generated code by `nitrestful`
redef class MyAction
redef fun answer(request, truncated_uri)
do
var verbs = truncated_uri.split("/")
if verbs.not_empty and verbs.first.is_empty then verbs.shift
if verbs.length != 1 then return super
var verb = verbs.first

if verb == "foo" then
var in_s = request.string_arg("s")
var out_s = in_s

var in_i = request.string_arg("i")
var out_i = deserialize_arg(in_i)

var in_b = request.string_arg("b")
var out_b = deserialize_arg(in_b)

if not out_s isa String or not out_i isa Int or not out_b isa Bool then
return super
end
return foo(out_s, out_i, out_b)
end
return super
end
end
~~~

This is an early version of this tool. More work is needed to test different types, different usages, error management, and improve the docs and examples.

close #1852

Pull-Request: #1863
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Romain Chanoir <romain.chanoir@viacesi.fr>
Reviewed-by: Jean-Philippe Caissy <jpcaissy@piji.ca>