diff --git a/src/API.cpp b/src/API.cpp index f799e2f..4cbf3d3 100644 --- a/src/API.cpp +++ b/src/API.cpp @@ -273,43 +273,37 @@ void NyaAPI::get_path_from_json_api( SourceData* source, std::string url, float timeoutInSeconds, - std::function finished, - std::string apiKey) { - - std::thread([&, source, url, finished, apiKey] { - auto options = WebUtils::URLOptions(url); - options.timeOut = timeoutInSeconds; - if (apiKey != "") { options.headers.emplace("Authorization", apiKey); } + std::function finished, std::string apiKey +) { + if (finished == nullptr) { + return ERROR("Can't get data async without a callback to use it with"); + } + if (source == nullptr) { + ERROR("Source is null"); + return finished(false, ""); + } - auto response = WebUtils::Get( - options - ); + auto options = WebUtils::URLOptions(url); + options.timeOut = timeoutInSeconds; + if (apiKey != "") { options.headers.emplace("Authorization", apiKey); } + + std::thread([propertyName = source->propertyName, options, finished] { + auto response = WebUtils::Get(options); - if (!response.IsSuccessful()) { - if(finished != nullptr) finished(false, ""); - return; - } + if (!response.IsSuccessful()) return finished(false, ""); auto result = response.responseData.has_value(); - if (!result) { - if(finished != nullptr) finished(false, ""); - return; - } + if (!result) return finished(false, ""); auto& document = response.responseData.value(); - if(document.HasParseError() || !document.IsObject()) { - if(finished != nullptr) finished(false, ""); - return; - } + if(document.HasParseError() || !document.IsObject()) return finished(false, ""); - std::string url = ""; - if(document.HasMember(source->propertyName)) - { - url = document.FindMember(source->propertyName)->value.GetString(); - if(finished != nullptr) finished(true, url); + auto itr = document.FindMember(propertyName); + if (itr != document.MemberEnd() && itr->value.IsString()) { + std::string url(itr->value.GetString(), itr->value.GetStringLength()); + return finished(true, url); } else { - if(finished != nullptr) finished(false, ""); - return; + return finished(false, ""); } }).detach(); } diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index d9ec30d..46d861e 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -215,7 +215,7 @@ namespace Nya::Utils { return; } - auto data = response.responseData.value(); + auto& data = response.responseData.value(); if (data.size() == 0) { ERROR("Failed to get the data, data size is 0"); if (onFinished) {