1
0
mirror of synced 2024-11-24 15:50:16 +01:00

sys: Added postJson Net helper function

This commit is contained in:
WerWolv 2022-12-04 21:50:47 +01:00
parent ab93894442
commit 1347f81236
2 changed files with 27 additions and 6 deletions

View File

@ -38,8 +38,10 @@ namespace hex {
constexpr static u32 DefaultTimeout = 2'000; constexpr static u32 DefaultTimeout = 2'000;
std::future<Response<std::string>> getString(const std::string &url, u32 timeout = DefaultTimeout); std::future<Response<std::string>> getString(const std::string &url, u32 timeout = DefaultTimeout, const std::map<std::string, std::string> &extraHeaders = {}, const std::string &body = {});
std::future<Response<nlohmann::json>> getJson(const std::string &url, u32 timeout = DefaultTimeout); std::future<Response<nlohmann::json>> getJson(const std::string &url, u32 timeout = DefaultTimeout, const std::map<std::string, std::string> &extraHeaders = {}, const std::string &body = {});
std::future<Response<nlohmann::json>> postJson(const std::string &url, u32 timeout = DefaultTimeout, const std::map<std::string, std::string> &extraHeaders = {}, const std::string &body = {});
std::future<Response<std::string>> uploadFile(const std::string &url, const std::fs::path &filePath, u32 timeout = DefaultTimeout); std::future<Response<std::string>> uploadFile(const std::string &url, const std::fs::path &filePath, u32 timeout = DefaultTimeout);
std::future<Response<void>> downloadFile(const std::string &url, const std::fs::path &filePath, u32 timeout = DefaultTimeout); std::future<Response<void>> downloadFile(const std::string &url, const std::fs::path &filePath, u32 timeout = DefaultTimeout);

View File

@ -143,7 +143,7 @@ namespace hex {
return i32(responseCode); return i32(responseCode);
} }
std::future<Response<std::string>> Net::getString(const std::string &url, u32 timeout) { std::future<Response<std::string>> Net::getString(const std::string &url, u32 timeout, const std::map<std::string, std::string> &extraHeaders, const std::string &body) {
this->m_transmissionActive.lock(); this->m_transmissionActive.lock();
return std::async(std::launch::async, [=, this] { return std::async(std::launch::async, [=, this] {
@ -152,7 +152,7 @@ namespace hex {
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); }; ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET");
setCommonSettings(response, url, timeout); setCommonSettings(response, url, timeout, extraHeaders, body);
auto responseCode = execute(); auto responseCode = execute();
@ -160,7 +160,7 @@ namespace hex {
}); });
} }
std::future<Response<nlohmann::json>> Net::getJson(const std::string &url, u32 timeout) { std::future<Response<nlohmann::json>> Net::getJson(const std::string &url, u32 timeout, const std::map<std::string, std::string> &extraHeaders, const std::string &body) {
this->m_transmissionActive.lock(); this->m_transmissionActive.lock();
return std::async(std::launch::async, [=, this] { return std::async(std::launch::async, [=, this] {
@ -169,7 +169,26 @@ namespace hex {
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); }; ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET");
setCommonSettings(response, url, timeout); setCommonSettings(response, url, timeout, extraHeaders, body);
auto responseCode = execute();
if (!responseCode.has_value())
return Response<nlohmann::json> { 0, { } };
else
return Response<nlohmann::json> { responseCode.value_or(0), nlohmann::json::parse(response, nullptr, false, true) };
});
}
std::future<Response<nlohmann::json>> Net::postJson(const std::string &url, u32 timeout, const std::map<std::string, std::string> &extraHeaders, const std::string &body) {
this->m_transmissionActive.lock();
return std::async(std::launch::async, [=, this] {
std::string response;
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "POST");
setCommonSettings(response, url, timeout, extraHeaders, body);
auto responseCode = execute(); auto responseCode = execute();
if (!responseCode.has_value()) if (!responseCode.has_value())