Initial client API V2

This commit is contained in:
Chunting Gu
2019-03-06 11:13:33 +08:00
parent b38b7e6d38
commit 9b8304f06c
38 changed files with 777 additions and 362 deletions
+37 -17
View File
@@ -1,29 +1,49 @@
#include <iostream>
#include "webcc/http_client.h"
#include "webcc/http_client_session.h" // TEST
#include "webcc/logger.h"
static void PrintError(const webcc::HttpClient& client) {
std::cout << webcc::DescribeError(client.error());
if (client.timed_out()) {
std::cout << " (timed out)";
}
std::cout << std::endl;
}
int main() {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
auto request = webcc::HttpRequest::New(webcc::kHttpGet, "/get",
"httpbin.org");
using namespace webcc;
webcc::HttpClient client;
HttpResponsePtr r;
if (client.Request(*request)) {
std::cout << client.response_content() << std::endl;
} else {
PrintError(client);
}
HttpClientSession session;
#if 0
r = session.Request(HttpRequestArgs("GET")
.url("http://httpbin.org/get") // Moved
.parameters({ "key1", "value1", "key2", "value2" }) // Moved
.headers({ "Accept", "application/json" }) // Moved
.buffer_size(1000));
std::cout << r->content() << std::endl;
// If you want to create the args object firstly, there'll be an extra call
// to its move constructor.
// - constructor: HttpRequestArgs("GET")
// - move constructor: auto args = ...
auto args = HttpRequestArgs("GET")
.url("http://httpbin.org/get")
.parameters({ "key1", "value1", "key2", "value2" })
.headers({ "Accept", "application/json" })
.buffer_size(1000);
r = session.Request(std::move(args));
r = session.Get("http://httpbin.org/get",
{ "key1", "value1", "key2", "value2" },
{ "Accept", "application/json" },
HttpRequestArgs().buffer_size(1000));
#endif
r = session.Post("http://httpbin.org/post", "{ 'key': 'value' }", true,
{ "Accept", "application/json" },
HttpRequestArgs().buffer_size(1000));
std::cout << r->content() << std::endl;
return 0;
}