Add async-client support; refine http message dump format.
parent
79665c75ba
commit
9bf45e6ecb
@ -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}")
|
@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "boost/asio/io_context.hpp"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/http_async_client.h"
|
||||
|
||||
// In order to test this client, create a file index.html whose content is
|
||||
// simply "Hello, World!", then start a HTTP server with Python 3:
|
||||
// $ python -m http.server
|
||||
// The default port number should be 8000.
|
||||
|
||||
void Test(boost::asio::io_context& ioc) {
|
||||
std::shared_ptr<webcc::HttpRequest> request(new webcc::HttpRequest());
|
||||
|
||||
request->set_method(webcc::kHttpGet);
|
||||
request->set_url("/index.html");
|
||||
request->SetHost("localhost", "8000");
|
||||
|
||||
request->Build();
|
||||
|
||||
webcc::HttpAsyncClientPtr client(new webcc::HttpAsyncClient(ioc));
|
||||
|
||||
// Response handler.
|
||||
auto handler = [](std::shared_ptr<webcc::HttpResponse> response,
|
||||
webcc::Error error) {
|
||||
if (error == webcc::kNoError) {
|
||||
std::cout << response->content() << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(error) << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
client->Request(request, handler);
|
||||
}
|
||||
|
||||
int main() {
|
||||
LOG_INIT(webcc::ERRO, 0);
|
||||
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
Test(ioc);
|
||||
Test(ioc);
|
||||
Test(ioc);
|
||||
|
||||
ioc.run();
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
add_executable(http_client main.cc)
|
||||
|
||||
target_link_libraries(http_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_client "${CMAKE_THREAD_LIBS_INIT}")
|
@ -0,0 +1,38 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/http_client.h"
|
||||
|
||||
// In order to test this client, create a file index.html whose content is
|
||||
// simply "Hello, World!", then start a HTTP server with Python 3:
|
||||
// $ python -m http.server
|
||||
// The default port number should be 8000.
|
||||
|
||||
void Test() {
|
||||
webcc::HttpRequest request;
|
||||
|
||||
request.set_method(webcc::kHttpGet);
|
||||
request.set_url("/index.html");
|
||||
request.SetHost("localhost", "8000");
|
||||
|
||||
request.Build();
|
||||
|
||||
webcc::HttpResponse response;
|
||||
|
||||
webcc::HttpClient client;
|
||||
if (!client.Request(request)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << response.content() << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
LOG_INIT(webcc::ERRO, 0);
|
||||
|
||||
Test();
|
||||
Test();
|
||||
Test();
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
add_executable(rest_book_async_client main.cc)
|
||||
|
||||
target_link_libraries(rest_book_async_client webcc jsoncpp ${Boost_LIBRARIES})
|
||||
target_link_libraries(rest_book_async_client "${CMAKE_THREAD_LIBS_INIT}")
|
@ -0,0 +1,137 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "json/json.h"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/rest_async_client.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Write a JSON object to string.
|
||||
std::string JsonToString(const Json::Value& json) {
|
||||
Json::StreamWriterBuilder builder;
|
||||
return Json::writeString(builder, json);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookListClient {
|
||||
public:
|
||||
BookListClient(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port)
|
||||
: client_(io_context, host, port) {
|
||||
}
|
||||
|
||||
void ListBooks(webcc::HttpResponseHandler handler) {
|
||||
std::cout << "ListBooks" << std::endl;
|
||||
|
||||
client_.Get("/books", handler);
|
||||
}
|
||||
|
||||
void CreateBook(const std::string& id,
|
||||
const std::string& title,
|
||||
double price,
|
||||
webcc::HttpResponseHandler handler) {
|
||||
std::cout << "CreateBook: " << id << " " << title << " " << price
|
||||
<< std::endl;
|
||||
|
||||
Json::Value json(Json::objectValue);
|
||||
json["id"] = id;
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
|
||||
client_.Post("/books", JsonToString(json), handler);
|
||||
}
|
||||
|
||||
private:
|
||||
webcc::RestAsyncClient client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookDetailClient {
|
||||
public:
|
||||
BookDetailClient(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port)
|
||||
: rest_client_(io_context, host, port) {
|
||||
}
|
||||
|
||||
void GetBook(const std::string& id, webcc::HttpResponseHandler handler) {
|
||||
std::cout << "GetBook: " << id << std::endl;
|
||||
|
||||
rest_client_.Get("/book/" + id, handler);
|
||||
}
|
||||
|
||||
void UpdateBook(const std::string& id,
|
||||
const std::string& title,
|
||||
double price,
|
||||
webcc::HttpResponseHandler handler) {
|
||||
std::cout << "UpdateBook: " << id << " " << title << " " << price
|
||||
<< std::endl;
|
||||
|
||||
// NOTE: ID is already in the URL.
|
||||
Json::Value json(Json::objectValue);
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
|
||||
rest_client_.Put("/book/" + id, JsonToString(json), handler);
|
||||
}
|
||||
|
||||
void DeleteBook(const std::string& id, webcc::HttpResponseHandler handler) {
|
||||
std::cout << "DeleteBook: " << id << std::endl;
|
||||
|
||||
rest_client_.Delete("/book/" + id, handler);
|
||||
}
|
||||
|
||||
private:
|
||||
webcc::RestAsyncClient rest_client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <host> <port>" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " localhost 8080" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::ERRO, 0);
|
||||
|
||||
std::string host = argv[1];
|
||||
std::string port = argv[2];
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
BookListClient list_client(io_context, host, port);
|
||||
BookDetailClient detail_client(io_context, host, port);
|
||||
|
||||
// Response handler.
|
||||
auto handler = [](std::shared_ptr<webcc::HttpResponse> response,
|
||||
webcc::Error error) {
|
||||
if (error == webcc::kNoError) {
|
||||
std::cout << response->content() << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(error) << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
list_client.ListBooks(handler);
|
||||
list_client.CreateBook("1", "1984", 12.3, handler);
|
||||
|
||||
detail_client.GetBook("1", handler);
|
||||
detail_client.UpdateBook("1", "1Q84", 32.1, handler);
|
||||
detail_client.GetBook("1", handler);
|
||||
detail_client.DeleteBook("1", handler);
|
||||
|
||||
list_client.ListBooks(handler);
|
||||
|
||||
io_context.run();
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/rest_server.h"
|
||||
|
||||
#include "book_services.h"
|
||||
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <port> [seconds]" << std::endl;
|
||||
std::cout << "If |seconds| is provided, the server will sleep these seconds "
|
||||
"before sending back each response."
|
||||
<< std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " 8080" << std::endl;
|
||||
std::cout << " " << argv0 << " 8080 3" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::VERB, 0);
|
||||
|
||||
unsigned short port = std::atoi(argv[1]);
|
||||
|
||||
int sleep_seconds = 0;
|
||||
if (argc >= 3) {
|
||||
sleep_seconds = std::atoi(argv[2]);
|
||||
}
|
||||
|
||||
std::size_t workers = 2;
|
||||
|
||||
try {
|
||||
webcc::RestServer server(port, workers);
|
||||
|
||||
server.Bind(std::make_shared<BookListService>(sleep_seconds),
|
||||
"/books", false);
|
||||
|
||||
server.Bind(std::make_shared<BookDetailService>(sleep_seconds),
|
||||
"/book/(\\d+)", true);
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/rest_server.h"
|
||||
|
||||
#include "book_services.h"
|
||||
|
||||
static void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <port>" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " 8080" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 2) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::VERB, 0);
|
||||
|
||||
unsigned short port = std::atoi(argv[1]);
|
||||
|
||||
std::size_t workers = 2;
|
||||
|
||||
try {
|
||||
webcc::RestServer server(port, workers);
|
||||
|
||||
server.Bind(std::make_shared<BookListService>(), "/books", false);
|
||||
|
||||
server.Bind(std::make_shared<BookDetailService>(), "/book/(\\d+)", true);
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,7 +1,3 @@
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
file(GLOB SRCS *.cc *.h)
|
||||
|
||||
add_executable(soap_calc_client ${SRCS})
|
@ -1,7 +1,3 @@
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
file(GLOB SRCS *.cc *.h)
|
||||
|
||||
add_executable(soap_calc_server ${SRCS})
|
@ -1,6 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/soap_server.h"
|
||||
|
||||
#include "calc_service.h"
|
||||
|
||||
static void Help(const char* argv0) {
|
@ -0,0 +1,27 @@
|
||||
#include "webcc/rest_async_client.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
void RestAsyncClient::Request(const std::string& method,
|
||||
const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler) {
|
||||
response_handler_ = response_handler;
|
||||
|
||||
HttpRequestPtr request(new webcc::HttpRequest());
|
||||
|
||||
request->set_method(method);
|
||||
request->set_url(url);
|
||||
request->SetHost(host_, port_);
|
||||
|
||||
if (!content.empty()) {
|
||||
request->SetContent(content);
|
||||
}
|
||||
|
||||
request->Build();
|
||||
|
||||
HttpAsyncClientPtr http_client(new HttpAsyncClient(io_context_));
|
||||
http_client->Request(request, response_handler_);
|
||||
}
|
||||
|
||||
} // namespace webcc
|
@ -0,0 +1,60 @@
|
||||
#ifndef WEBCC_REST_ASYNC_CLIENT_H_
|
||||
#define WEBCC_REST_ASYNC_CLIENT_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "webcc/globals.h"
|
||||
#include "webcc/http_async_client.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class RestAsyncClient {
|
||||
public:
|
||||
RestAsyncClient(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port)
|
||||
: io_context_(io_context), host_(host), port_(port) {
|
||||
}
|
||||
|
||||
void Get(const std::string& url,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpGet, url, "", response_handler);
|
||||
}
|
||||
|
||||
void Post(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpPost, url, content, response_handler);
|
||||
}
|
||||
|
||||
void Put(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpPut, url, content, response_handler);
|
||||
}
|
||||
|
||||
void Patch(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpPatch, url, content, response_handler);
|
||||
}
|
||||
|
||||
void Delete(const std::string& url,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpDelete, url, "", response_handler);
|
||||
}
|
||||
|
||||
private:
|
||||
void Request(const std::string& method,
|
||||
const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler);
|
||||
|
||||
boost::asio::io_context& io_context_;
|
||||
std::string host_;
|
||||
std::string port_;
|
||||
HttpResponseHandler response_handler_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_REST_ASYNC_CLIENT_H_
|
Loading…
Reference in New Issue