Improvements to getting motd and featured data

This commit is contained in:
quaK 2023-06-14 20:19:35 +03:00
parent ef989ae192
commit 161d3105d1
3 changed files with 30 additions and 6 deletions

View File

@ -16,7 +16,33 @@ namespace demonware
{
auto reply = server->create_reply(this->task_id());
const auto motd_content = utils::http::get_data("https://h1.gg/data/motd.json");
const int timeout = 7; // seconds
std::optional<utils::http::result> motd_content;
std::optional<utils::http::result> featured_content;
std::optional<utils::http::result> featured2_content;
auto get_motd = [&motd_content]()
{
motd_content = utils::http::get_data("https://h1.gg/data/motd.json", {}, {}, {}, timeout);
};
auto get_featured = [&featured_content]()
{
featured_content = utils::http::get_data("https://h1.gg/data/featured.json", {}, {}, {}, timeout);
};
auto get_featured2 = [&featured2_content]()
{
featured2_content = utils::http::get_data("https://h1.gg/data/featured2.json", {}, {}, {}, timeout);
};
std::thread get_motd_thread(get_motd);
std::thread get_featured_thread(get_featured);
std::thread get_featured2_thread(get_featured2);
get_motd_thread.join();
get_featured_thread.join();
get_featured2_thread.join();
if (motd_content.has_value())
{
const auto motd = new bdMarketingMessage;
@ -27,7 +53,6 @@ namespace demonware
reply->add(motd);
}
const auto featured_content = utils::http::get_data("https://h1.gg/data/featured.json");
if (featured_content.has_value())
{
const auto featured = new bdMarketingMessage;
@ -38,7 +63,6 @@ namespace demonware
reply->add(featured);
}
const auto featured2_content = utils::http::get_data("https://h1.gg/data/featured2.json");
if (featured2_content.has_value())
{
const auto featured2 = new bdMarketingMessage;

View File

@ -43,7 +43,7 @@ namespace utils::http
}
std::optional<result> get_data(const std::string& url, const std::string& fields,
const headers& headers, const std::function<int(size_t, size_t)>& callback)
const headers& headers, const std::function<int(size_t, size_t)>& callback, int timeout)
{
curl_slist* header_list = nullptr;
auto* curl = curl_easy_init();
@ -77,7 +77,7 @@ namespace utils::http
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
if (!fields.empty())
{

View File

@ -19,7 +19,7 @@ namespace utils::http
using headers = std::unordered_map<std::string, std::string>;
std::optional<result> get_data(const std::string& url, const std::string& fields = {},
const headers& headers = {}, const std::function<int(size_t, size_t)>& callback = {});
const headers& headers = {}, const std::function<int(size_t, size_t)>& callback = {}, int timeout = 0);
std::future<std::optional<result>> get_data_async(const std::string& url, const std::string& fields = {},
const headers& headers = {}, const std::function<int(size_t, size_t)>& callback = {});
}