lib/nitcorn: intro the restful annotation an support module
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 29 Nov 2015 00:56:07 +0000 (19:56 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Sun, 29 Nov 2015 05:08:38 +0000 (00:08 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/nitcorn/restful.nit [new file with mode: 0644]

diff --git a/lib/nitcorn/restful.nit b/lib/nitcorn/restful.nit
new file mode 100644 (file)
index 0000000..bccfd2c
--- /dev/null
@@ -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