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.
launchpadlib-cpp/src/binary_package_publishing_h...

179 lines
9.5 KiB

#include "binary_package_publishing_history.h"
#include "launchpad.h"
#include <iostream>
#include <map>
#include <functional>
#include <nlohmann/json.hpp>
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> 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<std::string, std::function<void(const nlohmann::json&)>> fields = {
{"architecture_specific", [this](const nlohmann::json& val) { architecture_specific = val.get<bool>(); }},
{"archive_link", [this](const nlohmann::json& val) { archive_link = val.get<std::string>(); }},
{"binary_package_name", [this](const nlohmann::json& val) { binary_package_name = val.get<std::string>(); }},
{"binary_package_version", [this](const nlohmann::json& val) { binary_package_version = val.get<std::string>(); }},
{"build_link", [this](const nlohmann::json& val) { build_link = val.get<std::string>(); }},
{"component_name", [this](const nlohmann::json& val) { component_name = val.get<std::string>(); }},
{"copied_from_archive_link", [this](const nlohmann::json& val) { copied_from_archive_link = val.get<std::string>(); }},
{"creator_link", [this](const nlohmann::json& val) { creator_link = val.get<std::string>(); }},
{"date_created", [this](const nlohmann::json& val) { date_created = val.get<std::string>(); }},
{"date_made_pending", [this](const nlohmann::json& val) { date_made_pending = val.get<std::string>(); }},
{"date_published", [this](const nlohmann::json& val) { date_published = val.get<std::string>(); }},
{"date_removed", [this](const nlohmann::json& val) { date_removed = val.get<std::string>(); }},
{"date_superseded", [this](const nlohmann::json& val) { date_superseded = val.get<std::string>(); }},
{"display_name", [this](const nlohmann::json& val) { display_name = val.get<std::string>(); }},
{"distro_arch_series_link", [this](const nlohmann::json& val) { distro_arch_series_link = val.get<std::string>(); }},
{"http_etag", [this](const nlohmann::json& val) { http_etag = val.get<std::string>(); }},
{"is_debug", [this](const nlohmann::json& val) { is_debug = val.get<bool>(); }},
{"phased_update_percentage", [this](const nlohmann::json& val) {
if (!val.is_null()) phased_update_percentage = val.get<int>();
else phased_update_percentage = -1;
}},
{"pocket", [this](const nlohmann::json& val) { pocket = val.get<std::string>(); }},
{"priority_name", [this](const nlohmann::json& val) { priority_name = val.get<std::string>(); }},
{"removal_comment", [this](const nlohmann::json& val) { removal_comment = val.get<std::string>(); }},
{"removed_by_link", [this](const nlohmann::json& val) { removed_by_link = val.get<std::string>(); }},
{"resource_type_link", [this](const nlohmann::json& val) { resource_type_link = val.get<std::string>(); }},
{"scheduled_deletion_date", [this](const nlohmann::json& val) { scheduled_deletion_date = val.get<std::string>(); }},
{"section_name", [this](const nlohmann::json& val) { section_name = val.get<std::string>(); }},
{"self_link", [this](const nlohmann::json& val) { self_link = val.get<std::string>(); }},
{"source_package_name", [this](const nlohmann::json& val) { source_package_name = val.get<std::string>(); }},
{"source_package_version", [this](const nlohmann::json& val) { source_package_version = val.get<std::string>(); }},
{"status", [this](const nlohmann::json& val) { status = val.get<std::string>(); }}
};
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> 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<nlohmann::json> binary_package_publishing_history::binaryFileUrls(bool include_meta) const {
if(self_link.empty())return std::nullopt;
std::map<std::string,std::string>params;
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<nlohmann::json> binary_package_publishing_history::getDailyDownloadTotals(const std::string& start_date, const std::string& end_date) const {
if(self_link.empty())return std::nullopt;
std::map<std::string,std::string>params;
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<int> binary_package_publishing_history::getDownloadCount() const {
if(self_link.empty())return std::nullopt;
std::map<std::string,std::string>params;
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<int>();
return std::nullopt;
}
std::optional<nlohmann::json> binary_package_publishing_history::getDownloadCounts(const std::string& start_date, const std::string& end_date) const {
if(self_link.empty())return std::nullopt;
std::map<std::string,std::string>params;
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> binary_package_publishing_history::changeOverride(const std::optional<std::string>& new_component,
const std::optional<int>& new_phased_update_percentage,
const std::optional<std::string>& new_priority,
const std::optional<std::string>& new_section) const {
if(self_link.empty())return std::nullopt;
std::map<std::string,std::string>params;
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<std::string>& removal_comment) const {
if(self_link.empty())return false;
std::map<std::string,std::string>params;
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();
}