Kurlyk
Loading...
Searching...
No Matches
threaded_request_processing_example.cpp
Go to the documentation of this file.
1#define KURLYK_AUTO_INIT 0
2#include <kurlyk.hpp>
3
4#include <atomic>
5#include <chrono>
6#include <iostream>
7#include <thread>
8
9int main() {
10 kurlyk::init(false);
11
12 std::atomic<bool> running(true);
13 std::atomic<bool> completed(false);
14
15 std::thread processing_thread([&running]() {
16 while (running) {
18 std::this_thread::sleep_for(std::chrono::milliseconds(50));
19 }
20 });
21
22 {
23 kurlyk::HttpClient client("https://httpbin.org");
24 client.set_user_agent("KurlykClient/1.0");
25 client.set_timeout(10);
26 client.set_retry_attempts(3, 1000);
27
28 KURLYK_PRINT << "Sending GET request..." << std::endl;
29 client.get("/ip", kurlyk::QueryParams(), kurlyk::Headers(),
30 [&completed](const kurlyk::HttpResponsePtr response) {
31 if (!response || !response->ready) return;
32
34 << "GET Response Content: " << response->content << std::endl
35 << "Status Code: " << response->status_code << std::endl
36 << "Error: " << response->error_code.message() << std::endl;
37 completed = true;
38 });
39
40 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(30);
41 while (!completed && std::chrono::steady_clock::now() < deadline) {
42 std::this_thread::sleep_for(std::chrono::milliseconds(50));
43 }
44 }
45
46 running = false;
47 processing_thread.join();
49
50 KURLYK_PRINT << "Request processing completed. Exiting program." << std::endl;
51 return 0;
52}
Concrete HTTP client for making requests to a specific host.
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.
void process()
Processes pending requests (used in synchronous mode).
Definition runtime.hpp:32
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT