doc/commands: introduce command initialization from HTTP requests
[nit.git] / src / doc / commands / commands_http.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 # Initialize commands from HTTP requests
16 #
17 # FIXME: this module is pretty tied up to the nitwed routes.
18 # To be more generic, param names should be extracted as variables.
19 module commands_http
20
21 import commands
22 import commands::commands_catalog
23 import nitcorn::vararg_routes
24
25 redef class DocCommand
26 # Init the command from an HTTPRequest
27 fun http_init(req: HttpRequest): CmdMessage do return init_command
28 end
29
30 redef class CmdEntity
31
32
33 redef fun http_init(req) do
34 var name = req.param("id")
35 if name != null then name = name.from_percent_encoding
36 self.mentity_name = name
37
38 return super
39 end
40 end
41
42 redef class CmdList
43 redef fun http_init(req) do
44 limit = req.int_arg("l")
45 page = req.int_arg("p")
46 return super
47 end
48 end
49
50 # Error Handling
51
52 # Message handling
53
54 redef class CmdMessage
55 # HTTP code to return for this message
56 var http_status_code = 200
57 end
58
59 redef class CmdError
60 redef var http_status_code = 400
61 end
62
63 redef class CmdWarning
64 redef var http_status_code = 404
65 end
66
67 redef class ErrorMEntityNoName
68 redef var http_status_code = 400
69 end
70
71 redef class ErrorMEntityNotFound
72 redef var http_status_code = 404
73 end
74
75 redef class ErrorMEntityConflict
76 redef var http_status_code = 300
77 end
78
79 # CmdModel
80
81 redef class CmdComment
82 redef fun http_init(req) do
83 full_doc = req.bool_arg("full_doc") or else true
84 fallback = req.bool_arg("fallback") or else true
85 format = req.string_arg("format") or else "raw"
86 return super
87 end
88 end
89
90 redef class CmdAncestors
91 redef fun http_init(req) do
92 parents = req.bool_arg("parents") or else true
93 return super
94 end
95 end
96
97 redef class CmdDescendants
98 redef fun http_init(req) do
99 children = req.bool_arg("children") or else true
100 return super
101 end
102 end
103
104 redef class CmdEntityList
105 # FIXME avoid linearization conflict
106 redef fun http_init(req) do return super
107 end
108
109 redef class CmdSearch
110 redef fun http_init(req) do
111 query = req.string_arg("q")
112 return super
113 end
114 end
115
116 redef class CmdModelEntities
117 redef fun http_init(req) do
118 kind = req.string_arg("kind") or else "all"
119 return super
120 end
121 end
122
123 redef class CmdCode
124 redef fun http_init(req) do
125 format = req.string_arg("format") or else "raw"
126 return super
127 end
128 end
129
130 # CmdGraph
131
132 redef class CmdGraph
133 redef fun http_init(req) do
134 format = req.string_arg("format") or else "dot"
135 return super
136 end
137 end
138
139 redef class CmdInheritanceGraph
140 redef fun http_init(req) do
141 pdepth = req.int_arg("pdepth")
142 cdepth = req.int_arg("cdepth")
143 return super
144 end
145 end
146
147 # CmdCatalog
148
149 redef class CmdCatalogTag
150 redef fun http_init(req) do
151 var tag = req.param("tid")
152 if tag != null then tag = tag.from_percent_encoding
153 self.tag = tag
154 return super
155 end
156 end
157
158 redef class CmdCatalogPerson
159 redef fun http_init(req) do
160 var name = req.param("pid")
161 if name != null then name = name.from_percent_encoding
162 self.person_name = name
163 return super
164 end
165 end