nitweb: introduce /api/catalog/packages route
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 15 Aug 2017 20:35:21 +0000 (16:35 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 26 Sep 2017 15:10:05 +0000 (11:10 -0400)
List all packages in current catalog

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/web/api_catalog.nit

index 1d7f4bf..79bb111 100644 (file)
@@ -34,6 +34,8 @@ end
 redef class APIRouter
        redef init do
                super
+               use("/catalog/packages/", new APICatalogPackages(config))
+
                use("/catalog/highlighted", new APICatalogHighLighted(config))
                use("/catalog/required", new APICatalogMostRequired(config))
                use("/catalog/bytags", new APICatalogByTags(config))
@@ -75,6 +77,22 @@ abstract class APICatalogHandler
        end
 end
 
+# Get all the packages from the catalog using pagination
+#
+# `GET /packages?p=1&n=10`: get the list of catalog by page
+class APICatalogPackages
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               var page = req.int_arg("p")
+               var limit = req.int_arg("n")
+               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