examples: annotate examples
[nit.git] / lib / nitcorn / restful.nit
index e46bf13..a7580a4 100644 (file)
@@ -32,7 +32,7 @@
 # Arguments that are `nullable` are optional,
 # if they are missing `null` is passed to the `restful` method.
 #
-# The annotation accepts two kinds of arguments, in any order:
+# The annotation accepts three kinds of arguments, in any order:
 #
 # * String literals rename or add an alias for the HTTP resource.
 #   By default, the name of the HTTP resource is the name of the `restful` method.
 # * Ids such as `GET`, `POST`, `PUT` and `DELETE` restrict which HTTP methods
 #   are accepted. By default, all HTTP methods are accepted.
 #
+# * The `async` keyword triggers executing calls to this service asynchronously
+#   by the `thread_pool` attribute of the `RestfulAction`.
+#   By default, each call are executed on the same thread in a FIFO order.
+#
 # See the example at `lib/nitcorn/examples/restful_annot.nit` or
 # a real world use case at `contrib/benitlux/src/server/benitlux_controller.nit`.
 #
@@ -53,8 +57,8 @@
 module restful is new_annotation(restful)
 
 import nitcorn
-import json
-import pthreads
+private import json
+import pthreads::threadpool
 
 # Action with `restful` methods
 class RestfulAction
@@ -62,18 +66,18 @@ class RestfulAction
 
        redef fun answer(request, truncated_uri) do return new HttpResponse(400)
 
-       # Service to deserialize arguments from JSON
+       # Deserialize `val` from JSON for a parameter typed by `static_type`
        #
        # 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
+       protected fun deserialize_arg(val: nullable String, static_type: String): nullable Object
        do
                if val == null then return null
 
                var deserializer = new JsonDeserializer(val)
-               var obj = deserializer.deserialize
+               var obj = deserializer.deserialize(static_type)
 
                if deserializer.errors.not_empty then
                        print_error deserializer.errors.join("\n")
@@ -82,6 +86,9 @@ class RestfulAction
 
                return obj
        end
+
+       # Thread pool used by methods annotated with `restful(async)`
+       var thread_pool = new ThreadPool is writable
 end
 
 # Thread dedicated to a single `request`