2021-04-18 20:24:42 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
|
|
|
#include <cstring>
|
2021-04-18 20:24:42 +02:00
|
|
|
#include <future>
|
2021-08-31 20:03:08 +02:00
|
|
|
#include <optional>
|
2021-08-28 00:45:59 +02:00
|
|
|
#include <string>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <atomic>
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json_fwd.hpp>
|
2021-12-17 09:52:58 +01:00
|
|
|
#include <curl/system.h>
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2022-01-13 14:33:30 +01:00
|
|
|
#include <hex/helpers/paths.hpp>
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
using CURL = void;
|
|
|
|
struct curl_slist;
|
2021-04-18 20:24:42 +02:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Response {
|
2022-01-22 22:37:52 +01:00
|
|
|
i32 code;
|
2021-08-28 00:45:59 +02:00
|
|
|
T body;
|
2021-04-18 20:24:42 +02:00
|
|
|
};
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
template<>
|
|
|
|
struct Response<void> {
|
2022-01-22 22:37:52 +01:00
|
|
|
i32 code;
|
2021-09-03 02:34:40 +02:00
|
|
|
};
|
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
class Net {
|
|
|
|
public:
|
|
|
|
Net();
|
|
|
|
~Net();
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
std::future<Response<std::string>> getString(const std::string &url, u32 timeout = 2000);
|
|
|
|
std::future<Response<nlohmann::json>> getJson(const std::string &url, u32 timeout = 2000);
|
2021-08-28 00:45:59 +02:00
|
|
|
|
2022-01-13 14:33:30 +01:00
|
|
|
std::future<Response<std::string>> uploadFile(const std::string &url, const fs::path &filePath, u32 timeout = 2000);
|
|
|
|
std::future<Response<void>> downloadFile(const std::string &url, const fs::path &filePath, u32 timeout = 2000);
|
2021-08-28 00:45:59 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
[[nodiscard]] std::string encode(const std::string &input);
|
2021-08-28 16:02:53 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
[[nodiscard]] float getProgress() const { return this->m_progress; }
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
void cancel() { this->m_shouldCancel = true; }
|
|
|
|
|
|
|
|
private:
|
2022-01-24 20:53:17 +01:00
|
|
|
void setCommonSettings(std::string &response, const std::string &url, u32 timeout = 2000, const std::map<std::string, std::string> &extraHeaders = {}, const std::string &body = {});
|
2022-01-22 22:37:52 +01:00
|
|
|
std::optional<i32> execute();
|
2021-08-28 00:45:59 +02:00
|
|
|
|
|
|
|
friend int progressCallback(void *contents, curl_off_t dlTotal, curl_off_t dlNow, curl_off_t ulTotal, curl_off_t ulNow);
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
private:
|
|
|
|
CURL *m_ctx;
|
2021-12-17 09:52:58 +01:00
|
|
|
curl_slist *m_headers = nullptr;
|
2021-08-28 00:45:59 +02:00
|
|
|
|
|
|
|
std::mutex m_transmissionActive;
|
2022-02-01 22:09:44 +01:00
|
|
|
float m_progress = 0.0F;
|
2021-08-28 00:45:59 +02:00
|
|
|
bool m_shouldCancel = false;
|
2021-04-18 20:24:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|