|
9 | 9 | #include <iostream> |
10 | 10 | #include <algorithm> |
11 | 11 | #include <mutex> |
| 12 | +#include <future> |
12 | 13 | #include <cctype> |
13 | 14 | #include <cpr/cpr.h> |
14 | 15 | #include <nlohmann/json.hpp> |
@@ -160,6 +161,53 @@ public: |
160 | 161 | throw Exception("Expected redirect (3xx) but received " + std::to_string(response.status_code), response.status_code, response.text); |
161 | 162 | } |
162 | 163 |
|
| 164 | + /** |
| 165 | + * @brief Asynchronous version of call<T>. Returns a std::future<T> immediately. |
| 166 | + * The underlying HTTP request runs on a new thread managed by the standard library. |
| 167 | + */ |
| 168 | + template <typename T> |
| 169 | + [[nodiscard]] std::future<T> callAsync(std::string_view method, std::string_view path, |
| 170 | + const std::map<std::string, std::string>& headers = {}, |
| 171 | + const nlohmann::json& params = {}) const { |
| 172 | + // Capture config snapshot eagerly — safe even if Client is reconfigured after dispatch. |
| 173 | + auto config = getConfig(); |
| 174 | + return std::async(std::launch::async, [this, method = std::string(method), |
| 175 | + path = std::string(path), headers, params]() { |
| 176 | + return call<T>(method, path, headers, params); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @brief Asynchronous version of callBytes. Returns a std::future<std::vector<uint8_t>>. |
| 182 | + */ |
| 183 | + [[nodiscard]] std::future<std::vector<uint8_t>> callBytesAsync( |
| 184 | + std::string_view method, std::string_view path, |
| 185 | + const std::map<std::string, std::string>& headers = {}, |
| 186 | + const nlohmann::json& params = {}) const { |
| 187 | + return std::async(std::launch::async, [this, method = std::string(method), |
| 188 | + path = std::string(path), headers, params]() { |
| 189 | + return callBytes(method, path, headers, params); |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * @brief Asynchronous version of fileUpload<T>. Returns a std::future<T>. |
| 195 | + * Progress callbacks are still invoked on the worker thread. |
| 196 | + */ |
| 197 | + template <typename T> |
| 198 | + [[nodiscard]] std::future<T> fileUploadAsync(std::string_view method, std::string_view path, |
| 199 | + const std::string& fileKey, |
| 200 | + const InputFile& inputFile, |
| 201 | + const std::map<std::string, std::string>& headers = {}, |
| 202 | + const nlohmann::json& params = {}, |
| 203 | + const ProgressCallback& onProgress = nullptr) const { |
| 204 | + return std::async(std::launch::async, [this, method = std::string(method), |
| 205 | + path = std::string(path), fileKey, inputFile, |
| 206 | + headers, params, onProgress]() { |
| 207 | + return fileUpload<T>(method, path, fileKey, inputFile, headers, params, onProgress); |
| 208 | + }); |
| 209 | + } |
| 210 | + |
163 | 211 | template <typename T> |
164 | 212 | [[nodiscard]] T fileUpload(std::string_view method, std::string_view path, |
165 | 213 | const std::string& fileKey, |
|
0 commit comments