Refine timeout control; refine rest book examples.
This commit is contained in:
@@ -1,4 +1,13 @@
|
||||
add_executable(rest_book_async_client main.cc)
|
||||
set(TARGET_NAME rest_book_async_client)
|
||||
|
||||
target_link_libraries(rest_book_async_client webcc jsoncpp ${Boost_LIBRARIES})
|
||||
target_link_libraries(rest_book_async_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
set(SRCS
|
||||
../common/book.cc
|
||||
../common/book.h
|
||||
../common/book_json.cc
|
||||
../common/book_json.h
|
||||
main.cc)
|
||||
|
||||
add_executable(${TARGET_NAME} ${SRCS})
|
||||
|
||||
target_link_libraries(${TARGET_NAME} webcc jsoncpp ${Boost_LIBRARIES})
|
||||
target_link_libraries(${TARGET_NAME} "${CMAKE_THREAD_LIBS_INIT}")
|
||||
|
||||
@@ -8,18 +8,72 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Write a JSON object to string.
|
||||
std::string JsonToString(const Json::Value& json) {
|
||||
static std::string JsonToString(const Json::Value& json) {
|
||||
Json::StreamWriterBuilder builder;
|
||||
return Json::writeString(builder, json);
|
||||
}
|
||||
|
||||
static Json::Value StringToJson(const std::string& str) {
|
||||
Json::Value json;
|
||||
|
||||
Json::CharReaderBuilder builder;
|
||||
std::stringstream stream(str);
|
||||
std::string errs;
|
||||
if (!Json::parseFromStream(builder, stream, &json, &errs)) {
|
||||
std::cerr << errs << std::endl;
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookListClient {
|
||||
class BookClientBase {
|
||||
public:
|
||||
BookClientBase(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port,
|
||||
int timeout_seconds)
|
||||
: rest_client_(io_context, host, port) {
|
||||
rest_client_.set_timeout_seconds(timeout_seconds);
|
||||
}
|
||||
|
||||
virtual ~BookClientBase() = default;
|
||||
|
||||
protected:
|
||||
void PrintSeparateLine() {
|
||||
std::cout << "--------------------------------";
|
||||
std::cout << "--------------------------------";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// Generic response handler for RestAsyncClient APIs.
|
||||
void GenericHandler(std::function<void(webcc::HttpResponsePtr)> rsp_callback,
|
||||
webcc::HttpResponsePtr response,
|
||||
webcc::Error error,
|
||||
bool timed_out) {
|
||||
if (error != webcc::kNoError) {
|
||||
std::cout << webcc::DescribeError(error);
|
||||
if (timed_out) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
} else {
|
||||
// Call the response callback on success.
|
||||
rsp_callback(response);
|
||||
}
|
||||
}
|
||||
|
||||
webcc::RestAsyncClient rest_client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookListClient : public BookClientBase {
|
||||
public:
|
||||
BookListClient(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port)
|
||||
: rest_client_(io_context, host, port) {
|
||||
const std::string& host, const std::string& port,
|
||||
int timeout_seconds)
|
||||
: BookClientBase(io_context, host, port, timeout_seconds) {
|
||||
}
|
||||
|
||||
void ListBooks(webcc::HttpResponseHandler handler) {
|
||||
@@ -28,38 +82,48 @@ class BookListClient {
|
||||
rest_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;
|
||||
void CreateBook(const std::string& title, double price,
|
||||
std::function<void(std::string)> id_callback) {
|
||||
std::cout << "CreateBook: " << title << " " << price << std::endl;
|
||||
|
||||
Json::Value json(Json::objectValue);
|
||||
json["id"] = id;
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
|
||||
rest_client_.Post("/books", JsonToString(json), handler);
|
||||
}
|
||||
auto rsp_callback = [id_callback](webcc::HttpResponsePtr response) {
|
||||
Json::Value rsp_json = StringToJson(response->content());
|
||||
id_callback(rsp_json["id"].asString());
|
||||
};
|
||||
|
||||
private:
|
||||
webcc::RestAsyncClient rest_client_;
|
||||
rest_client_.Post("/books", JsonToString(json),
|
||||
std::bind(&BookListClient::GenericHandler, this,
|
||||
rsp_callback,
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
std::placeholders::_3));
|
||||
}
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookDetailClient {
|
||||
class BookDetailClient : public BookClientBase {
|
||||
public:
|
||||
BookDetailClient(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port)
|
||||
: rest_client_(io_context, host, port) {
|
||||
const std::string& host, const std::string& port,
|
||||
int timeout_seconds)
|
||||
: BookClientBase(io_context, host, port, timeout_seconds) {
|
||||
}
|
||||
|
||||
void GetBook(const std::string& id, webcc::HttpResponseHandler handler) {
|
||||
std::cout << "GetBook: " << id << std::endl;
|
||||
|
||||
rest_client_.Get("/book/" + id, handler);
|
||||
auto rsp_callback = [](webcc::HttpResponsePtr response) {
|
||||
Json::Value rsp_json = StringToJson(response->content());
|
||||
|
||||
//id_callback(rsp_json["id"].asString());
|
||||
};
|
||||
|
||||
rest_client_.Get("/books/" + id, handler);
|
||||
}
|
||||
|
||||
void UpdateBook(const std::string& id,
|
||||
@@ -74,29 +138,27 @@ class BookDetailClient {
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
|
||||
rest_client_.Put("/book/" + id, JsonToString(json), handler);
|
||||
rest_client_.Put("/books/" + 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);
|
||||
rest_client_.Delete("/books/" + id, handler);
|
||||
}
|
||||
|
||||
private:
|
||||
webcc::RestAsyncClient rest_client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <host> <port>" << std::endl;
|
||||
std::cout << "Usage: " << argv0 << " <host> <port> [timeout]" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " localhost 8080" << std::endl;
|
||||
std::cout << " " << argv0 << " localhost 8080 2" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) {
|
||||
if (argc < 3) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
@@ -106,10 +168,15 @@ int main(int argc, char* argv[]) {
|
||||
std::string host = argv[1];
|
||||
std::string port = argv[2];
|
||||
|
||||
int timeout_seconds = -1;
|
||||
if (argc > 3) {
|
||||
timeout_seconds = std::atoi(argv[3]);
|
||||
}
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
BookListClient list_client(io_context, host, port);
|
||||
BookDetailClient detail_client(io_context, host, port);
|
||||
BookListClient list_client(io_context, host, port, timeout_seconds);
|
||||
BookDetailClient detail_client(io_context, host, port, timeout_seconds);
|
||||
|
||||
// Response handler.
|
||||
auto handler = [](webcc::HttpResponsePtr response, webcc::Error error,
|
||||
@@ -126,16 +193,20 @@ int main(int argc, char* argv[]) {
|
||||
};
|
||||
|
||||
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.CreateBook("1984", 12.3, [](std::string id) {
|
||||
std::cout << "ID: " << id << std::endl;
|
||||
});
|
||||
|
||||
list_client.ListBooks(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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user