Kurlyk
Toggle main menu visibility
Loading...
Searching...
No Matches
http_streaming_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 <future>
7
#include <iostream>
8
9
namespace
{
10
11
void
finish_once
(std::promise<void>& promise) {
12
try
{
13
promise.set_value();
14
}
catch
(...) {
15
}
16
}
17
18
}
// namespace
19
20
int
main
() {
21
kurlyk::init
(
true
);
22
23
std::promise<void> standalone_done_promise;
24
auto
standalone_done_future = standalone_done_promise.get_future();
25
std::atomic<int> standalone_chunk_count(0);
26
27
const
uint64_t request_id =
kurlyk::http_get
(
28
"http://httpbin.org/stream/5"
,
29
kurlyk::QueryParams
(),
30
kurlyk::Headers
(),
31
true
,
32
[&standalone_done_promise, &standalone_chunk_count](
kurlyk::HttpResponsePtr
response) {
33
if
(!response)
return
;
34
35
if
(response->stream_chunk) {
36
++standalone_chunk_count;
37
KURLYK_PRINT
<<
"standalone chunk: "
<< response->content << std::endl;
38
return
;
39
}
40
if
(!response->ready)
return
;
41
42
KURLYK_PRINT
43
<<
"standalone ready: "
<< std::boolalpha << response->ready << std::endl
44
<<
"status_code: "
<< response->status_code << std::endl
45
<<
"chunks: "
<< standalone_chunk_count.load() << std::endl
46
<<
"error_code: "
<< response->error_code.message() << std::endl;
47
48
finish_once
(standalone_done_promise);
49
});
50
51
KURLYK_PRINT
<<
"Standalone request id: "
<< request_id << std::endl;
52
standalone_done_future.wait_for(std::chrono::seconds(30));
53
54
std::promise<void> client_done_promise;
55
auto
client_done_future = client_done_promise.get_future();
56
std::atomic<int> client_chunk_count(0);
57
58
kurlyk::HttpClient
client(
"http://httpbin.org"
);
59
client.
set_streaming
(
true
);
60
client.
get
(
"/stream/5"
,
kurlyk::QueryParams
(),
kurlyk::Headers
(),
61
[&client_done_promise, &client_chunk_count](
kurlyk::HttpResponsePtr
response) {
62
if
(!response)
return
;
63
64
if
(response->stream_chunk) {
65
++client_chunk_count;
66
KURLYK_PRINT
<<
"client chunk: "
<< response->content << std::endl;
67
return
;
68
}
69
if
(!response->ready)
return
;
70
71
KURLYK_PRINT
72
<<
"client ready: "
<< std::boolalpha << response->ready << std::endl
73
<<
"status_code: "
<< response->status_code << std::endl
74
<<
"chunks: "
<< client_chunk_count.load() << std::endl
75
<<
"error_code: "
<< response->error_code.message() << std::endl;
76
77
finish_once
(client_done_promise);
78
});
79
80
client_done_future.wait_for(std::chrono::seconds(30));
81
82
kurlyk::deinit
();
83
return
0;
84
}
kurlyk::HttpClient
Concrete HTTP client for making requests to a specific host.
Definition
HttpClient.hpp:17
kurlyk::HttpClient::set_streaming
void set_streaming(bool streaming)
Enables or disables intermediate callbacks for response body chunks.
Definition
HttpClient.hpp:444
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
main
int main()
Definition
http_streaming_example.cpp:20
kurlyk.hpp
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
anonymous_namespace{http_streaming_example.cpp}::finish_once
void finish_once(std::promise< void > &promise)
Definition
http_streaming_example.cpp:11
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::http_get
uint64_t http_get(const std::string &url, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends an asynchronous HTTP GET request with a callback.
Definition
utils.hpp:421
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
examples
http_streaming_example.cpp
Generated by
1.17.0