import highlight
import uml
-# Specific handler for nitweb API.
-abstract class APIHandler
- super ModelHandler
-
- # The JSON API does not filter anything by default.
- #
- # So we can cache the model view.
- var view: ModelView is lazy do
- var view = new ModelView(model)
- view.min_visibility = private_visibility
- view.include_fictive = true
- view.include_empty_doc = true
- view.include_attribute = true
- view.include_test_suite = true
- return view
- end
-
- # Try to load the mentity from uri with `/:id`.
- #
- # Send 400 if `:id` is null.
- # Send 404 if no entity is found.
- # Return null in both cases.
- fun mentity_from_uri(req: HttpRequest, res: HttpResponse): nullable MEntity do
- var id = req.param("id")
- if id == null then
- res.error 400
- return null
- end
- var mentity = find_mentity(view, id)
- if mentity == null then
- res.error 404
- end
- return mentity
- end
-end
-
-# Group all api handlers in one router.
-class APIRouter
- super Router
-
- # Model to pass to handlers.
- var model: Model
-
- # ModelBuilder to pass to handlers.
- var modelbuilder: ModelBuilder
-
- # Mainmodule to pass to handlers.
- var mainmodule: MModule
-
- init do
- use("/list", new APIList(model, mainmodule))
- use("/search", new APISearch(model, mainmodule))
- use("/random", new APIRandom(model, mainmodule))
- use("/entity/:id", new APIEntity(model, mainmodule))
- use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
- use("/uml/:id", new APIEntityUML(model, mainmodule))
- end
-end
-
# List all mentities.
#
# MEntities can be filtered on their kind using the `k` parameter.
end
end
+# List ancestors, parents, child and descendants of MEntity
+#
+# Example: `GET /entity/core::Array/inheritance`
+class APIEntityInheritance
+ super APIHandler
+
+ redef fun get(req, res) do
+ var mentity = mentity_from_uri(req, res)
+ if mentity == null then
+ res.error 404
+ return
+ end
+ res.json mentity.hierarchy_poset(view)[mentity]
+ end
+end
+
+# Linearize super definitions of a MClassDef or a MPropDef if any.
+#
+# Example: `GET /entity/core::Array/linearization`
+class APIEntityLinearization
+ super APIHandler
+
+ redef fun get(req, res) do
+ var mentity = mentity_from_uri(req, res)
+ if mentity == null then
+ res.error 404
+ return
+ end
+ var lin = mentity.collect_linearization(mainmodule)
+ if lin == null then
+ res.error 404
+ return
+ end
+ res.json new JsonArray.from(lin)
+ end
+end
+
+# List definitions of a MEntity.
+#
+# Example: `GET /defs/core::Array`
+class APIEntityDefs
+ super APIHandler
+
+ redef fun get(req, res) do
+ var mentity = mentity_from_uri(req, res)
+ var arr = new JsonArray
+ if mentity isa MModule then
+ for mclassdef in mentity.mclassdefs do arr.add mclassdef
+ else if mentity isa MClass then
+ for mclassdef in mentity.mclassdefs do arr.add mclassdef
+ else if mentity isa MClassDef then
+ for mpropdef in mentity.mpropdefs do arr.add mpropdef
+ else if mentity isa MProperty then
+ for mpropdef in mentity.mpropdefs do arr.add mpropdef
+ else
+ res.error 404
+ return
+ end
+ res.json arr
+ end
+end
+
+abstract class SVGHandler
+ super APIHandler
+
+ # Render a `dot` string as a svg image.
+ fun render_dot(dot: Text): String do
+ var proc = new ProcessDuplex("dot", "-Tsvg")
+ var svg = proc.write_and_read(dot)
+ proc.close
+ proc.wait
+ return svg
+ end
+end
# Return a UML representation of MEntity.
#
# Example: `GET /entity/core::Array/uml`
class APIEntityUML
- super APIHandler
+ super SVGHandler
redef fun get(req, res) do
var mentity = mentity_from_uri(req, res)
res.error 404
return
end
- res.send render_svg(dot)
- end
-
- # Render a `dot` string as a svg image.
- fun render_svg(dot: String): String do
- var proc = new ProcessDuplex("dot", "-Tsvg")
- var svg = proc.write_and_read(dot)
- proc.close
- proc.wait
- return svg
+ res.send render_dot(dot)
end
end