Refine REST book examples.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
#include "json/json.h"
|
||||
|
||||
@@ -34,18 +35,24 @@ class BookClientBase {
|
||||
virtual ~BookClientBase() = default;
|
||||
|
||||
protected:
|
||||
// Log the socket communication error.
|
||||
void LogError() {
|
||||
if (rest_client_.timed_out()) {
|
||||
LOG_ERRO("%s (timed out)", webcc::DescribeError(rest_client_.error()));
|
||||
} else {
|
||||
LOG_ERRO(webcc::DescribeError(rest_client_.error()));
|
||||
}
|
||||
}
|
||||
|
||||
//std::cout << webcc::DescribeError(rest_client_.error());
|
||||
//if (rest_client_.timed_out()) {
|
||||
// std::cout << " (timed out)";
|
||||
//}
|
||||
//std::cout << std::endl;
|
||||
// Check HTTP response status.
|
||||
bool CheckStatus(webcc::HttpStatus::Enum expected_status) {
|
||||
int status = rest_client_.response_status();
|
||||
if (status != expected_status) {
|
||||
LOG_ERRO("HTTP status error (actual: %d, expected: %d).",
|
||||
status, expected_status);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
webcc::RestClient rest_client_;
|
||||
@@ -54,19 +61,34 @@ class BookClientBase {
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookListClient : public BookClientBase {
|
||||
public:
|
||||
public:
|
||||
BookListClient(const std::string& host, const std::string& port,
|
||||
int timeout_seconds)
|
||||
: BookClientBase(host, port, timeout_seconds) {
|
||||
}
|
||||
|
||||
bool ListBooks() {
|
||||
bool ListBooks(std::list<Book>* books) {
|
||||
if (!rest_client_.Get("/books")) {
|
||||
// Socket communication error.
|
||||
LogError();
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << rest_client_.response_content() << std::endl;
|
||||
if (!CheckStatus(webcc::HttpStatus::kOK)) {
|
||||
// Response HTTP status error.
|
||||
return false;
|
||||
}
|
||||
|
||||
Json::Value rsp_json = StringToJson(rest_client_.response_content());
|
||||
|
||||
if (!rsp_json.isArray()) {
|
||||
return false; // Should be a JSON array of books.
|
||||
}
|
||||
|
||||
for (Json::ArrayIndex i = 0; i < rsp_json.size(); ++i) {
|
||||
books->push_back(JsonToBook(rsp_json[i]));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -80,6 +102,10 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CheckStatus(webcc::HttpStatus::kCreated)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Json::Value rsp_json = StringToJson(rest_client_.response_content());
|
||||
*id = rsp_json["id"].asString();
|
||||
|
||||
@@ -90,7 +116,7 @@ public:
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookDetailClient : public BookClientBase {
|
||||
public:
|
||||
public:
|
||||
BookDetailClient(const std::string& host, const std::string& port,
|
||||
int timeout_seconds)
|
||||
: BookClientBase(host, port, timeout_seconds) {
|
||||
@@ -102,12 +128,15 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CheckStatus(webcc::HttpStatus::kOK)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return JsonStringToBook(rest_client_.response_content(), book);
|
||||
}
|
||||
|
||||
bool UpdateBook(const std::string& id, const std::string& title,
|
||||
double price) {
|
||||
// NOTE: ID is already in the URL.
|
||||
Json::Value json;
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
@@ -117,9 +146,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
int status = rest_client_.response_status();
|
||||
if (status != webcc::HttpStatus::kOK) {
|
||||
LOG_ERRO("Failed to update book (status: %d).", status);
|
||||
if (!CheckStatus(webcc::HttpStatus::kOK)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -127,14 +154,12 @@ public:
|
||||
}
|
||||
|
||||
bool DeleteBook(const std::string& id) {
|
||||
if (!rest_client_.Delete("/books/0" /*+ id*/)) {
|
||||
if (!rest_client_.Delete("/books/" + id)) {
|
||||
LogError();
|
||||
return false;
|
||||
}
|
||||
|
||||
int status = rest_client_.response_status();
|
||||
if (status != webcc::HttpStatus::kOK) {
|
||||
LOG_ERRO("Failed to delete book (status: %d).", status);
|
||||
if (!CheckStatus(webcc::HttpStatus::kOK)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -148,6 +173,19 @@ void PrintSeparator() {
|
||||
std::cout << std::string(80, '-') << std::endl;
|
||||
}
|
||||
|
||||
void PrintBook(const Book& book) {
|
||||
std::cout << "Book: " << book << std::endl;
|
||||
}
|
||||
|
||||
void PrintBookList(const std::list<Book>& books) {
|
||||
std::cout << "Book list: " << books.size() << std::endl;
|
||||
for (const Book& book : books) {
|
||||
std::cout << " Book: " << book << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <host> <port> [timeout]" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
@@ -176,7 +214,9 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
PrintSeparator();
|
||||
|
||||
list_client.ListBooks();
|
||||
std::list<Book> books;
|
||||
list_client.ListBooks(&books);
|
||||
PrintBookList(books);
|
||||
|
||||
PrintSeparator();
|
||||
|
||||
@@ -184,13 +224,19 @@ int main(int argc, char* argv[]) {
|
||||
if (!list_client.CreateBook("1984", 12.3, &id)) {
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Book ID: " << id << std::endl;
|
||||
|
||||
PrintSeparator();
|
||||
|
||||
books.clear();
|
||||
list_client.ListBooks(&books);
|
||||
PrintBookList(books);
|
||||
|
||||
PrintSeparator();
|
||||
|
||||
Book book;
|
||||
if (detail_client.GetBook(id, &book)) {
|
||||
std::cout << "Book: " << book << std::endl;
|
||||
}
|
||||
detail_client.GetBook(id, &book);
|
||||
PrintBook(book);
|
||||
|
||||
PrintSeparator();
|
||||
|
||||
@@ -198,9 +244,8 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
PrintSeparator();
|
||||
|
||||
if (detail_client.GetBook(id, &book)) {
|
||||
std::cout << "Book " << book << std::endl;
|
||||
}
|
||||
detail_client.GetBook(id, &book);
|
||||
PrintBook(book);
|
||||
|
||||
PrintSeparator();
|
||||
|
||||
@@ -208,7 +253,9 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
PrintSeparator();
|
||||
|
||||
list_client.ListBooks();
|
||||
books.clear();
|
||||
list_client.ListBooks(&books);
|
||||
PrintBookList(books);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user