#include "distro_arch_series.h" #include "launchpad.h" #include "utils.h" #include #include distro_arch_series::distro_arch_series() : enabled(false), is_nominated_arch_indep(false), official(false), package_count(0), supports_virtualized(false), lp(nullptr) {} distro_arch_series::~distro_arch_series() {} std::optional distro_arch_series::parse(const std::string& json_data) { distro_arch_series das; das.parse_json(json_data); return das; } void distro_arch_series::parse_json(const std::string& json_data) { if (json_data.empty()) { std::cerr << "Error: Empty JSON data for distro_arch_series::parse_json." << std::endl; return; } try { auto data = nlohmann::json::parse(json_data); auto get_bool = [&](const std::string& key, bool& field) { if (data.contains(key) && data[key].is_boolean()) field = data[key].get(); }; auto get_str = [&](const std::string& key, std::string& field) { if (data.contains(key) && data[key].is_string()) field = data[key].get(); }; auto get_int = [&](const std::string& key, int& field) { if (data.contains(key) && data[key].is_number()) field = data[key].get(); }; get_str("architecture_tag", architecture_tag); get_str("chroot_url", chroot_url); get_str("display_name", display_name); get_str("distroseries_link", distroseries_link); get_bool("enabled", enabled); get_str("http_etag", http_etag); get_bool("is_nominated_arch_indep", is_nominated_arch_indep); get_str("main_archive_link", main_archive_link); get_bool("official", official); get_str("owner_link", owner_link); get_int("package_count", package_count); get_str("processor_link", processor_link); get_str("resource_type_link", resource_type_link); get_str("self_link", self_link); get_bool("supports_virtualized", supports_virtualized); get_str("title", title); get_str("web_link", web_link); } catch (const nlohmann::json::parse_error& e) { std::cerr << "JSON parse error in distro_arch_series::parse_json: " << e.what() << std::endl; } catch (const std::exception& e) { std::cerr << "Unexpected error during JSON parsing: " << e.what() << std::endl; } } void distro_arch_series::set_lp(launchpad* lp_ptr) { lp = lp_ptr; } std::optional distro_arch_series::get() const { if (self_link.empty()) return std::nullopt; auto resp = lp->api_get(self_link); if (!resp) return std::nullopt; auto das = parse(resp.value()); if (das) das->set_lp(lp); return das; } bool distro_arch_series::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 distro_arch_series::put(const nlohmann::json& data) const { if (self_link.empty()) return false; // Emulate PUT with ws.op=replace std::string endpoint = self_link + "?ws.op=replace"; auto resp = lp->api_patch(endpoint, data); return resp.has_value(); } std::generator distro_arch_series::getBuildRecords(const std::string& build_state, const std::string& pocket, const std::string& source_name) const { if (self_link.empty()) co_return; std::map params; params["ws.op"] = "getBuildRecords"; if(!build_state.empty()) params["build_state"] = build_state; if(!pocket.empty()) params["pocket"] = pocket; if(!source_name.empty()) params["source_name"] = source_name; 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"]) { co_yield e; } } 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()); if(!response) break; data = nlohmann::json::parse(response.value()); } } std::optional distro_arch_series::getChrootHash(const std::string& image_type, const std::string& pocket) const { if (self_link.empty()) return std::nullopt; std::map params; params["ws.op"] = "getChrootHash"; if(!image_type.empty()) params["image_type"] = image_type; if(!pocket.empty()) params["pocket"] = pocket; auto resp = lp->api_get(self_link, params); if(!resp)return std::nullopt; return nlohmann::json::parse(resp.value()); } std::optional distro_arch_series::getChrootURL(const std::optional& image_type, const std::optional& pocket) const { if (self_link.empty()) return std::nullopt; std::map params; params["ws.op"] = "getChrootURL"; if(image_type.has_value()) params["image_type"] = image_type.value(); if(pocket.has_value()) params["pocket"] = pocket.value(); auto resp = lp->api_get(self_link, params); if(!resp)return std::nullopt; auto j = nlohmann::json::parse(resp.value()); if(j.is_string()) return j.get(); return std::nullopt; } std::optional distro_arch_series::getSourceFilter() const { if (self_link.empty()) return std::nullopt; std::map params; params["ws.op"] = "getSourceFilter"; auto resp=lp->api_get(self_link,params); if(!resp)return std::nullopt; return nlohmann::json::parse(resp.value()); } bool distro_arch_series::removeChroot(const std::optional& image_type, const std::optional& pocket) const { if(self_link.empty())return false; std::mapparams; params["ws.op"]="removeChroot"; if(image_type.has_value()) params["image_type"]=image_type.value(); if(pocket.has_value()) params["pocket"]=pocket.value(); auto resp=lp->api_post(self_link,params); return resp.has_value(); } bool distro_arch_series::removeSourceFilter() const { if(self_link.empty())return false; std::mapparams; params["ws.op"]="removeSourceFilter"; auto resp=lp->api_post(self_link,params); return resp.has_value(); } bool distro_arch_series::setChroot(const std::string& data_link, const std::optional& image_type, const std::optional& pocket, const std::string& sha1sum) const { if(self_link.empty())return false; std::mapparams; params["ws.op"]="setChroot"; params["data"]=data_link; if(image_type.has_value()) params["image_type"]=image_type.value(); if(pocket.has_value()) params["pocket"]=pocket.value(); params["sha1sum"]=sha1sum; auto resp=lp->api_post(self_link,params); return resp.has_value(); } bool distro_arch_series::setChrootFromBuild(const std::string& filename, const std::string& livefsbuild_link, const std::optional& image_type, const std::optional& pocket) const { if(self_link.empty())return false; std::mapparams; params["ws.op"]="setChrootFromBuild"; params["filename"]=filename; params["livefsbuild"]=livefsbuild_link; if(image_type.has_value()) params["image_type"]=image_type.value(); if(pocket.has_value()) params["pocket"]=pocket.value(); auto resp=lp->api_post(self_link,params); return resp.has_value(); } bool distro_arch_series::setSourceFilter(const std::string& packageset, const std::string& sense) const { if(self_link.empty())return false; std::mapparams; params["ws.op"]="setSourceFilter"; params["packageset"]=packageset; params["sense"]=sense; // must be "Include" or "Exclude" auto resp=lp->api_post(self_link,params); return resp.has_value(); }