Better endpoint error handling

main
Simon Quigley 1 month ago
parent bc2f365925
commit 57bd56b0d2

@ -211,12 +211,16 @@ bool launchpad::is_authenticated() const {
std::string launchpad::build_full_url(const std::string& endpoint) const {
if (endpoint.rfind("http", 0) == 0) {
return endpoint;
}
} else if (endpoint != "") {
return service_root + "/" + api_version + "/" + endpoint;
}
std::cerr << "endpoint is empty" << std::endl;
return "";
}
std::optional<std::string> launchpad::api_get(const std::string& endpoint, const std::map<std::string, std::string>& params) const {
std::string url = build_full_url(endpoint);
if (url.empty()) { return std::nullopt; }
if (!params.empty()) {
url += "?";
for (const auto& [key, value] : params) {
@ -282,6 +286,7 @@ std::optional<std::string> launchpad::api_post(
const std::string& token_secret_override
) {
std::string url = build_endpoint ? build_full_url(endpoint) : endpoint;
if (url.empty()) { return std::nullopt; }
CURL* curl = curl_easy_init();
if (!curl) {
@ -346,6 +351,7 @@ std::optional<std::string> launchpad::api_post(
std::optional<std::string> launchpad::api_patch(const std::string& endpoint, const nlohmann::json& data) {
std::string url = build_full_url(endpoint) + "?ws.op=edit";
if (url.empty()) { return std::nullopt; }
CURL* curl = curl_easy_init();
if (!curl) {
std::cerr << "Failed to initialize CURL for PATCH request." << std::endl;
@ -399,6 +405,7 @@ std::optional<std::string> launchpad::api_patch(const std::string& endpoint, con
std::optional<std::string> launchpad::api_delete(const std::string& endpoint) {
// We'll try DELETE method:
std::string url = build_full_url(endpoint);
if (url.empty()) { return std::nullopt; }
CURL* curl = curl_easy_init();
if (!curl) {
std::cerr << "Failed to initialize CURL for DELETE request." << std::endl;

Loading…
Cancel
Save