Kurlyk
Loading...
Searching...
No Matches
delayed_request_example.cpp
Go to the documentation of this file.
1#include <iostream>
2#define KURLYK_AUTO_INIT 0
3#include <kurlyk.hpp>
4#include <future>
5#include <chrono>
6
9 << "ready: " << response->ready << std::endl
10 << "response: " << std::endl
11 << response->content << std::endl
12 << "error_code: " << response->error_code << std::endl
13 << "status_code: " << response->status_code << std::endl
14 << "retry_attempt: " << response->retry_attempt << std::endl
15 << "connect_time: " << response->connect_time << std::endl
16 << "appconnect_time: " << response->appconnect_time << std::endl
17 << "pretransfer_time: " << response->pretransfer_time << std::endl
18 << "starttransfer_time: " << response->starttransfer_time << std::endl
19 << "total_time: " << response->total_time << std::endl
20 << "----------------------------------------" << std::endl;
21}
22
23int main() {
24 kurlyk::init(true);
25 kurlyk::HttpClient client("https://httpbin.org");
26
27 client.set_user_agent("KurlykClient/1.0");
28 client.set_timeout(15); // Set a higher timeout to handle delayed responses
29 client.set_connect_timeout(5);
30 client.set_retry_attempts(2, 1000);
31
32 int delay = 5; // Adjust delay as needed (in seconds)
33 std::string path = "/delay/" + std::to_string(delay);
34
35 // Using std::future for delayed GET request
36 KURLYK_PRINT << "Sending delayed GET request with " << delay << " seconds delay..." << std::endl;
37 auto future_response = client.get(path, kurlyk::QueryParams(), kurlyk::Headers());
38
39 // Process response when it completes
40 auto response = future_response.get();
41 print_response(response);
42
43 KURLYK_PRINT << "Delayed request completed." << std::endl;
45 return 0;
46}
Concrete HTTP client for making requests to a specific host.
void set_connect_timeout(long connect_timeout)
Sets the connection timeout duration.
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets retry attempts and delay between retries for HTTP requests.
void set_timeout(long timeout)
Sets the timeout duration for HTTP requests.
void set_user_agent(const std::string &user_agent)
Sets the User-Agent header.
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
void print_response(const kurlyk::HttpResponsePtr &response)
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition runtime.hpp:13
std::unique_ptr< HttpResponse > HttpResponsePtr
Owning pointer to an HTTP response.
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition runtime.hpp:26
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT