nitweb: better error responses from API
[nit.git] / src / web / api_model.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 module api_model
16
17 import web_base
18 import highlight
19 import uml
20
21 redef class APIRouter
22 redef init do
23 super
24 use("/list", new APIList(config))
25 use("/search", new APISearch(config))
26 use("/random", new APIRandom(config))
27 use("/entity/:id", new APIEntity(config))
28 use("/code/:id", new APIEntityCode(config))
29 use("/uml/:id", new APIEntityUML(config))
30 use("/linearization/:id", new APIEntityLinearization(config))
31 use("/defs/:id", new APIEntityDefs(config))
32 use("/inheritance/:id", new APIEntityInheritance(config))
33 end
34 end
35
36 # List all mentities.
37 #
38 # MEntities can be filtered on their kind using the `k` parameter.
39 # Allowed kinds are `package`, `group`, `module`, `class`, `classdef`, `property`, `propdef`.
40 #
41 # List size can be limited with the `n` parameter.
42 #
43 # Example: `GET /list?k=module?n=10`
44 class APIList
45 super APIHandler
46
47 # List mentities depending on the `k` kind parameter.
48 fun list_mentities(req: HttpRequest): Array[MEntity] do
49 var k = req.string_arg("k")
50 var mentities = new Array[MEntity]
51 if k == "package" then
52 for mentity in view.mpackages do mentities.add mentity
53 else if k == "group" then
54 for mentity in view.mgroups do mentities.add mentity
55 else if k == "module" then
56 for mentity in view.mmodules do mentities.add mentity
57 else if k == "class" then
58 for mentity in view.mclasses do mentities.add mentity
59 else if k == "classdef" then
60 for mentity in view.mclassdefs do mentities.add mentity
61 else if k == "property" then
62 for mentity in view.mproperties do mentities.add mentity
63 else if k == "propdef" then
64 for mentity in view.mpropdefs do mentities.add mentity
65 else
66 for mentity in view.mentities do mentities.add mentity
67 end
68 return mentities
69 end
70
71 # Limit mentities depending on the `n` parameter.
72 fun limit_mentities(req: HttpRequest, mentities: Array[MEntity]): Array[MEntity] do
73 var n = req.int_arg("n")
74 if n != null then
75 return mentities.sub(0, n)
76 end
77 return mentities
78 end
79
80 redef fun get(req, res) do
81 var mentities = list_mentities(req)
82 mentities = limit_mentities(req, mentities)
83 res.json new JsonArray.from(mentities)
84 end
85 end
86
87 # Search mentities from a query string.
88 #
89 # Example: `GET /search?q=Arr`
90 class APISearch
91 super APIList
92
93 redef fun list_mentities(req) do
94 var q = req.string_arg("q")
95 var mentities = new Array[MEntity]
96 if q == null then return mentities
97 for mentity in view.mentities do
98 if mentity.name.has_prefix(q) then mentities.add mentity
99 end
100 return mentities
101 end
102 end
103
104 # Return a random list of MEntities.
105 #
106 # Example: `GET /random?n=10&k=module`
107 class APIRandom
108 super APIList
109
110 # Randomize mentities order.
111 fun randomize_mentities(req: HttpRequest, mentities: Array[MEntity]): Array[MEntity] do
112 var res = mentities.to_a
113 res.shuffle
114 return res
115 end
116
117 redef fun get(req, res) do
118 var mentities = list_mentities(req)
119 mentities = limit_mentities(req, mentities)
120 mentities = randomize_mentities(req, mentities)
121 res.json new JsonArray.from(mentities)
122 end
123 end
124
125 # Return the JSON representation of a MEntity.
126 #
127 # Example: `GET /entity/core::Array`
128 class APIEntity
129 super APIHandler
130
131 redef fun get(req, res) do
132 var mentity = mentity_from_uri(req, res)
133 if mentity == null then return
134 res.json mentity.api_json(self)
135 end
136 end
137
138 # List ancestors, parents, child and descendants of MEntity
139 #
140 # Example: `GET /entity/core::Array/inheritance`
141 class APIEntityInheritance
142 super APIHandler
143
144 redef fun get(req, res) do
145 var mentity = mentity_from_uri(req, res)
146 if mentity == null then return
147 res.json mentity.hierarchy_poset(view)[mentity]
148 end
149 end
150
151 # Linearize super definitions of a MClassDef or a MPropDef if any.
152 #
153 # Example: `GET /entity/core::Array/linearization`
154 class APIEntityLinearization
155 super APIHandler
156
157 redef fun get(req, res) do
158 var mentity = mentity_from_uri(req, res)
159 if mentity == null then return
160 var lin = mentity.collect_linearization(config.mainmodule)
161 if lin == null then
162 res.api_error(404, "No linearization for mentity `{mentity.full_name}`")
163 return
164 end
165 res.json new JsonArray.from(lin)
166 end
167 end
168
169 # List definitions of a MEntity.
170 #
171 # Example: `GET /defs/core::Array`
172 class APIEntityDefs
173 super APIHandler
174
175 redef fun get(req, res) do
176 var mentity = mentity_from_uri(req, res)
177 if mentity == null then return
178 var arr = new JsonArray
179 if mentity isa MModule then
180 for mclassdef in mentity.mclassdefs do arr.add mclassdef
181 else if mentity isa MClass then
182 for mclassdef in mentity.mclassdefs do arr.add mclassdef
183 else if mentity isa MClassDef then
184 for mpropdef in mentity.mpropdefs do arr.add mpropdef
185 else if mentity isa MProperty then
186 for mpropdef in mentity.mpropdefs do arr.add mpropdef
187 else
188 res.api_error(404, "No definition list for mentity `{mentity.full_name}`")
189 return
190 end
191 res.json arr
192 end
193 end
194
195 abstract class SVGHandler
196 super APIHandler
197
198 # Render a `dot` string as a svg image.
199 fun render_dot(dot: Text): String do
200 var proc = new ProcessDuplex("dot", "-Tsvg")
201 var svg = proc.write_and_read(dot)
202 proc.close
203 proc.wait
204 return svg
205 end
206 end
207
208 # Return a UML representation of MEntity.
209 #
210 # Example: `GET /entity/core::Array/uml`
211 class APIEntityUML
212 super SVGHandler
213
214 redef fun get(req, res) do
215 var mentity = mentity_from_uri(req, res)
216 if mentity == null then return
217 var dot
218 if mentity isa MClassDef then mentity = mentity.mclass
219 if mentity isa MClass then
220 var uml = new UMLModel(view, config.mainmodule)
221 dot = uml.generate_class_uml.write_to_string
222 else if mentity isa MModule then
223 var uml = new UMLModel(view, mentity)
224 dot = uml.generate_package_uml.write_to_string
225 else
226 res.api_error(404, "No UML for mentity `{mentity.full_name}`")
227 return
228 end
229 res.send render_dot(dot)
230 end
231 end
232
233 # Return the source code of MEntity.
234 #
235 # Example: `GET /entity/core::Array/code`
236 class APIEntityCode
237 super APIHandler
238
239 redef fun get(req, res) do
240 var mentity = mentity_from_uri(req, res)
241 if mentity == null then return
242 var source = render_source(mentity)
243 if source == null then
244 res.api_error(404, "No code for mentity `{mentity.full_name}`")
245 return
246 end
247 res.send source
248 end
249
250 # Highlight `mentity` source code.
251 private fun render_source(mentity: MEntity): nullable HTMLTag do
252 var node = config.modelbuilder.mentity2node(mentity)
253 if node == null then return null
254 var hl = new HighlightVisitor
255 hl.enter_visit node
256 return hl.html
257 end
258 end