From 1347f81236a17b1d5061156e0a7a36124c918ba3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 4 Dec 2022 21:50:47 +0100 Subject: [PATCH] sys: Added postJson Net helper function --- lib/libimhex/include/hex/helpers/net.hpp | 6 ++++-- lib/libimhex/source/helpers/net.cpp | 27 ++++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/net.hpp b/lib/libimhex/include/hex/helpers/net.hpp index 5ecf5feb3..8614c3898 100644 --- a/lib/libimhex/include/hex/helpers/net.hpp +++ b/lib/libimhex/include/hex/helpers/net.hpp @@ -38,8 +38,10 @@ namespace hex { constexpr static u32 DefaultTimeout = 2'000; - std::future> getString(const std::string &url, u32 timeout = DefaultTimeout); - std::future> getJson(const std::string &url, u32 timeout = DefaultTimeout); + std::future> getString(const std::string &url, u32 timeout = DefaultTimeout, const std::map &extraHeaders = {}, const std::string &body = {}); + std::future> getJson(const std::string &url, u32 timeout = DefaultTimeout, const std::map &extraHeaders = {}, const std::string &body = {}); + + std::future> postJson(const std::string &url, u32 timeout = DefaultTimeout, const std::map &extraHeaders = {}, const std::string &body = {}); std::future> uploadFile(const std::string &url, const std::fs::path &filePath, u32 timeout = DefaultTimeout); std::future> downloadFile(const std::string &url, const std::fs::path &filePath, u32 timeout = DefaultTimeout); diff --git a/lib/libimhex/source/helpers/net.cpp b/lib/libimhex/source/helpers/net.cpp index a29cd497f..3e80eed06 100644 --- a/lib/libimhex/source/helpers/net.cpp +++ b/lib/libimhex/source/helpers/net.cpp @@ -143,7 +143,7 @@ namespace hex { return i32(responseCode); } - std::future> Net::getString(const std::string &url, u32 timeout) { + std::future> Net::getString(const std::string &url, u32 timeout, const std::map &extraHeaders, const std::string &body) { this->m_transmissionActive.lock(); return std::async(std::launch::async, [=, this] { @@ -152,7 +152,7 @@ namespace hex { ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); }; curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET"); - setCommonSettings(response, url, timeout); + setCommonSettings(response, url, timeout, extraHeaders, body); auto responseCode = execute(); @@ -160,7 +160,7 @@ namespace hex { }); } - std::future> Net::getJson(const std::string &url, u32 timeout) { + std::future> Net::getJson(const std::string &url, u32 timeout, const std::map &extraHeaders, const std::string &body) { this->m_transmissionActive.lock(); return std::async(std::launch::async, [=, this] { @@ -169,7 +169,26 @@ namespace hex { ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); }; 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 { 0, { } }; + else + return Response { responseCode.value_or(0), nlohmann::json::parse(response, nullptr, false, true) }; + }); + } + + std::future> Net::postJson(const std::string &url, u32 timeout, const std::map &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(); if (!responseCode.has_value())