Add a base class for http async clients; refine examples.

This commit is contained in:
Chunting Gu
2019-02-12 17:06:26 +08:00
parent 9ecec5de0b
commit 43eaf90621
43 changed files with 803 additions and 507 deletions
+4
View File
@@ -0,0 +1,4 @@
add_executable(http_async_client main.cc)
target_link_libraries(http_async_client webcc ${Boost_LIBRARIES})
target_link_libraries(http_async_client "${CMAKE_THREAD_LIBS_INIT}")
+49
View File
@@ -0,0 +1,49 @@
#include <iostream>
#include "boost/asio/io_context.hpp"
#include "webcc/http_async_client.h"
#include "webcc/logger.h"
// TODO: The program blocks during read response.
// Only HttpBin.org has this issue.
static void Test(boost::asio::io_context& io_context) {
auto request = webcc::HttpRequest::Make(webcc::kHttpGet, "/get",
"httpbin.org");
webcc::HttpAsyncClientPtr client{
new webcc::HttpAsyncClient(io_context)
};
client->SetTimeout(3);
// Response callback.
auto callback = [](webcc::HttpResponsePtr response, webcc::Error error,
bool timed_out) {
if (error == webcc::kNoError) {
std::cout << response->content() << std::endl;
} else {
std::cout << DescribeError(error);
if (timed_out) {
std::cout << " (timed out)";
}
std::cout << std::endl;
}
};
client->Request(request, callback);
}
int main() {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
boost::asio::io_context io_context;
//Test(io_context);
Test(io_context);
io_context.run();
return 0;
}