From a9520d6f26702bf851482af5ca0e6bcc45ed9000 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Mon, 13 Sep 2021 10:18:07 +0000 Subject: [PATCH] Add shallow option to library API endpoints --- src/library/entry.cr | 22 +++++------------- src/library/library.cr | 26 +++++++-------------- src/library/title.cr | 53 ++++++++++++++---------------------------- src/routes/api.cr | 27 +++++++++++++-------- 4 files changed, 50 insertions(+), 78 deletions(-) diff --git a/src/library/entry.cr b/src/library/entry.cr index 28b7122..d592e43 100644 --- a/src/library/entry.cr +++ b/src/library/entry.cr @@ -46,28 +46,18 @@ class Entry file.close end - def to_slim_json : String - JSON.build do |json| - json.object do - {% for str in ["zip_path", "title", "size", "id"] %} - json.field {{str}}, @{{str.id}} - {% end %} - json.field "title_id", @book.id - json.field "pages" { json.number @pages } - end - end - end - - def to_json(json : JSON::Builder) + def build_json(json : JSON::Builder, *, slim = false) json.object do {% for str in ["zip_path", "title", "size", "id"] %} json.field {{str}}, @{{str.id}} {% end %} json.field "title_id", @book.id - json.field "display_name", @book.display_name @title - json.field "cover_url", cover_url json.field "pages" { json.number @pages } - json.field "mtime" { json.number @mtime.to_unix } + unless slim + json.field "display_name", @book.display_name @title + json.field "cover_url", cover_url + json.field "mtime" { json.number @mtime.to_unix } + end end end diff --git a/src/library/library.cr b/src/library/library.cr index 9351e60..06b1e0f 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -65,26 +65,18 @@ class Library titles.flat_map &.deep_entries end - def to_slim_json : String - JSON.build do |json| - json.object do - json.field "dir", @dir - json.field "titles" do - json.array do - self.titles.each do |title| - json.raw title.to_slim_json - end - end - end - end - end - end - - def to_json(json : JSON::Builder) + def build_json(json : JSON::Builder, *, slim = false, shallow = false) json.object do json.field "dir", @dir json.field "titles" do - json.raw self.titles.to_json + json.array do + self.titles.each do |title| + raw = JSON.build do |j| + title.build_json j, slim: slim, shallow: shallow + end + json.raw raw + end + end end end end diff --git a/src/library/title.cr b/src/library/title.cr index f1915d4..2d4e347 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -63,24 +63,35 @@ class Title end end - def to_slim_json : String - JSON.build do |json| - json.object do - {% for str in ["dir", "title", "id"] %} + def build_json(json : JSON::Builder, *, slim = false, shallow = false) + json.object do + {% for str in ["dir", "title", "id"] %} json.field {{str}}, @{{str.id}} {% end %} - json.field "signature" { json.number @signature } + json.field "signature" { json.number @signature } + unless slim + json.field "display_name", display_name + json.field "cover_url", cover_url + json.field "mtime" { json.number @mtime.to_unix } + end + unless shallow json.field "titles" do json.array do self.titles.each do |title| - json.raw title.to_slim_json + raw = JSON.build do |j| + title.build_json j, slim: slim, shallow: shallow + end + json.raw raw end end end json.field "entries" do json.array do @entries.each do |entry| - json.raw entry.to_slim_json + raw = JSON.build do |j| + entry.build_json j, slim: slim + end + json.raw raw end end end @@ -98,34 +109,6 @@ class Title end end - def to_json(json : JSON::Builder) - json.object do - {% for str in ["dir", "title", "id"] %} - json.field {{str}}, @{{str.id}} - {% end %} - json.field "signature" { json.number @signature } - json.field "display_name", display_name - json.field "cover_url", cover_url - json.field "mtime" { json.number @mtime.to_unix } - json.field "titles" do - json.raw self.titles.to_json - end - json.field "entries" do - json.raw @entries.to_json - end - json.field "parents" do - json.array do - self.parents.each do |title| - json.object do - json.field "title", title.title - json.field "id", title.id - end - end - end - end - end - end - def titles @title_ids.map { |tid| Library.default.get_title! tid } end diff --git a/src/routes/api.cr b/src/routes/api.cr index b1fb3b3..924085b 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -133,10 +133,12 @@ struct APIRouter end Koa.describe "Returns the book with title `tid`", <<-MD - Supply the `tid` query parameter to strip away "display_name", "cover_url", and "mtime" from the returned object to speed up the loading time + - Supply the `slim` query parameter to strip away "display_name", "cover_url", and "mtime" from the returned object to speed up the loading time + - Supply the `shallow` query parameter to get only the title information, without the list of entries and nested titles MD Koa.path "tid", desc: "Title ID" Koa.query "slim" + Koa.query "shallow" Koa.response 200, schema: "title" Koa.response 404, "Title not found" Koa.tag "library" @@ -146,10 +148,11 @@ struct APIRouter title = Library.default.get_title tid raise "Title ID `#{tid}` not found" if title.nil? - if env.params.query["slim"]? - send_json env, title.to_slim_json - else - send_json env, title.to_json + slim = !env.params.query["slim"]?.nil? + shallow = !env.params.query["shallow"]?.nil? + + json = JSON.build do |j| + title.build_json j, slim: slim, shallow: shallow end rescue e Logger.error e @@ -159,20 +162,24 @@ struct APIRouter end Koa.describe "Returns the entire library with all titles and entries", <<-MD - Supply the `tid` query parameter to strip away "display_name", "cover_url", and "mtime" from the returned object to speed up the loading time + - Supply the `slim` query parameter to strip away "display_name", "cover_url", and "mtime" from the returned object to speed up the loading time + - Supply the `shallow` query parameter to get only the top-level titles, without the nested titles and entries MD Koa.query "slim" + Koa.query "shallow" Koa.response 200, schema: { "dir" => String, "titles" => ["title"], } Koa.tag "library" get "/api/library" do |env| - if env.params.query["slim"]? - send_json env, Library.default.to_slim_json - else - send_json env, Library.default.to_json + slim = !env.params.query["slim"]?.nil? + shallow = !env.params.query["shallow"]?.nil? + + json = JSON.build do |j| + Library.default.build_json j, slim: slim, shallow: shallow end + send_json env, json end Koa.describe "Triggers a library scan"