#pragma once #include #include #include #include #include #include #include #include #include #include using CURL = void; struct curl_slist; namespace hex { template struct Response { i32 code; T body; }; template<> struct Response { i32 code; }; class Net { public: Net(); ~Net(); static constexpr 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> uploadFile(const std::string &url, const fs::path &filePath, u32 timeout = DefaultTimeout); std::future> downloadFile(const std::string &url, const fs::path &filePath, u32 timeout = DefaultTimeout); [[nodiscard]] std::string encode(const std::string &input); [[nodiscard]] float getProgress() const { return this->m_progress; } void cancel() { this->m_shouldCancel = true; } private: void setCommonSettings(std::string &response, const std::string &url, u32 timeout = 2000, const std::map &extraHeaders = {}, const std::string &body = {}); std::optional execute(); friend int progressCallback(void *contents, curl_off_t dlTotal, curl_off_t dlNow, curl_off_t ulTotal, curl_off_t ulNow); private: CURL *m_ctx; curl_slist *m_headers = nullptr; std::mutex m_transmissionActive; float m_progress = 0.0F; bool m_shouldCancel = false; }; }