launchpadlib-cpp/src/source_package_publishing_history.cpp

139 lines
5.3 KiB
C++

// Copyright (C) 2024 Simon Quigley <tsimonq2@ubuntu.com>
//
// 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
// (at your option) 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, see <https://www.gnu.org/licenses/>.
#include "source_package_publishing_history.h"
#include "binary_package_publishing_history.h"
#include "build.h"
#include "launchpad.h"
#include <iostream>
#include <nlohmann/json.hpp>
source_package_publishing_history::source_package_publishing_history()
: source_package_name(""),
source_package_version(""),
self_link(""),
lp(nullptr),
distro_series([this]() -> std::optional<class distro_series> {
if (distro_series_link.empty()) return std::nullopt;
//if (_distro_series) return _distro_series;
auto response = lp ? lp->api_get(distro_series_link) : std::nullopt;
if (!response) return std::nullopt;
auto data = nlohmann::json::parse(response.value());
auto ds = distro_series::parse(data.dump());
if (ds) {
ds->set_lp(lp);
//_distro_series = ds.value();
return ds;
}
return std::nullopt;
})
{}
source_package_publishing_history::~source_package_publishing_history() {}
std::optional<source_package_publishing_history> source_package_publishing_history::parse(const std::string& json_data) {
source_package_publishing_history s;
s.parse_json(json_data);
return s;
}
void source_package_publishing_history::parse_json(const std::string& json_data) {
try {
auto data = nlohmann::json::parse(json_data);
if (data.contains("source_package_name") && data["source_package_name"].is_string()) {
source_package_name = data["source_package_name"].get<std::string>();
}
if (data.contains("source_package_version") && data["source_package_version"].is_string()) {
source_package_version = data["source_package_version"].get<std::string>();
}
if (data.contains("self_link") && data["self_link"].is_string()) {
self_link = data["self_link"].get<std::string>();
}
if (data.contains("distro_series_link") && data["distro_series_link"].is_string()) {
distro_series_link = data["distro_series_link"].get<std::string>();
}
if (data.contains("display_name") && data["display_name"].is_string()) {
display_name = data["display_name"].get<std::string>();
}
} catch (const std::exception& e) {
std::cerr << "Error parsing source JSON: " << e.what() << std::endl;
}
}
std::generator<build> source_package_publishing_history::getBuilds() const {
if (self_link.empty()) { co_return; }
std::map<std::string, std::string> params = {{"ws.op", "getBuilds"}};
auto response = lp->api_get(self_link, params);
if (!response.has_value()) { co_return; }
try {
auto data = nlohmann::json::parse(response.value());
if (data.contains("entries") && data["entries"].is_array()) {
for (auto& entry : data["entries"]) {
auto b_opt = build::parse(entry.dump());
if(!b_opt.has_value()) continue;
b_opt->set_lp(lp);
co_yield b_opt.value();
}
}
} catch (const std::exception& e) {
std::cerr << "Error parsing builds: " << e.what() << std::endl;
}
}
void source_package_publishing_history::set_lp(launchpad* lp_ptr) {
lp = lp_ptr;
}
std::generator<binary_package_publishing_history> source_package_publishing_history::getPublishedBinaries(bool active_binaries_only) const {
std::map<std::string,std::string> params;
params["ws.op"] = "getPublishedBinaries";
if (active_binaries_only) params["active_binaries_only"] = "true";
auto response=lp->api_get(self_link, params);
if(!response) co_return;
auto data = nlohmann::json::parse(response.value());
while (true) {
if (data.contains("entries") && data["entries"].is_array()) {
for (auto &e : data["entries"]) {
auto bpph = binary_package_publishing_history::parse(e.dump());
if (bpph) { bpph->set_lp(lp); co_yield bpph.value(); }
}
}
if(!data.contains("next_collection_link")||data["next_collection_link"].is_null()||data["next_collection_link"]=="")break;
response=lp->api_get(data["next_collection_link"].get<std::string>());
if(!response)break;
data=nlohmann::json::parse(response.value());
}
}
bool source_package_publishing_history::requestDeletion(const std::string& removal_comment) {
std::map<std::string, std::string> params;
params["ws.op"] = "requestDeletion";
params["removal_comment"] = removal_comment;
auto resp = lp->api_post(self_link, params);
return resp.has_value();
}