1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 # Specific handler for nitweb API.
22 abstract class APIHandler
25 # The JSON API does not filter anything by default.
27 # So we can cache the model view.
28 var view
: ModelView is lazy
do
29 var view
= new ModelView(model
)
30 view
.min_visibility
= private_visibility
31 view
.include_fictive
= true
32 view
.include_empty_doc
= true
33 view
.include_attribute
= true
34 view
.include_test_suite
= true
38 # Try to load the mentity from uri with `/:id`.
40 # Send 400 if `:id` is null.
41 # Send 404 if no entity is found.
42 # Return null in both cases.
43 fun mentity_from_uri
(req
: HttpRequest, res
: HttpResponse): nullable MEntity do
44 var id
= req
.param
("id")
49 var mentity
= find_mentity
(view
, id
)
50 if mentity
== null then
57 # Group all api handlers in one router.
61 # Model to pass to handlers.
64 # ModelBuilder to pass to handlers.
65 var modelbuilder
: ModelBuilder
67 # Mainmodule to pass to handlers.
68 var mainmodule
: MModule
71 use
("/list", new APIList(model
, mainmodule
))
72 use
("/search", new APISearch(model
, mainmodule
))
73 use
("/random", new APIRandom(model
, mainmodule
))
74 use
("/entity/:id", new APIEntity(model
, mainmodule
))
75 use
("/code/:id", new APIEntityCode(model
, mainmodule
, modelbuilder
))
76 use
("/uml/:id", new APIEntityUML(model
, mainmodule
))
82 # MEntities can be filtered on their kind using the `k` parameter.
83 # Allowed kinds are `package`, `group`, `module`, `class`, `classdef`, `property`, `propdef`.
85 # List size can be limited with the `n` parameter.
87 # Example: `GET /list?k=module?n=10`
91 # List mentities depending on the `k` kind parameter.
92 fun list_mentities
(req
: HttpRequest): Array[MEntity] do
93 var k
= req
.string_arg
("k")
94 var mentities
= new Array[MEntity]
95 if k
== "package" then
96 for mentity
in view
.mpackages
do mentities
.add mentity
97 else if k
== "group" then
98 for mentity
in view
.mgroups
do mentities
.add mentity
99 else if k
== "module" then
100 for mentity
in view
.mmodules
do mentities
.add mentity
101 else if k
== "class" then
102 for mentity
in view
.mclasses
do mentities
.add mentity
103 else if k
== "classdef" then
104 for mentity
in view
.mclassdefs
do mentities
.add mentity
105 else if k
== "property" then
106 for mentity
in view
.mproperties
do mentities
.add mentity
107 else if k
== "propdef" then
108 for mentity
in view
.mpropdefs
do mentities
.add mentity
110 for mentity
in view
.mentities
do mentities
.add mentity
115 # Limit mentities depending on the `n` parameter.
116 fun limit_mentities
(req
: HttpRequest, mentities
: Array[MEntity]): Array[MEntity] do
117 var n
= req
.int_arg
("n")
119 return mentities
.sub
(0, n
)
124 redef fun get
(req
, res
) do
125 var mentities
= list_mentities
(req
)
126 mentities
= limit_mentities
(req
, mentities
)
127 res
.json
new JsonArray.from
(mentities
)
131 # Search mentities from a query string.
133 # Example: `GET /search?q=Arr`
137 redef fun list_mentities
(req
) do
138 var q
= req
.string_arg
("q")
139 var mentities
= new Array[MEntity]
140 if q
== null then return mentities
141 for mentity
in view
.mentities
do
142 if mentity
.name
.has_prefix
(q
) then mentities
.add mentity
148 # Return a random list of MEntities.
150 # Example: `GET /random?n=10&k=module`
154 # Randomize mentities order.
155 fun randomize_mentities
(req
: HttpRequest, mentities
: Array[MEntity]): Array[MEntity] do
156 var res
= mentities
.to_a
161 redef fun get
(req
, res
) do
162 var mentities
= list_mentities
(req
)
163 mentities
= limit_mentities
(req
, mentities
)
164 mentities
= randomize_mentities
(req
, mentities
)
165 res
.json
new JsonArray.from
(mentities
)
169 # Return the JSON representation of a MEntity.
171 # Example: `GET /entity/core::Array`
175 redef fun get
(req
, res
) do
176 var mentity
= mentity_from_uri
(req
, res
)
177 if mentity
== null then return
178 res
.json mentity
.api_json
(self)
183 # Return a UML representation of MEntity.
185 # Example: `GET /entity/core::Array/uml`
189 redef fun get
(req
, res
) do
190 var mentity
= mentity_from_uri
(req
, res
)
192 if mentity
isa MClassDef then mentity
= mentity
.mclass
193 if mentity
isa MClass then
194 var uml
= new UMLModel(view
, mainmodule
)
195 dot
= uml
.generate_class_uml
.write_to_string
196 else if mentity
isa MModule then
197 var uml
= new UMLModel(view
, mentity
)
198 dot
= uml
.generate_package_uml
.write_to_string
203 res
.send render_svg
(dot
)
206 # Render a `dot` string as a svg image.
207 fun render_svg
(dot
: String): String do
208 var proc
= new ProcessDuplex("dot", "-Tsvg")
209 var svg
= proc
.write_and_read
(dot
)
216 # Return the source code of MEntity.
218 # Example: `GET /entity/core::Array/code`
222 # Modelbuilder used to access sources.
223 var modelbuilder
: ModelBuilder
225 redef fun get
(req
, res
) do
226 var mentity
= mentity_from_uri
(req
, res
)
227 if mentity
== null then return
228 var source
= render_source
(mentity
)
229 if source
== null then
236 # Highlight `mentity` source code.
237 private fun render_source
(mentity
: MEntity): nullable HTMLTag do
238 var node
= modelbuilder
.mentity2node
(mentity
)
239 if node
== null then return null
240 var hl
= new HighlightVisitor