mirror of https://github.com/kiwix/libkiwix.git
Serving /catalog/v2/categories
This commit is contained in:
parent
b259afa408
commit
2e53b51696
|
@ -722,6 +722,8 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2(const RequestContext
|
||||||
|
|
||||||
if (url == "root.xml") {
|
if (url == "root.xml") {
|
||||||
return handle_catalog_v2_root(request);
|
return handle_catalog_v2_root(request);
|
||||||
|
} else if (url == "categories") {
|
||||||
|
return handle_catalog_v2_categories(request);
|
||||||
} else {
|
} else {
|
||||||
return Response::build_404(*this, request, "");
|
return Response::build_404(*this, request, "");
|
||||||
}
|
}
|
||||||
|
@ -744,6 +746,34 @@ 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 = normalizeRootUrl(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
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,7 @@ class InternalServer {
|
||||||
std::unique_ptr<Response> handle_catalog(const RequestContext& request);
|
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(const RequestContext& request);
|
||||||
std::unique_ptr<Response> handle_catalog_v2_root(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_meta(const RequestContext& request);
|
||||||
std::unique_ptr<Response> handle_search(const RequestContext& request);
|
std::unique_ptr<Response> handle_search(const RequestContext& request);
|
||||||
std::unique_ptr<Response> handle_suggest(const RequestContext& request);
|
std::unique_ptr<Response> handle_suggest(const RequestContext& request);
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||||
|
<id>{{feed_id}}</id>
|
||||||
|
<link rel="self"
|
||||||
|
href="{{endpoint_root}}/categories"
|
||||||
|
type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
|
||||||
|
<link rel="start"
|
||||||
|
href="{{endpoint_root}}/root.xml"
|
||||||
|
type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
|
||||||
|
<title>List of categories</title>
|
||||||
|
<updated>{{date}}</updated>
|
||||||
|
|
||||||
|
{{#categories}}
|
||||||
|
<entry>
|
||||||
|
<title>{{name}}</title>
|
||||||
|
<link rel="subsection"
|
||||||
|
href="{{endpoint_root}}/entries?category={{{urlencoded_name}}}"
|
||||||
|
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
|
||||||
|
<updated>{{updated}}</updated>
|
||||||
|
<id>{{id}}</id>
|
||||||
|
<content type="text">All entries with category of '{{name}}'.</content>
|
||||||
|
</entry>
|
||||||
|
{{/categories}}
|
||||||
|
</feed>
|
|
@ -38,3 +38,4 @@ templates/external_blocker_part.html
|
||||||
templates/captured_external.html
|
templates/captured_external.html
|
||||||
opensearchdescription.xml
|
opensearchdescription.xml
|
||||||
catalog_v2_root.xml
|
catalog_v2_root.xml
|
||||||
|
catalog_v2_categories.xml
|
||||||
|
|
|
@ -941,3 +941,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>
|
||||||
|
)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue