#include "binary_package_publishing_history.h" #include "launchpad.h" #include #include #include #include binary_package_publishing_history::binary_package_publishing_history() : architecture_specific(false), is_debug(false), phased_update_percentage(-1), lp(nullptr) {} binary_package_publishing_history::~binary_package_publishing_history() {} std::optional binary_package_publishing_history::parse(const std::string& json_data) { binary_package_publishing_history b; b.parse_json(json_data); return b; } void binary_package_publishing_history::parse_json(const std::string& json_data) { if (json_data.empty()) { std::cerr << "Error: Empty JSON data for binary_package_publishing_history::parse_json." << std::endl; return; } try { auto data = nlohmann::json::parse(json_data); std::map> fields = { {"architecture_specific", [this](const nlohmann::json& val) { architecture_specific = val.get(); }}, {"archive_link", [this](const nlohmann::json& val) { archive_link = val.get(); }}, {"binary_package_name", [this](const nlohmann::json& val) { binary_package_name = val.get(); }}, {"binary_package_version", [this](const nlohmann::json& val) { binary_package_version = val.get(); }}, {"build_link", [this](const nlohmann::json& val) { build_link = val.get(); }}, {"component_name", [this](const nlohmann::json& val) { component_name = val.get(); }}, {"copied_from_archive_link", [this](const nlohmann::json& val) { copied_from_archive_link = val.get(); }}, {"creator_link", [this](const nlohmann::json& val) { creator_link = val.get(); }}, {"date_created", [this](const nlohmann::json& val) { date_created = val.get(); }}, {"date_made_pending", [this](const nlohmann::json& val) { date_made_pending = val.get(); }}, {"date_published", [this](const nlohmann::json& val) { date_published = val.get(); }}, {"date_removed", [this](const nlohmann::json& val) { date_removed = val.get(); }}, {"date_superseded", [this](const nlohmann::json& val) { date_superseded = val.get(); }}, {"display_name", [this](const nlohmann::json& val) { display_name = val.get(); }}, {"distro_arch_series_link", [this](const nlohmann::json& val) { distro_arch_series_link = val.get(); }}, {"http_etag", [this](const nlohmann::json& val) { http_etag = val.get(); }}, {"is_debug", [this](const nlohmann::json& val) { is_debug = val.get(); }}, {"phased_update_percentage", [this](const nlohmann::json& val) { if (!val.is_null()) phased_update_percentage = val.get(); else phased_update_percentage = -1; }}, {"pocket", [this](const nlohmann::json& val) { pocket = val.get(); }}, {"priority_name", [this](const nlohmann::json& val) { priority_name = val.get(); }}, {"removal_comment", [this](const nlohmann::json& val) { removal_comment = val.get(); }}, {"removed_by_link", [this](const nlohmann::json& val) { removed_by_link = val.get(); }}, {"resource_type_link", [this](const nlohmann::json& val) { resource_type_link = val.get(); }}, {"scheduled_deletion_date", [this](const nlohmann::json& val) { scheduled_deletion_date = val.get(); }}, {"section_name", [this](const nlohmann::json& val) { section_name = val.get(); }}, {"self_link", [this](const nlohmann::json& val) { self_link = val.get(); }}, {"source_package_name", [this](const nlohmann::json& val) { source_package_name = val.get(); }}, {"source_package_version", [this](const nlohmann::json& val) { source_package_version = val.get(); }}, {"status", [this](const nlohmann::json& val) { status = val.get(); }} }; for (auto& [key, value] : data.items()) { try { if (fields.find(key) != fields.end()) { fields[key](value); fields.erase(key); } } catch (...) { continue; } } } catch (const nlohmann::json::parse_error& e) { std::cerr << "JSON parse error in binary_package_publishing_history::parse_json: " << e.what() << std::endl; } catch (const std::exception& e) { std::cerr << "Unexpected error during JSON parsing: " << e.what() << std::endl; } } void binary_package_publishing_history::set_lp(launchpad* lp_ptr) { lp = lp_ptr; } std::optional binary_package_publishing_history::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 binary_package_publishing_history::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 binary_package_publishing_history::put(const nlohmann::json& data) const { if(self_link.empty())return false; // For PUT, we emulate with POST & ws.op=replace or something similar, but doc says PUT should contain the full representation. // We'll try POST with build_endpoint=false and full JSON. Actually we have no direct method for PUT in code. We'll just do a POST with `?ws.op=replace`. std::string endpoint=self_link; endpoint+="?ws.op=replace"; auto resp=lp->api_patch(endpoint,data); return resp.has_value(); } std::optional binary_package_publishing_history::binaryFileUrls(bool include_meta) const { if(self_link.empty())return std::nullopt; std::mapparams; params["ws.op"]="binaryFileUrls"; if(include_meta)params["include_meta"]="true"; auto resp=lp->api_get(self_link,params); if(!resp)return std::nullopt; return nlohmann::json::parse(resp.value()); } std::optional binary_package_publishing_history::getDailyDownloadTotals(const std::string& start_date, const std::string& end_date) const { if(self_link.empty())return std::nullopt; std::mapparams; params["ws.op"]="getDailyDownloadTotals"; if(!start_date.empty())params["start_date"]=start_date; if(!end_date.empty())params["end_date"]=end_date; auto resp=lp->api_get(self_link,params); if(!resp)return std::nullopt; return nlohmann::json::parse(resp.value()); } std::optional binary_package_publishing_history::getDownloadCount() const { if(self_link.empty())return std::nullopt; std::mapparams; params["ws.op"]="getDownloadCount"; auto resp=lp->api_get(self_link,params); if(!resp)return std::nullopt; auto j=nlohmann::json::parse(resp.value()); if(j.is_number())return j.get(); return std::nullopt; } std::optional binary_package_publishing_history::getDownloadCounts(const std::string& start_date, const std::string& end_date) const { if(self_link.empty())return std::nullopt; std::mapparams; params["ws.op"]="getDownloadCounts"; if(!start_date.empty())params["start_date"]=start_date; if(!end_date.empty())params["end_date"]=end_date; auto resp=lp->api_get(self_link,params); if(!resp)return std::nullopt; return nlohmann::json::parse(resp.value()); } std::optional binary_package_publishing_history::changeOverride(const std::optional& new_component, const std::optional& new_phased_update_percentage, const std::optional& new_priority, const std::optional& new_section) const { if(self_link.empty())return std::nullopt; std::mapparams; params["ws.op"]="changeOverride"; if(new_component.has_value()) params["new_component"]=new_component.value(); if(new_phased_update_percentage.has_value()) params["new_phased_update_percentage"]=std::to_string(new_phased_update_percentage.value()); if(new_priority.has_value()) params["new_priority"]=new_priority.value(); if(new_section.has_value()) params["new_section"]=new_section.value(); auto resp=lp->api_post(self_link,params); if(!resp)return std::nullopt; auto obj=parse(resp.value()); if(obj)obj->set_lp(lp); return obj; } bool binary_package_publishing_history::requestDeletion(const std::optional& removal_comment) const { if(self_link.empty())return false; std::mapparams; params["ws.op"]="requestDeletion"; if(removal_comment.has_value())params["removal_comment"]=removal_comment.value(); auto resp=lp->api_post(self_link,params); return resp.has_value(); }