23 const char* message) {
25 <<
"\n Message: " << message
26 <<
"\n Exception: " << ex.what()
27 <<
"\n Function: " << func
28 <<
"\n File: " << file
29 <<
"\n Line: " << line << std::endl;
34 "https://httpbin.org/delay/5",
38 KURLYK_PRINT <<
"Callback-based GET request response:" << std::endl;
43# if __cplusplus >= 201703L
46 "https://httpbin.org/delay/5",
52 "https://httpbin.org/delay/5",
56 uint64_t request_id2 = func_result.first;
57 auto future_response = std::move(func_result.second);
60 KURLYK_PRINT <<
"Sent two requests. Request IDs: " << request_id1 <<
", " << request_id2 << std::endl;
63 std::this_thread::sleep_for(std::chrono::seconds(1));
64 KURLYK_PRINT <<
"Cancelling the first request (ID: " << request_id1 <<
")..." << std::endl;
66 KURLYK_PRINT <<
"Request 1 cancelled successfully." << std::endl;
70 KURLYK_PRINT <<
"Cancelling the second request (ID: " << request_id2 <<
")..." << std::endl;
75 future_response.get();
76 }
catch (
const std::exception& e) {
77 KURLYK_PRINT <<
"Future-based request exception: " << e.what() << std::endl;
82 for (
int n = 0; n < 10; ++n) {
89 int cancel_after_ms = 3000;
91 std::vector<std::unique_ptr<kurlyk::HttpClient>> clients;
92 std::vector<std::future<kurlyk::HttpResponsePtr>> futures;
94 for (
int i = 0; i < num_clients; ++i) {
95 auto client = std::make_unique<kurlyk::HttpClient>(
"https://httpbin.org");
96 client->set_timeout(5);
97 client->set_connect_timeout(5);
98 client->set_retry_attempts(3, 1000);
102 for (
int j = 0; j < num_req; ++j) {
104 client->set_head_only(
true);
106 client->set_head_only(
false);
111 clients.emplace_back(std::move(client));
112 std::this_thread::sleep_for(std::chrono::milliseconds(100));
115 std::this_thread::sleep_for(std::chrono::milliseconds(cancel_after_ms));
117 for (
int i = 0; i < num_clients; ++i) {
119 auto& client = clients[i];
121 KURLYK_PRINT <<
"Client #" << i <<
" using HEAD request" << std::endl;
122 client->set_head_only(
true);
124 client->set_head_only(
false);
127 KURLYK_PRINT <<
"[Cancel] Starting cancel for client #" << i << std::endl;
128 client->cancel_requests();
129 KURLYK_PRINT <<
"[Cancel] Finished cancel for client #" << i << std::endl;
130 }
catch (
const std::exception& e) {
131 KURLYK_PRINT <<
"[Cancel] Exception for client #" << i <<
": " << e.what() << std::endl;
135 KURLYK_PRINT <<
"[Cancel2] Starting cancel for client #" << i << std::endl;
136 client->cancel_requests();
137 KURLYK_PRINT <<
"[Cancel2] Finished cancel for client #" << i << std::endl;
138 }
catch (
const std::exception& e) {
139 KURLYK_PRINT <<
"[Cancel2] Exception for client #" << i <<
": " << e.what() << std::endl;
144 for (
int i = 0; i < num_clients; ++i) {
146 auto response = futures[i].get();
148 <<
" | Ready: " << response->ready
149 <<
" | Status: " << response->status_code
150 <<
" | Error: " << response->error_code.message()
152 }
catch (
const std::exception& e) {
153 KURLYK_PRINT <<
"[Result] Client #" << i <<
" threw exception: " << e.what() << std::endl;
uint64_t http_get(const std::string &url, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends an asynchronous HTTP GET request with a callback.