nitweb: catalog api serves tags related data
[nit.git] / src / web / api_catalog.nit
index 79bb111..4d4ca75 100644 (file)
@@ -41,6 +41,9 @@ redef class APIRouter
                use("/catalog/bytags", new APICatalogByTags(config))
                use("/catalog/contributors", new APICatalogContributors(config))
                use("/catalog/stats", new APICatalogStats(config))
+
+               use("/catalog/tags", new APICatalogTags(config))
+               use("/catalog/tag/:tid", new APICatalogTag(config))
        end
 end
 
@@ -89,23 +92,7 @@ class APICatalogPackages
                var mpackages = config.catalog.mpackages.values.to_a
                mpackages_sorter.sort(mpackages)
                var response = new JsonArray.from(mpackages)
-               res.json paginate(response, page, limit)
-       end
-end
-
-class APICatalogStats
-       super APICatalogHandler
-
-       redef fun get(req, res) do
-               var obj = new JsonObject
-               obj["packages"] = config.model.mpackages.length
-               obj["maintainers"] = config.catalog.maint2proj.length
-               obj["contributors"] = config.catalog.contrib2proj.length
-               obj["modules"] = config.catalog.mmodules.sum
-               obj["classes"] = config.catalog.mclasses.sum
-               obj["methods"] = config.catalog.mmethods.sum
-               obj["loc"] = config.catalog.loc.sum
-               res.json obj
+               res.json paginate(response, response.length, page, limit)
        end
 end
 
@@ -148,6 +135,68 @@ class APICatalogContributors
        end
 end
 
+# Get the catalog statistics
+#
+# `GET /stats`: return the catalog statistics
+class APICatalogStats
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               res.json config.catalog.catalog_stats
+       end
+end
+
+# Get all the tags from the catalog
+#
+# `GET /tags`: the list of tags associated with their number of packages
+class APICatalogTags
+       super APICatalogHandler
+
+       # Sorter to sort tags alphabetically
+       var tags_sorter = new CatalogTagsSorter
+
+       redef fun get(req, res) do
+               var obj = new JsonObject
+
+               var tags = config.catalog.tag2proj.keys.to_a
+               tags_sorter.sort(tags)
+
+               for tag in tags do
+                       if not config.catalog.tag2proj.has_key(tag) then continue
+                       obj[tag] = config.catalog.tag2proj[tag].length
+               end
+               res.json obj
+       end
+end
+
+# Get the packages related to a tag
+#
+# `GET /tag/:tid?p=1&n=10`: return a paginated list of packages
+class APICatalogTag
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               var page = req.int_arg("p")
+               var limit = req.int_arg("n")
+               var id = req.param("tid")
+               if id == null then
+                       res.api_error(400, "Missing tag")
+                       return
+               end
+               id = id.from_percent_encoding
+               if not config.catalog.tag2proj.has_key(id) then
+                       res.api_error(404, "Tag not found")
+                       return
+               end
+               var obj = new JsonObject
+               obj["tag"] = id
+               var mpackages = config.catalog.tag2proj[id]
+               mpackages_sorter.sort(mpackages)
+               var response = new JsonArray.from(mpackages)
+               obj["packages"] = paginate(response, response.length, page, limit)
+               res.json obj
+       end
+end
 redef class Catalog
 
        # Build the catalog from `mpackages`