nitweb: migrate /entity?json to /api/entity
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 20 May 2016 02:59:04 +0000 (22:59 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 30 May 2016 15:04:41 +0000 (11:04 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/web/model_api.nit
src/web/web_actions.nit
src/web/web_base.nit

index f6ad2e5..85a2360 100644 (file)
@@ -71,6 +71,7 @@ class APIRouter
                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
@@ -172,6 +173,19 @@ class APIRandom
        end
 end
 
+# Return the JSON representation of a MEntity.
+#
+# Example: `GET /entity/core::Array`
+class APIEntity
+       super APIHandler
+
+       redef fun get(req, res) do
+               var mentity = mentity_from_uri(req, res)
+               if mentity == null then return
+               res.json mentity.api_json(self)
+       end
+end
+
 
 # Return a UML representation of MEntity.
 #
index 13a7db7..cb193fd 100644 (file)
@@ -44,11 +44,6 @@ class DocAction
                        res.error(404)
                        return
                end
-               if req.is_json_asked then
-                       res.json(mentity.to_json)
-                       return
-               end
-
                var view = new HtmlDocPage(modelbuilder, mentity)
                res.send_view(view)
        end
index f905c9f..ec9bf75 100644 (file)
@@ -62,13 +62,6 @@ redef class HttpResponse
        fun send_view(view: NitView, status: nullable Int) do send(view.render, status)
 end
 
-redef class HttpRequest
-       # Does the client asked for a json formatted response?
-       #
-       # Checks the URL get parameter `?json=true`.
-       fun is_json_asked: Bool do return bool_arg("json") or else false
-end
-
 redef class MEntity
 
        # URL to `self` within the web interface.