Skip to content

Commit

Permalink
Serving /catalog/v2/categories
Browse files Browse the repository at this point in the history
  • Loading branch information
veloman-yunkan committed Apr 16, 2021
1 parent 4be02d1 commit e5ce43c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2(const RequestContext

if (url == "root.xml") {
return handle_catalog_v2_root(request);
} else if (url == "categories") {
return handle_catalog_v2_categories(request);
} else {
return Response::build_404(*this, request, "");
}
Expand All @@ -784,6 +786,37 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_root(const RequestCo
);
}

std::unique_ptr<Response> InternalServer::handle_catalog_v2_categories(const RequestContext& request)
{
const std::string root_url = (m_root.empty() || m_root[0] == '/')
? m_root
: "/" + m_root;

const auto now = gen_date_str();
kainjow::mustache::list categoryData;
for ( const auto& category : mp_library->getBooksCategories() ) {
const auto urlencodedCategoryName = urlEncode(category);
categoryData.push_back(kainjow::mustache::object{
{"name", category},
{"urlencoded_name", urlencodedCategoryName},
{"updated", now},
{"id", gen_uuid(m_server_id + "/categories/" + urlencodedCategoryName)}
});
}

return ContentResponse::build(
*this,
RESOURCE::catalog_v2_categories_xml,
kainjow::mustache::object{
{"date", now},
{"endpoint_root", root_url + "/catalog/v2"},
{"feed_id", gen_uuid(m_server_id + "/categories")},
{"categories", categoryData }
},
"application/atom+xml;profile=opds-catalog;kind=navigation"
);
}

namespace
{

Expand Down
1 change: 1 addition & 0 deletions src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class InternalServer {
std::unique_ptr<Response> handle_catalog(const RequestContext& request);
std::unique_ptr<Response> handle_catalog_v2(const RequestContext& request);
std::unique_ptr<Response> handle_catalog_v2_root(const RequestContext& request);
std::unique_ptr<Response> handle_catalog_v2_categories(const RequestContext& request);
std::unique_ptr<Response> handle_meta(const RequestContext& request);
std::unique_ptr<Response> handle_search(const RequestContext& request);
std::unique_ptr<Response> handle_suggest(const RequestContext& request);
Expand Down
1 change: 1 addition & 0 deletions static/resources_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ templates/external_blocker_part.html
templates/captured_external.html
opensearchdescription.xml
catalog_v2_root.xml
catalog_v2_categories.xml
40 changes: 40 additions & 0 deletions test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,43 @@ R"(<?xml version="1.0" encoding="UTF-8"?>
)"
);
}

TEST_F(LibraryServerTest, catalog_v2_categories)
{
const auto r = zfs1_->GET("/catalog/v2/categories");
EXPECT_EQ(r->status, 200);
EXPECT_EQ(maskVariableOPDSFeedData(r->body),
R"(<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>12345678-90ab-cdef-1234-567890abcdef</id>
<link rel="self"
href="/catalog/v2/categories"
type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="start"
href="/catalog/v2/root.xml"
type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<title>List of categories</title>
<updated>YYYY-MM-DDThh:mm:ssZ</updated>
<entry>
<title>jazz</title>
<link rel="subsection"
href="/catalog/v2/entries?category=jazz"
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
<updated>YYYY-MM-DDThh:mm:ssZ</updated>
<id>12345678-90ab-cdef-1234-567890abcdef</id>
<content type="text">All entries with category of 'jazz'.</content>
</entry>
<entry>
<title>wikipedia</title>
<link rel="subsection"
href="/catalog/v2/entries?category=wikipedia"
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
<updated>YYYY-MM-DDThh:mm:ssZ</updated>
<id>12345678-90ab-cdef-1234-567890abcdef</id>
<content type="text">All entries with category of 'wikipedia'.</content>
</entry>
</feed>
)"
);
}

0 comments on commit e5ce43c

Please sign in to comment.