major cleanup

This commit is contained in:
ineed bots
2023-08-27 01:35:49 -06:00
parent 3f71e99a0b
commit cdad6184dd
21 changed files with 75 additions and 6968 deletions

View File

@ -11,29 +11,24 @@ namespace utils::http
{
struct progress_helper
{
const std::function<void(size_t)>* callback{};
std::exception_ptr exception{};
const std::function<bool(size_t)>* callback{};
};
#pragma warning(push)
#pragma warning(disable: 4244)
int progress_callback(void* clientp, const curl_off_t /*dltotal*/, const curl_off_t dlnow, const curl_off_t /*ultotal*/, const curl_off_t /*ulnow*/)
{
auto* helper = static_cast<progress_helper*>(clientp);
try
if (*helper->callback && !(*helper->callback)(dlnow))
{
if (*helper->callback)
{
(*helper->callback)(dlnow);
}
}
catch (...)
{
helper->exception = std::current_exception();
return -1;
}
return 0;
}
#pragma warning(pop)
size_t write_callback(void* contents, const size_t size, const size_t nmemb, void* userp)
{
@ -45,7 +40,7 @@ namespace utils::http
}
}
std::optional<std::string> get_data(const std::string& url, const headers& headers, const std::function<void(size_t)>& callback)
std::optional<std::string> get_data(const std::string& url, const headers& headers, const std::function<bool(size_t)>& callback)
{
curl_slist* header_list = nullptr;
auto* curl = curl_easy_init();
@ -83,11 +78,6 @@ namespace utils::http
return {std::move(buffer)};
}
if (helper.exception)
{
std::rethrow_exception(helper.exception);
}
return {};
}

View File

@ -8,6 +8,6 @@ namespace utils::http
{
using headers = std::unordered_map<std::string, std::string>;
std::optional<std::string> get_data(const std::string& url, const headers& headers = {}, const std::function<void(size_t)>& callback = {});
std::optional<std::string> get_data(const std::string& url, const headers& headers = {}, const std::function<bool(size_t)>& callback = {});
std::future<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers = {});
}