From e9ed78623181e3395478c6b9bb2972ce51eafbb3 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 17 Aug 2017 16:32:06 -0400 Subject: [PATCH] nitweb: allows client to precise ?pretty in request args Signed-off-by: Alexandre Terrasa --- src/web/api_catalog.nit | 18 +++++++++--------- src/web/api_feedback.nit | 16 ++++++++-------- src/web/api_light.nit | 2 +- src/web/api_metrics.nit | 2 +- src/web/api_model.nit | 17 +++++++++-------- src/web/web_base.nit | 18 ++++++++++++++++++ 6 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/web/api_catalog.nit b/src/web/api_catalog.nit index afad834..61b0d0d 100644 --- a/src/web/api_catalog.nit +++ b/src/web/api_catalog.nit @@ -67,7 +67,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, response.length, page, limit) + res.api_json(req, paginate(response, response.length, page, limit)) end end @@ -78,7 +78,7 @@ class APICatalogStats super APICatalogHandler redef fun get(req, res) do - res.json config.catalog.catalog_stats + res.api_json(req, config.catalog.catalog_stats) end end @@ -101,7 +101,7 @@ class APICatalogTags if not config.catalog.tag2proj.has_key(tag) then continue obj[tag] = config.catalog.tag2proj[tag].length end - res.json obj + res.api_json(req, obj) end end @@ -130,7 +130,7 @@ class APICatalogTag mpackages_sorter.sort(mpackages) var response = new JsonArray.from(mpackages) obj["packages"] = paginate(response, response.length, page, limit) - res.json obj + res.api_json(req, obj) end end @@ -158,7 +158,7 @@ class APICatalogPerson redef fun get(req, res) do var person = get_person(req, res) if person == null then return - res.json person + res.api_json(req, person) end end @@ -180,7 +180,7 @@ class APICatalogMaintaining end mpackages_sorter.sort(array) var response = new JsonArray.from(array) - res.json paginate(response, response.length, page, limit) + res.api_json(req, paginate(response, response.length, page, limit)) end end @@ -202,7 +202,7 @@ class APICatalogContributing end mpackages_sorter.sort(array) var response = new JsonArray.from(array) - res.json paginate(response, response.length, page, limit) + res.api_json(req, paginate(response, response.length, page, limit)) end end @@ -213,9 +213,9 @@ redef class APIEntity # Special case for packages (catalog view) if mentity isa MPackage then - res.raw_json mentity.to_full_catalog_json(plain=true, config.mainmodule, config.catalog) + res.raw_json mentity.to_full_catalog_json(config.catalog, plain = true, pretty = req.bool_arg("pretty")) else - res.raw_json mentity.to_full_json(config.mainmodule) + res.api_full_json(req, mentity) end end end diff --git a/src/web/api_feedback.nit b/src/web/api_feedback.nit index 6a356c3..d94c9b3 100644 --- a/src/web/api_feedback.nit +++ b/src/web/api_feedback.nit @@ -65,7 +65,7 @@ class APIStarsMost super APIFeedBack redef fun get(req, res) do - res.json new JsonArray.from(config.stars.most_rated) + res.api_json(req, new JsonArray.from(config.stars.most_rated)) end end @@ -74,7 +74,7 @@ class APIStarsBest super APIFeedBack redef fun get(req, res) do - res.json new JsonArray.from(config.stars.best_rated) + res.api_json(req, new JsonArray.from(config.stars.best_rated)) end end @@ -83,7 +83,7 @@ class APIStarsWorst super APIFeedBack redef fun get(req, res) do - res.json new JsonArray.from(config.stars.worst_rated) + res.api_json(req, new JsonArray.from(config.stars.worst_rated)) end end @@ -92,7 +92,7 @@ class APIStarsUsers super APIFeedBack redef fun get(req, res) do - res.json new JsonArray.from(config.stars.users_ratings) + res.api_json(req, new JsonArray.from(config.stars.users_ratings)) end end @@ -103,7 +103,7 @@ class APIUserStars redef fun get(req, res) do var user = get_session_user(req) if user == null then return - res.json new JsonArray.from(user.ratings(config)) + res.api_json(req, new JsonArray.from(user.ratings(config))) end end @@ -115,7 +115,7 @@ class APIStars var login = get_session_login(req) var mentity = mentity_from_uri(req, res) if mentity == null then return - res.json mentity.ratings(config, login) + res.api_json(req, mentity.ratings(config, login)) end end @@ -129,7 +129,7 @@ class APIStarsDimension if mentity == null then return var dimension = req.param("dimension") if dimension == null then return - res.json mentity.ratings_by_dimension(config, dimension, login) + res.api_json(req, mentity.ratings_by_dimension(config, dimension, login)) end redef fun post(req, res) do @@ -166,7 +166,7 @@ class APIStarsDimension else config.stars.save new StarRating(login, mentity.full_name, dimension, rating) end - res.json mentity.ratings_by_dimension(config, dimension, login) + res.api_json(req, mentity.ratings_by_dimension(config, dimension, login)) end end diff --git a/src/web/api_light.nit b/src/web/api_light.nit index 10f0694..8e7bde5 100644 --- a/src/web/api_light.nit +++ b/src/web/api_light.nit @@ -32,6 +32,6 @@ class APILight redef fun post(req, res) do var hl = new HtmlightVisitor var hlcode = hl.highlightcode(req.body) - res.json(hlcode) + res.api_json(req, hlcode) end end diff --git a/src/web/api_metrics.nit b/src/web/api_metrics.nit index 21f84b4..1d0e3bb 100644 --- a/src/web/api_metrics.nit +++ b/src/web/api_metrics.nit @@ -72,7 +72,7 @@ class APIStructuralMetrics res.api_error(404, "No metric for mentity `{mentity.full_name}`") return end - res.json metrics + res.api_json(req, metrics) end end diff --git a/src/web/api_model.nit b/src/web/api_model.nit index 0de4806..fba7196 100644 --- a/src/web/api_model.nit +++ b/src/web/api_model.nit @@ -102,7 +102,7 @@ class APIList var mentities = list_mentities(req) mentities = sort_mentities(req, mentities) mentities = limit_mentities(req, mentities) - res.json new JsonArray.from(mentities) + res.api_json(req, new JsonArray.from(mentities)) end end @@ -121,7 +121,7 @@ class APISearch var page = req.int_arg("p") var limit = req.int_arg("n") var response = new JsonArray.from(search(query, limit)) - res.json paginate(response, response.length, page, limit) + res.api_json(req, paginate(response, response.length, page, limit)) end fun search(query: String, limit: nullable Int): Array[MEntity] do @@ -147,7 +147,7 @@ class APIRandom mentities = filter_mentities(req, mentities) mentities = randomize_mentities(req, mentities) mentities = limit_mentities(req, mentities) - res.json new JsonArray.from(mentities) + res.api_json(req, new JsonArray.from(mentities)) end end @@ -160,7 +160,7 @@ class APIEntity redef fun get(req, res) do var mentity = mentity_from_uri(req, res) if mentity == null then return - res.raw_json mentity.to_full_json(config.view.mainmodule) + res.api_full_json(req, mentity) end end @@ -180,7 +180,7 @@ class APIEntityDoc obj["documentation"] = mdoc.html_documentation.write_to_string obj["location"] = mdoc.location end - res.json obj + res.api_json(req, obj) end end @@ -193,7 +193,7 @@ class APIEntityInheritance redef fun get(req, res) do var mentity = mentity_from_uri(req, res) if mentity == null then return - res.json mentity.hierarchy_poset(config.view)[mentity] + res.api_json(req, mentity.hierarchy_poset(config.view)[mentity]) end end @@ -213,7 +213,7 @@ class APIEntityLinearization end var mentities = new JsonArray for e in lin do mentities.add e - res.json mentities + res.api_json(req, mentities) end end @@ -248,7 +248,8 @@ class APIEntityDefs mentities = filter_mentities(req, mentities) mentities = sort_mentities(req, mentities) mentities = limit_mentities(req, mentities) - res.json new JsonArray.from(mentities) + res.api_json(req, new JsonArray.from(mentities)) + end end end diff --git a/src/web/web_base.nit b/src/web/web_base.nit index 4aad814..6bcb710 100644 --- a/src/web/web_base.nit +++ b/src/web/web_base.nit @@ -137,6 +137,24 @@ redef class HttpResponse json(new APIError(status, message), status) end + # Return `serializable` as a json string + # + # Uses `req` to define serialization options. + fun api_json(req: HttpRequest, serializable: nullable Serializable, status: nullable Int, plain, pretty: nullable Bool) do + json(serializable, status, plain, req.bool_arg("pretty")) + end + + # Return the full version of `serializable` as a json string + # + # Uses `req` to define serializable options. + fun api_full_json(req: HttpRequest, serializable: nullable MEntity, status: nullable Int, plain, pretty: nullable Bool) do + if serializable == null then + json(null, status) + else + raw_json(serializable.serialize_to_full_json( + plain or else true, req.bool_arg("pretty") or else false), status) + end + end # Write data as JSON and set the right content type header. fun raw_json(json: nullable String, status: nullable Int) do header["Content-Type"] = media_types["json"].as(not null) -- 1.7.9.5