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.

92 lines
3.4 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 "distribution.h"
#include <iostream>
#include <nlohmann/json.hpp>
distribution::distribution()
: name(""),
description(""),
displayname(""),
title(""),
summary(""),
domain_name(""),
lp(nullptr) {}
distribution::~distribution() {}
std::optional<distribution> distribution::parse(const std::string& json_data) {
distribution d;
d.parse_json(json_data);
return d;
}
void distribution::parse_json(const std::string& json_data) {
try {
auto data = nlohmann::json::parse(json_data);
// Map JSON keys to lambda functions
std::map<std::string, std::function<void(const nlohmann::json&)>> json_map = {
{"name", [this](const nlohmann::json& val) { name = val.get<std::string>(); }},
{"description", [this](const nlohmann::json& val) { description = val.get<std::string>(); }},
{"displayname", [this](const nlohmann::json& val) { displayname = val.get<std::string>(); }},
{"title", [this](const nlohmann::json& val) { title = val.get<std::string>(); }},
{"summary", [this](const nlohmann::json& val) { summary = val.get<std::string>(); }},
{"domain_name", [this](const nlohmann::json& val) { domain_name = val.get<std::string>(); }},
{"self_link", [this](const nlohmann::json& val) { self_link = val.get<std::string>(); }},
{"web_link", [this](const nlohmann::json& val) { web_link = val.get<std::string>(); }},
{"translationgroup_link", [this](const nlohmann::json& val) { translationgroup_link = val.get<std::string>(); }}
};
// Process JSON keys dynamically
for (auto& [key, value] : data.items()) {
try {
if (json_map.find(key) != json_map.end()) {
json_map[key](value);
json_map.erase(key);
}
} catch (...) {
continue;
}
}
} catch (const nlohmann::json::parse_error& e) {
std::cerr << "JSON parse error in distribution::parse_json: " << e.what() << std::endl;
throw;
}
}
std::optional<distro_series> distribution::getSeries(const std::string& name_or_version) {
std::map<std::string, std::string> params = {
{"name_or_version", name_or_version},
{"ws.op", "getSeries"}
};
auto response = lp->api_get(self_link, params);
if (!response.has_value()) {
return std::nullopt;
}
std::optional<distro_series> ds_opt = distro_series::parse(response.value());
if (!ds_opt.has_value()) return std::nullopt;
// Set lp in the returned archive
ds_opt->set_lp(lp);
return ds_opt;
}
void distribution::set_lp(launchpad* lp_ptr) {
lp = lp_ptr;
}