Kurlyk
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
9namespace {
10
11void finish_once(std::promise<void>& promise) {
12 try {
13 promise.set_value();
14 } catch (...) {
15 }
16}
17
18} // namespace
19
20int 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",
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
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
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
83 return 0;
84}
Concrete HTTP client for making requests to a specific host.
void set_streaming(bool streaming)
Enables or disables intermediate callbacks for response body chunks.
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.
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
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT