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.
80 lines
3.1 KiB
80 lines
3.1 KiB
1 week ago
|
#include "archive_dependency.h"
|
||
|
#include "launchpad.h"
|
||
|
#include <iostream>
|
||
|
#include <nlohmann/json.hpp>
|
||
|
|
||
|
archive_dependency::archive_dependency()
|
||
|
: lp(nullptr) {}
|
||
|
|
||
|
archive_dependency::~archive_dependency() {}
|
||
|
|
||
|
std::optional<archive_dependency> 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<std::string, std::function<void(const nlohmann::json&)>> fields = {
|
||
|
{"archive_link", [this](const nlohmann::json& val) { archive_link = val.get<std::string>(); }},
|
||
|
{"component_name", [this](const nlohmann::json& val) { component_name = val.get<std::string>(); }},
|
||
|
{"date_created", [this](const nlohmann::json& val) { date_created = val.get<std::string>(); }},
|
||
|
{"dependency_link", [this](const nlohmann::json& val) { dependency_link = val.get<std::string>(); }},
|
||
|
{"http_etag", [this](const nlohmann::json& val) { http_etag = val.get<std::string>(); }},
|
||
|
{"pocket", [this](const nlohmann::json& val) { pocket = val.get<std::string>(); }},
|
||
|
{"resource_type_link", [this](const nlohmann::json& val) { resource_type_link = val.get<std::string>(); }},
|
||
|
{"self_link", [this](const nlohmann::json& val) { self_link = val.get<std::string>(); }},
|
||
|
{"snap_base_link", [this](const nlohmann::json& val) { snap_base_link = val.get<std::string>(); }},
|
||
|
{"title", [this](const nlohmann::json& val) { title = val.get<std::string>(); }}
|
||
|
};
|
||
|
|
||
|
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> 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();
|
||
|
}
|