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