Fix an issue of receiving response; refine error handling.

This commit is contained in:
Adam Gu
2018-06-05 15:00:49 +08:00
parent 6a7aa4cc12
commit 2cc22a80a6
16 changed files with 147 additions and 121 deletions
+4 -4
View File
@@ -19,13 +19,13 @@ class BookListClient {
public:
BookListClient(boost::asio::io_context& io_context,
const std::string& host, const std::string& port)
: client_(io_context, host, port) {
: rest_client_(io_context, host, port) {
}
void ListBooks(webcc::HttpResponseHandler handler) {
std::cout << "ListBooks" << std::endl;
client_.Get("/books", handler);
rest_client_.Get("/books", handler);
}
void CreateBook(const std::string& id,
@@ -40,11 +40,11 @@ class BookListClient {
json["title"] = title;
json["price"] = price;
client_.Post("/books", JsonToString(json), handler);
rest_client_.Post("/books", JsonToString(json), handler);
}
private:
webcc::RestAsyncClient client_;
webcc::RestAsyncClient rest_client_;
};
// -----------------------------------------------------------------------------
+48 -21
View File
@@ -15,29 +15,60 @@ std::string JsonToString(const Json::Value& json) {
// -----------------------------------------------------------------------------
class BookListClient {
class BookClientBase {
public:
BookClientBase(const std::string& host, const std::string& port,
int timeout_seconds)
: rest_client_(host, port) {
rest_client_.set_timeout_seconds(timeout_seconds);
}
virtual ~BookClientBase() = default;
protected:
void PrintSeparateLine() {
std::cout << "--------------------------------";
std::cout << "--------------------------------";
std::cout << std::endl;
}
void PrintError() {
std::cout << webcc::DescribeError(rest_client_.error());
if (rest_client_.timeout_occurred()) {
std::cout << " (timeout)";
}
std::cout << std::endl;
}
webcc::RestClient rest_client_;
};
// -----------------------------------------------------------------------------
class BookListClient : public BookClientBase {
public:
BookListClient(const std::string& host, const std::string& port,
int timeout_seconds)
: client_(host, port) {
client_.set_timeout_seconds(timeout_seconds);
: BookClientBase(host, port, timeout_seconds) {
}
bool ListBooks() {
PrintSeparateLine();
std::cout << "ListBooks" << std::endl;
if (!client_.Get("/books")) {
std::cout << webcc::DescribeError(client_.error()) << std::endl;
if (!rest_client_.Get("/books")) {
PrintError();
return false;
}
std::cout << client_.response_content() << std::endl;
std::cout << rest_client_.response_content() << std::endl;
return true;
}
bool CreateBook(const std::string& id,
const std::string& title,
double price) {
PrintSeparateLine();
std::cout << "CreateBook: " << id << ", " << title << ", " << price
<< std::endl;
@@ -46,35 +77,32 @@ public:
json["title"] = title;
json["price"] = price;
if (!client_.Post("/books", JsonToString(json))) {
std::cout << webcc::DescribeError(client_.error()) << std::endl;
if (!rest_client_.Post("/books", JsonToString(json))) {
PrintError();
return false;
}
std::cout << client_.response_status() << std::endl;
std::cout << rest_client_.response_status() << std::endl;
return true;
}
private:
webcc::RestClient client_;
};
// -----------------------------------------------------------------------------
class BookDetailClient {
class BookDetailClient : public BookClientBase {
public:
BookDetailClient(const std::string& host, const std::string& port,
int timeout_seconds)
: rest_client_(host, port) {
rest_client_.set_timeout_seconds(timeout_seconds);
: BookClientBase(host, port, timeout_seconds) {
}
bool GetBook(const std::string& id) {
PrintSeparateLine();
std::cout << "GetBook: " << id << std::endl;
if (!rest_client_.Get("/book/" + id)) {
std::cout << webcc::DescribeError(rest_client_.error()) << std::endl;
PrintError();
return false;
}
@@ -85,6 +113,7 @@ public:
bool UpdateBook(const std::string& id,
const std::string& title,
double price) {
PrintSeparateLine();
std::cout << "UpdateBook: " << id << ", " << title << ", " << price
<< std::endl;
@@ -94,7 +123,7 @@ public:
json["price"] = price;
if (!rest_client_.Put("/book/" + id, JsonToString(json))) {
std::cout << webcc::DescribeError(rest_client_.error()) << std::endl;
PrintError();
return false;
}
@@ -103,19 +132,17 @@ public:
}
bool DeleteBook(const std::string& id) {
PrintSeparateLine();
std::cout << "DeleteBook: " << id << std::endl;
if (!rest_client_.Delete("/book/" + id)) {
std::cout << webcc::DescribeError(rest_client_.error()) << std::endl;
PrintError();
return false;
}
std::cout << rest_client_.response_status() << std::endl;
return true;
}
private:
webcc::RestClient rest_client_;
};
// -----------------------------------------------------------------------------