nitweb: factorize APIRouter
[nit.git] / src / web / web_base.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 # Base classes used by `nitweb`.
16 module web_base
17
18 import model::model_views
19 import model::model_json
20 import doc_down
21 import popcorn
22
23 # Specific nitcorn Action that uses a Model
24 class ModelHandler
25 super Handler
26
27 # Model to use.
28 var model: Model
29
30 # MModule used to flatten model.
31 var mainmodule: MModule
32
33 # Find the MEntity ` with `full_name`.
34 fun find_mentity(model: ModelView, full_name: nullable String): nullable MEntity do
35 if full_name == null then return null
36 return model.mentity_by_full_name(full_name.from_percent_encoding)
37 end
38
39 # Init the model view from the `req` uri parameters.
40 fun init_model_view(req: HttpRequest): ModelView do
41 var view = new ModelView(model)
42 var show_private = req.bool_arg("private") or else false
43 if not show_private then view.min_visibility = protected_visibility
44
45 view.include_fictive = req.bool_arg("fictive") or else false
46 view.include_empty_doc = req.bool_arg("empty-doc") or else true
47 view.include_test_suite = req.bool_arg("test-suite") or else false
48 view.include_attribute = req.bool_arg("attributes") or else true
49
50 return view
51 end
52 end
53
54 # Specific handler for nitweb API.
55 abstract class APIHandler
56 super ModelHandler
57
58 # The JSON API does not filter anything by default.
59 #
60 # So we can cache the model view.
61 var view: ModelView is lazy do
62 var view = new ModelView(model)
63 view.min_visibility = private_visibility
64 view.include_fictive = true
65 view.include_empty_doc = true
66 view.include_attribute = true
67 view.include_test_suite = true
68 return view
69 end
70
71 # Try to load the mentity from uri with `/:id`.
72 #
73 # Send 400 if `:id` is null.
74 # Send 404 if no entity is found.
75 # Return null in both cases.
76 fun mentity_from_uri(req: HttpRequest, res: HttpResponse): nullable MEntity do
77 var id = req.param("id")
78 if id == null then
79 res.error 400
80 return null
81 end
82 var mentity = find_mentity(view, id)
83 if mentity == null then
84 res.error 404
85 end
86 return mentity
87 end
88 end
89
90 # A Rooter dedicated to APIHandlers.
91 class APIRouter
92 super Router
93
94 # Model to use.
95 var model: Model
96
97 # MModule used to flatten model.
98 var mainmodule: MModule
99 end
100
101 redef class MEntity
102
103 # URL to `self` within the web interface.
104 fun web_url: String do return "/doc/" / full_name
105
106 # URL to `self` within the JSON api.
107 fun api_url: String do return "/api/entity/" / full_name
108
109 redef fun json do
110 var obj = super
111 obj["web_url"] = web_url
112 obj["api_url"] = api_url
113 return obj
114 end
115
116 # Get the full json repesentation of `self` with MEntityRefs resolved.
117 fun api_json(handler: ModelHandler): JsonObject do return json
118 end
119
120 redef class MEntityRef
121 redef fun json do
122 var obj = super
123 obj["web_url"] = mentity.web_url
124 obj["api_url"] = mentity.api_url
125 obj["name"] = mentity.name
126 obj["mdoc"] = mentity.mdoc_or_fallback
127 obj["visibility"] = mentity.visibility
128 obj["location"] = mentity.location
129 var modifiers = new JsonArray
130 for modifier in mentity.collect_modifiers do
131 modifiers.add modifier
132 end
133 obj["modifiers"] = modifiers
134 var mentity = self.mentity
135 if mentity isa MMethod then
136 obj["msignature"] = mentity.intro.msignature
137 else if mentity isa MMethodDef then
138 obj["msignature"] = mentity.msignature
139 else if mentity isa MVirtualTypeProp then
140 obj["bound"] = to_mentity_ref(mentity.intro.bound)
141 else if mentity isa MVirtualTypeDef then
142 obj["bound"] = to_mentity_ref(mentity.bound)
143 end
144 return obj
145 end
146 end
147
148 redef class MDoc
149
150 # Add doc down processing
151 redef fun json do
152 var obj = super
153 obj["synopsis"] = synopsis
154 obj["documentation"] = documentation
155 obj["comment"] = comment
156 obj["html_synopsis"] = html_synopsis.write_to_string
157 obj["html_documentation"] = html_documentation.write_to_string
158 obj["html_comment"] = html_comment.write_to_string
159 return obj
160 end
161 end
162
163 redef class MModule
164 redef fun api_json(handler) do
165 var obj = super
166 obj["intro_mclassdefs"] = to_mentity_refs(collect_intro_mclassdefs(private_view))
167 obj["redef_mclassdefs"] = to_mentity_refs(collect_redef_mclassdefs(private_view))
168 obj["imports"] = to_mentity_refs(in_importation.direct_greaters)
169 return obj
170 end
171 end
172
173 redef class MClass
174 redef fun api_json(handler) do
175 var obj = super
176 obj["all_mproperties"] = to_mentity_refs(collect_accessible_mproperties(private_view))
177 obj["intro_mproperties"] = to_mentity_refs(collect_intro_mproperties(private_view))
178 obj["redef_mproperties"] = to_mentity_refs(collect_redef_mproperties(private_view))
179 obj["parents"] = to_mentity_refs(collect_parents(private_view))
180 return obj
181 end
182 end
183
184 redef class MClassDef
185 redef fun json do
186 var obj = super
187 obj["intro"] = to_mentity_ref(mclass.intro)
188 obj["mpackage"] = to_mentity_ref(mmodule.mpackage)
189 return obj
190 end
191
192 redef fun api_json(handler) do
193 var obj = super
194 obj["intro_mpropdefs"] = to_mentity_refs(collect_intro_mpropdefs(private_view))
195 obj["redef_mpropdefs"] = to_mentity_refs(collect_redef_mpropdefs(private_view))
196 return obj
197 end
198 end
199
200 redef class MProperty
201 redef fun json do
202 var obj = super
203 obj["intro_mclass"] = to_mentity_ref(intro_mclassdef.mclass)
204 obj["mpackage"] = to_mentity_ref(intro_mclassdef.mmodule.mpackage)
205 return obj
206 end
207 end
208
209 redef class MPropDef
210 redef fun json do
211 var obj = super
212 obj["intro"] = to_mentity_ref(mproperty.intro)
213 obj["intro_mclassdef"] = to_mentity_ref(mproperty.intro.mclassdef)
214 obj["mmodule"] = to_mentity_ref(mclassdef.mmodule)
215 obj["mgroup"] = to_mentity_ref(mclassdef.mmodule.mgroup)
216 obj["mpackage"] = to_mentity_ref(mclassdef.mmodule.mpackage)
217 return obj
218 end
219 end
220
221 redef class MClassType
222 redef var web_url = mclass.web_url is lazy
223 end
224
225 redef class MNullableType
226 redef var web_url = mtype.web_url is lazy
227 end
228
229 redef class MParameterType
230 redef var web_url = mclass.web_url is lazy
231 end
232
233 redef class MVirtualType
234 redef var web_url = mproperty.web_url is lazy
235 end
236
237 redef class POSetElement[E]
238 super Jsonable
239
240 # Return JSON representation of `self`.
241 fun json: JsonObject do
242 assert self isa POSetElement[MEntity]
243 var obj = new JsonObject
244 obj["greaters"] = to_mentity_refs(greaters)
245 obj["direct_greaters"] = to_mentity_refs(direct_greaters)
246 obj["direct_smallers"] = to_mentity_refs(direct_smallers)
247 obj["smallers"] = to_mentity_refs(smallers)
248 return obj
249 end
250
251 redef fun to_json do return json.to_json
252 end