You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
5.0 KiB

// 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 "archive_permission.h"
#include "launchpad.h"
#include "distro_series.h"
#include <iostream>
#include <map>
#include <functional>
#include <nlohmann/json.hpp>
archive_permission::archive_permission()
: explicit_(false),
lp(nullptr) {}
archive_permission::~archive_permission() {}
std::optional<archive_permission> archive_permission::parse(const std::string& json_data) {
archive_permission ap;
ap.parse_json(json_data);
return ap;
}
void archive_permission::parse_json(const std::string& json_data) {
if (json_data.empty()) {
std::cerr << "Error: Empty JSON data for archive_permission::parse_json." << std::endl;
return;
}
try {
auto data = nlohmann::json::parse(json_data);
std::map<std::string, std::function<void(const nlohmann::json&)>> json_map = {
{"archive_link", [this](const nlohmann::json& val) { archive_link = val.get<std::string>(); }},
{"component_name", [this](const nlohmann::json& val) { component_name = val.get<std::string>(); }},
{"date_created", [this](const nlohmann::json& val) { date_created = val.get<std::string>(); }},
{"distro_series_name", [this](const nlohmann::json& val) { distro_series_name = val.get<std::string>(); }},
{"distroseries_link", [this](const nlohmann::json& val) { distroseries_link = val.get<std::string>(); }},
{"explicit", [this](const nlohmann::json& val) { explicit_ = val.get<bool>(); }},
{"package_set_name", [this](const nlohmann::json& val) { package_set_name = val.get<std::string>(); }},
{"permission", [this](const nlohmann::json& val) { permission = val.get<std::string>(); }},
{"person_link", [this](const nlohmann::json& val) { person_link = val.get<std::string>(); }},
{"pocket", [this](const nlohmann::json& val) { pocket = val.get<std::string>(); }},
{"source_package_name", [this](const nlohmann::json& val) { source_package_name = val.get<std::string>(); }}
};
std::map<std::string, std::function<void(const nlohmann::json&)>> readonly_map = {
{"http_etag", [this](const nlohmann::json& val) { http_etag = val.get<std::string>(); }},
{"resource_type_link", [this](const nlohmann::json& val) { resource_type_link = val.get<std::string>(); }},
{"self_link", [this](const nlohmann::json& val) { self_link = val.get<std::string>(); }}
};
for (auto& [key, value] : data.items()) {
try {
if (json_map.find(key) != json_map.end()) {
json_map[key](value);
json_map.erase(key);
} else if (readonly_map.find(key) != readonly_map.end()) {
readonly_map[key](value);
readonly_map.erase(key);
}
} catch (const std::exception&) {
continue;
}
}
} catch (const nlohmann::json::parse_error& e) {
std::cerr << "JSON parse error in archive_permission::parse_json: " << e.what() << std::endl;
std::cerr << "Input JSON was: " << json_data << std::endl;
} catch (const std::exception& e) {
std::cerr << "Unexpected error during JSON parsing: " << e.what() << std::endl;
}
}
void archive_permission::set_lp(launchpad* lp_ptr) {
lp = lp_ptr;
}
std::optional<archive_permission> archive_permission::get() const {
if(self_link.empty())return std::nullopt;
auto resp=lp->api_get(self_link);
if(!resp)return std::nullopt;
return parse(resp.value());
}
bool archive_permission::patch(const nlohmann::json& data) const {
if(self_link.empty())return false;
auto resp=lp->api_patch(self_link,data);
return resp.has_value();
}
bool archive_permission::put(const nlohmann::json& data) const {
if(self_link.empty())return false;
std::string endpoint=self_link+"?ws.op=replace";
auto resp=lp->api_patch(endpoint,data);
return resp.has_value();
}
std::generator<distro_series> archive_permission::getDistroSeries() const {
if (distroseries_link.empty() || !lp) {
co_return;
}
auto response = lp->api_get(distroseries_link);
if (!response.has_value()) {
co_return;
}
auto ds_opt = distro_series::parse(response.value());
if (!ds_opt) {
co_return;
}
ds_opt->set_lp(lp);
co_yield ds_opt.value();
}