-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from kiwix/catalog_search_url_generation
OpdsCatalog::getSearchUrl()
- Loading branch information
Showing
5 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2021 Veloman Yunkan <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
|
||
#ifndef KIWIX_OPDS_CATALOG_H | ||
#define KIWIX_OPDS_CATALOG_H | ||
|
||
|
||
#include "library.h" | ||
|
||
namespace kiwix | ||
{ | ||
|
||
std::string getSearchUrl(const Filter& f); | ||
|
||
} // namespace kiwix | ||
|
||
#endif // KIWIX_OPDS_CATALOG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2021 Veloman Yunkan <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
|
||
#include "opds_catalog.h" | ||
#include "tools/stringTools.h" | ||
|
||
#include <sstream> | ||
|
||
namespace kiwix | ||
{ | ||
|
||
namespace | ||
{ | ||
const char opdsSearchEndpoint[] = "/catalog/v2/entries"; | ||
|
||
enum Separator { AMP }; | ||
|
||
std::ostringstream& operator<<(std::ostringstream& oss, Separator sep) | ||
{ | ||
if ( oss.tellp() > 0 ) | ||
oss << "&"; | ||
return oss; | ||
} | ||
|
||
std::string buildSearchString(const Filter& f) | ||
{ | ||
std::ostringstream oss; | ||
if ( f.hasQuery() ) | ||
oss << AMP << "q=" << urlEncode(f.getQuery()); | ||
|
||
if ( f.hasCategory() ) | ||
oss << AMP << "category=" << urlEncode(f.getCategory()); | ||
|
||
if ( f.hasLang() ) | ||
oss << AMP << "lang=" << urlEncode(f.getLang()); | ||
|
||
if ( f.hasName() ) | ||
oss << AMP << "name=" << urlEncode(f.getName()); | ||
|
||
if ( !f.getAcceptTags().empty() ) | ||
oss << AMP << "tag=" << urlEncode(join(f.getAcceptTags(), ";")); | ||
|
||
return oss.str(); | ||
} | ||
|
||
} // unnamed namespace | ||
|
||
std::string getSearchUrl(const Filter& f) | ||
{ | ||
const std::string searchString = buildSearchString(f); | ||
|
||
if ( searchString.empty() ) | ||
return opdsSearchEndpoint; | ||
else | ||
return opdsSearchEndpoint + ("?" + searchString); | ||
} | ||
|
||
} // namespace kiwix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ tests = [ | |
'kiwixserve', | ||
'book', | ||
'manager', | ||
'opds_catalog', | ||
] | ||
|
||
if build_machine.system() != 'windows' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2021 Veloman Yunkan <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied | ||
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and | ||
* NON-INFRINGEMENT. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
*/ | ||
|
||
#include "../include/opds_catalog.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
using namespace kiwix; | ||
|
||
TEST(OpdsCatalog, getSearchUrl) | ||
{ | ||
#define EXPECT_SEARCH_URL(url) EXPECT_EQ(url, getSearchUrl(f)) | ||
{ | ||
Filter f; | ||
EXPECT_SEARCH_URL("/catalog/v2/entries"); | ||
} | ||
{ | ||
Filter f; | ||
f.query("abc"); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?q=abc"); | ||
} | ||
{ | ||
Filter f; | ||
f.query("abc def"); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?q=abc%20def"); | ||
} | ||
{ | ||
Filter f; | ||
f.category("ted"); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?category=ted"); | ||
} | ||
{ | ||
Filter f; | ||
f.lang("eng"); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?lang=eng"); | ||
} | ||
{ | ||
Filter f; | ||
f.name("second"); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?name=second"); | ||
} | ||
{ | ||
Filter f; | ||
f.acceptTags({"paper", "plastic"}); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?tag=paper;plastic"); | ||
} | ||
{ | ||
Filter f; | ||
f.query("abc"); | ||
f.category("ted"); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?q=abc&category=ted"); | ||
} | ||
{ | ||
Filter f; | ||
f.category("ted"); | ||
f.query("abc"); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?q=abc&category=ted"); | ||
} | ||
{ | ||
Filter f; | ||
f.query("peru"); | ||
f.category("scifi"); | ||
f.lang("html"); | ||
f.name("edsonarantesdonascimento"); | ||
f.acceptTags({"body", "script"}); | ||
EXPECT_SEARCH_URL("/catalog/v2/entries?q=peru&category=scifi&lang=html&name=edsonarantesdonascimento&tag=body;script"); | ||
} | ||
#undef EXPECT_SEARCH_URL | ||
} |