lib & contrib: update imports
[nit.git] / lib / nitcorn / restful.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Support module for the `nitrestful` tool and the `restful` annotation
16 module restful is new_annotation(restful)
17
18 import nitcorn
19 import json
20
21 # Action with `restful` methods
22 class RestfulAction
23 super Action
24
25 redef fun answer(request, truncated_uri) do return new HttpResponse(400)
26
27 # Service to deserialize arguments from JSON
28 #
29 # Accepts `nullable String` for convenience, but returns `null` when `val == null`.
30 #
31 # This method is called by the code generated by `nitrestful`.
32 # It can be specialized to customize its behavior.
33 protected fun deserialize_arg(val: nullable String): nullable Object
34 do
35 if val == null then return null
36
37 var deserializer = new JsonDeserializer(val)
38 var obj = deserializer.deserialize
39
40 if deserializer.errors.not_empty then
41 print_error deserializer.errors.join("\n")
42 return null
43 end
44
45 return obj
46 end
47 end