From: Alexandre Terrasa Date: Tue, 15 Aug 2017 20:50:51 +0000 (-0400) Subject: nitweb: catalog api serves tags related data X-Git-Url: http://nitlanguage.org nitweb: catalog api serves tags related data Signed-off-by: Alexandre Terrasa --- diff --git a/src/web/api_catalog.nit b/src/web/api_catalog.nit index 26f6ac3..4d4ca75 100644 --- a/src/web/api_catalog.nit +++ b/src/web/api_catalog.nit @@ -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 @@ -143,6 +146,57 @@ class APICatalogStats 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`