add function for tags endpoint

This commit is contained in:
HJfod 2024-03-24 11:27:28 +02:00
parent 4447153f24
commit c8a603cdbb
2 changed files with 47 additions and 1 deletions

View file

@ -415,3 +415,42 @@ ServerPromise<ByteVector> server::getModLogo(std::string const& id) {
return parseServerProgress(prog, "Downloading logo for " + id);
});
}
ServerPromise<std::unordered_set<std::string>> server::getTags(std::monostate) {
auto req = web::WebRequest();
req.userAgent(getServerUserAgent());
return req.get(getServerAPIBaseURL() + "/tags")
.then<std::unordered_set<std::string>, ServerError>([](auto response) -> Result<std::unordered_set<std::string>, ServerError> {
if (response) {
auto value = response.unwrap();
// Store the code, since the value is moved afterwards
auto code = value.code();
// Parse payload
auto payload = parseServerPayload(std::move(value));
if (!payload) {
return Err(payload.unwrapErr());
}
matjson::Value json = payload.unwrap();
if (!json.is_array()) {
return Err(ServerError(code, "Expected a string array"));
}
std::unordered_set<std::string> tags;
for (auto item : json.as_array()) {
if (!item.is_string()) {
return Err(ServerError(code, "Expected a string array"));
}
tags.insert(item.as_string());
}
return Ok(tags);
}
else {
return Err(parseServerError(response.unwrapErr()));
}
})
.progress<ServerProgress>([](auto prog) {
return parseServerProgress(prog, "Downloading valid tags");
});
}

View file

@ -101,6 +101,9 @@ namespace server {
ServerPromise<ServerModsList> getMods(ModsQuery const& query);
ServerPromise<ServerModMetadata> getMod(std::string const& id);
ServerPromise<ByteVector> getModLogo(std::string const& id);
ServerPromise<std::unordered_set<std::string>> getTags(std::monostate = std::monostate());
// ^^ Note: Any funcs with `std::monostate` parameter is because ServerResultCache expects a single query arg
// Caching for server endpoints
namespace detail {
@ -108,6 +111,7 @@ namespace server {
struct ExtractServerReqParams {
using Result = R;
using Query = Q;
ExtractServerReqParams(ServerPromise<R>(*)(Q)) {}
ExtractServerReqParams(ServerPromise<R>(*)(Q const&)) {}
};
}
@ -209,7 +213,10 @@ namespace server {
return inst;
}
ServerPromise<Result> get(Query const& query) {
ServerPromise<Result> get() requires std::is_same_v<Query, std::monostate> {
return m_cache.get(Query());
}
ServerPromise<Result> get(Query const& query) requires (!std::is_same_v<Query, std::monostate>) {
return m_cache.get(query);
}
ServerPromise<Result> refetch(Query const& query) {