Kurlyk
Loading...
Searching...
No Matches
async_multi_request_example.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <kurlyk.hpp>
3#include <future>
4#include <vector>
5#include <thread>
6#include <chrono>
7
10 << "ready: " << response->ready << std::endl
11 << "response: " << std::endl
12 << response->content << std::endl
13 << "error_code: " << response->error_code << std::endl
14 << "status_code: " << response->status_code << std::endl
15 << "retry_attempt: " << response->retry_attempt << std::endl
16 << "----------------------------------------" << std::endl;
17}
18
19void handle_async_responses(std::vector<std::future<kurlyk::HttpResponsePtr>>& futures) {
20 try {
21 for (auto& future : futures) {
22 kurlyk::HttpResponsePtr response = future.get();
23 print_response(response);
24 }
25 } catch(...) {
26 KURLYK_PRINT << "Error while retrieving async responses." << std::endl;
27 throw;
28 }
29 KURLYK_PRINT << "All async requests completed in separate thread." << std::endl;
30}
31
32int main() {
33 kurlyk::init(true);
34 kurlyk::HttpClient client("https://httpbin.org");
35
36 // Set client parameters
37 client.set_user_agent("KurlykClient/1.0");
38 client.set_timeout(2); // Request timeout in seconds
39 client.set_connect_timeout(2); // Connection timeout in seconds
40 client.set_retry_attempts(3, 1000); // 3 attempts with a delay of 1 second
41 client.set_rate_limit(5, 1000); // 5 requests per second limit
42
43 // GET request with callback handler
44 KURLYK_PRINT << "Sending GET request with callback..." << std::endl;
45 client.get("/ip", kurlyk::QueryParams(), kurlyk::Headers(),
46 [](const kurlyk::HttpResponsePtr response) {
47 print_response(response);
48 });
49
50 // POST request with callback handler
51 KURLYK_PRINT << "Sending POST request with callback..." << std::endl;
52 kurlyk::Headers post_headers;
53 post_headers.emplace("Custom-Header", "HeaderValue");
54
55 client.post("/post", kurlyk::QueryParams(), {{"Content-Type", "application/json"}}, "{\"text\":\"Sample POST Content\"}",
56 [](const kurlyk::HttpResponsePtr response) {
57 print_response(response);
58 });
59
60 // Asynchronous GET requests using std::future
61 KURLYK_PRINT << "Sending multiple async GET requests with std::future..." << std::endl;
62 std::vector<std::future<kurlyk::HttpResponsePtr>> futures;
63 for (int i = 0; i < 3; ++i) {
64 futures.push_back(client.get("/get", kurlyk::QueryParams{{"param", std::to_string(i)}}, kurlyk::Headers()));
65 }
66 // Asynchronous POST request using std::future
67 KURLYK_PRINT << "Sending async POST request with std::future..." << std::endl;
68 auto future_post = client.post("/post", kurlyk::QueryParams(), kurlyk::Headers{{"Async-Header", "AsyncValue"}}, "Async POST Content");
69 futures.push_back(std::move(future_post));
70
71 // Start a separate thread to wait for all async responses
72 std::thread async_thread(handle_async_responses, std::ref(futures));
73 async_thread.join(); // Wait for the async thread to complete
74
75 // std::cin.get();
76 KURLYK_PRINT << "All requests completed." << std::endl;
77
79 return 0;
80}
void print_response(const kurlyk::HttpResponsePtr &response)
void handle_async_responses(std::vector< std::future< kurlyk::HttpResponsePtr > > &futures)
A client class for making HTTP requests to a specific host.
void set_rate_limit(long requests_per_period, long period_ms, RateLimitType type=RateLimitType::RL_GENERAL)
Sets the rate limit for HTTP requests.
bool post(const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, HttpResponseCallback callback)
Sends a POST request.
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.
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
A unique pointer to an HttpResponse object for memory management.
void deinit()
Deinitializes the Kurlyk library, stopping the network worker and releasing resources.
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