#include "archive_dependency.h" #include "launchpad.h" #include #include archive_dependency::archive_dependency() : lp(nullptr) {} archive_dependency::~archive_dependency() {} std::optional archive_dependency::parse(const std::string& json_data) { archive_dependency ad; ad.parse_json(json_data); return ad; } void archive_dependency::parse_json(const std::string& json_data) { if (json_data.empty()) { std::cerr << "Error: Empty JSON data for archive_dependency::parse_json." << std::endl; return; } try { auto data = nlohmann::json::parse(json_data); std::map> fields = { {"archive_link", [this](const nlohmann::json& val) { archive_link = val.get(); }}, {"component_name", [this](const nlohmann::json& val) { component_name = val.get(); }}, {"date_created", [this](const nlohmann::json& val) { date_created = val.get(); }}, {"dependency_link", [this](const nlohmann::json& val) { dependency_link = val.get(); }}, {"http_etag", [this](const nlohmann::json& val) { http_etag = val.get(); }}, {"pocket", [this](const nlohmann::json& val) { pocket = val.get(); }}, {"resource_type_link", [this](const nlohmann::json& val) { resource_type_link = val.get(); }}, {"self_link", [this](const nlohmann::json& val) { self_link = val.get(); }}, {"snap_base_link", [this](const nlohmann::json& val) { snap_base_link = val.get(); }}, {"title", [this](const nlohmann::json& val) { title = val.get(); }} }; for (auto& [key, value] : data.items()) { if (fields.find(key) != fields.end()) { try { fields[key](value); fields.erase(key); } catch (...) { continue; } } } } catch (const nlohmann::json::parse_error& e) { std::cerr << "JSON parse error in archive_dependency::parse_json: " << e.what() << std::endl; } catch (const std::exception& e) { std::cerr << "Unexpected error during JSON parsing: " << e.what() << std::endl; } } void archive_dependency::set_lp(launchpad* lp_ptr) { lp = lp_ptr; } std::optional archive_dependency::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_dependency::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_dependency::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(); }