Kurlyk
Toggle main menu visibility
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
9
int
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) {
17
kurlyk::process
();
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
33
KURLYK_PRINT
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();
48
kurlyk::deinit
();
49
50
KURLYK_PRINT
<<
"Request processing completed. Exiting program."
<< std::endl;
51
return
0;
52
}
kurlyk::HttpClient
Concrete HTTP client for making requests to a specific host.
Definition
HttpClient.hpp:17
kurlyk::HttpClient::set_retry_attempts
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets retry attempts and delay between retries for HTTP requests.
Definition
HttpClient.hpp:504
kurlyk::HttpClient::set_timeout
void set_timeout(long timeout)
Sets the timeout duration for HTTP requests.
Definition
HttpClient.hpp:551
kurlyk::HttpClient::set_user_agent
void set_user_agent(const std::string &user_agent)
Sets the User-Agent header.
Definition
HttpClient.hpp:527
kurlyk::HttpClient::get
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
Definition
HttpClient.hpp:681
kurlyk.hpp
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
kurlyk::init
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition
runtime.hpp:13
kurlyk::HttpResponsePtr
std::unique_ptr< HttpResponse > HttpResponsePtr
Owning pointer to an HTTP response.
Definition
HttpResponse.hpp:36
kurlyk::deinit
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition
runtime.hpp:26
kurlyk::Headers
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
Definition
CaseInsensitiveMultimap.hpp:62
kurlyk::process
void process()
Processes pending requests (used in synchronous mode).
Definition
runtime.hpp:32
kurlyk::QueryParams
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
Definition
CaseInsensitiveMultimap.hpp:65
KURLYK_PRINT
#define KURLYK_PRINT
Definition
print_utils.hpp:8
main
int main()
Definition
threaded_request_processing_example.cpp:9
examples
threaded_request_processing_example.cpp
Generated by
1.17.0