-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
17 changed files
with
473 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/restful_annot_gen.nit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
all: | ||
all: bin/restful_annot | ||
mkdir -p bin/ | ||
../../../bin/nitc --dir bin src/nitcorn_hello_world.nit src/simple_file_server.nit | ||
|
||
xymus.net: | ||
mkdir -p bin/ | ||
../../../bin/nitc --dir bin/ src/xymus_net.nit | ||
|
||
pre-build: src/restful_annot_gen.nit | ||
src/restful_annot_gen.nit: | ||
../../../bin/nitrestful -o $@ src/restful_annot.nit | ||
|
||
bin/restful_annot: src/restful_annot_gen.nit | ||
mkdir -p bin/ | ||
../../../bin/nitc -o $@ src/restful_annot_gen.nit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# This file is part of NIT ( http://www.nitlanguage.org ). | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import nitcorn::restful | ||
|
||
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 | ||
|
||
# Method answering requests like `bar?s=these_arguments_are_optional` | ||
fun bar(s: nullable String, i: nullable Int, b: nullable Bool): HttpResponse | ||
is restful do | ||
var resp = new HttpResponse(200) | ||
resp.body = "bar {s or else "null"} {i or else "null"} {b or else "null"}" | ||
return resp | ||
end | ||
end | ||
|
||
var vh = new VirtualHost("localhost:8080") | ||
|
||
# Serve everything with our restful action | ||
vh.routes.add new Route(null, new MyAction) | ||
|
||
# Avoid executing when running tests | ||
if "NIT_TESTING".environ == "true" then exit 0 | ||
|
||
var factory = new HttpFactory.and_libevent | ||
factory.config.virtual_hosts.add vh | ||
factory.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# This file is part of NIT ( http://www.nitlanguage.org ). | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Support module for the `nitrestful` tool and the `restful` annotation | ||
module restful is new_annotation(restful) | ||
|
||
import nitcorn | ||
import json::serialization | ||
|
||
# Action with `restful` methods | ||
class RestfulAction | ||
super Action | ||
|
||
redef fun answer(request, truncated_uri) do return new HttpResponse(400) | ||
|
||
# Service to deserialize arguments from JSON | ||
# | ||
# Accepts `nullable String` for convenience, but returns `null` when `val == null`. | ||
# | ||
# This method is called by the code generated by `nitrestful`. | ||
# It can be specialized to customize its behavior. | ||
protected fun deserialize_arg(val: nullable String): nullable Object | ||
do | ||
if val == null then return null | ||
|
||
var deserializer = new JsonDeserializer(val) | ||
if deserializer.errors.not_empty then | ||
print_error deserializer.errors.join("\n") | ||
return null | ||
end | ||
|
||
return deserializer.deserialize | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# NAME | ||
|
||
nitrestful - generates boilerplate code to relay RESTful request to static Nit methods | ||
|
||
# SYNOPSIS | ||
|
||
nitrestful [*options*]... FILE | ||
|
||
# OPTIONS | ||
|
||
### `-o`, `--output` | ||
Output file (can also be 'stdout'). | ||
|
||
### `--dir` | ||
Output directory. | ||
|
||
# SEE ALSO | ||
|
||
The Nit language documentation and the source code of its tools and libraries may be downloaded from <http://nitlanguage.org> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.