doc/commands: parse CmdEntityLink commands from http
[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 redef fun http_init(req) do
32 var name = req.param("id")
33 if name != null then name = name.from_percent_encoding
34 self.mentity_name = name
35
36 return super
37 end
38 end
39
40 redef class CmdList
41 redef fun http_init(req) do
42 limit = req.int_arg("l")
43 page = req.int_arg("p")
44 return super
45 end
46 end
47
48 # Error Handling
49
50 # Message handling
51
52 redef class CmdMessage
53 # HTTP code to return for this message
54 var http_status_code = 200
55 end
56
57 redef class CmdError
58 redef var http_status_code = 400
59 end
60
61 redef class CmdWarning
62 redef var http_status_code = 404
63 end
64
65 redef class ErrorMEntityNoName
66 redef var http_status_code = 400
67 end
68
69 redef class ErrorMEntityNotFound
70 redef var http_status_code = 404
71 end
72
73 redef class ErrorMEntityConflict
74 redef var http_status_code = 300
75 end
76
77 # CmdModel
78
79 redef class CmdComment
80 redef fun http_init(req) do
81 full_doc = req.bool_arg("full_doc") or else true
82 fallback = req.bool_arg("fallback") or else true
83 format = req.string_arg("format") or else "raw"
84 return super
85 end
86 end
87
88 redef class CmdEntityLink
89 redef fun http_init(req) do
90 text = req.string_arg("text")
91 title = req.string_arg("title")
92 return super
93 end
94 end
95
96 redef class CmdAncestors
97 redef fun http_init(req) do
98 parents = req.bool_arg("parents") or else true
99 return super
100 end
101 end
102
103 redef class CmdDescendants
104 redef fun http_init(req) do
105 children = req.bool_arg("children") or else true
106 return super
107 end
108 end
109
110 redef class CmdEntityList
111 # FIXME avoid linearization conflict
112 redef fun http_init(req) do return super
113 end
114
115 redef class CmdSearch
116 redef fun http_init(req) do
117 query = req.string_arg("q")
118 return super
119 end
120 end
121
122 redef class CmdModelEntities
123 redef fun http_init(req) do
124 kind = req.string_arg("kind") or else "all"
125 return super
126 end
127 end
128
129 redef class CmdCode
130 redef fun http_init(req) do
131 format = req.string_arg("format") or else "raw"
132 return super
133 end
134 end
135
136 redef class CmdEntityCode
137 # FIXME avoid linearization conflict
138 redef fun http_init(req) do
139 var name = req.param("id")
140 if name != null then name = name.from_percent_encoding
141 mentity_name = name
142
143 format = req.string_arg("format") or else "raw"
144 return init_command
145 end
146 end
147
148 # CmdGraph
149
150 redef class CmdGraph
151 redef fun http_init(req) do
152 format = req.string_arg("format") or else "dot"
153 return super
154 end
155 end
156
157 redef class CmdInheritanceGraph
158 redef fun http_init(req) do
159 pdepth = req.int_arg("pdepth")
160 cdepth = req.int_arg("cdepth")
161 return super
162 end
163 end
164
165 # CmdCatalog
166
167 redef class CmdCatalogTag
168 redef fun http_init(req) do
169 var tag = req.param("tid")
170 if tag != null then tag = tag.from_percent_encoding
171 self.tag = tag
172 return super
173 end
174 end
175
176 redef class CmdCatalogPerson
177 redef fun http_init(req) do
178 var name = req.param("pid")
179 if name != null then name = name.from_percent_encoding
180 self.person_name = name
181 return super
182 end
183 end