Refine REST book examples.

master
Chunting Gu 7 years ago
parent 11551a0193
commit 7f692a9602

@ -26,11 +26,19 @@ Json::Value StringToJson(const std::string& str) {
} }
Json::Value BookToJson(const Book& book) { Json::Value BookToJson(const Book& book) {
Json::Value root; Json::Value json;
root["id"] = book.id; json["id"] = book.id;
root["title"] = book.title; json["title"] = book.title;
root["price"] = book.price; json["price"] = book.price;
return root; return json;
}
Book JsonToBook(const Json::Value& json) {
return {
json["id"].asString(),
json["title"].asString(),
json["price"].asDouble(),
};
} }
std::string BookToJsonString(const Book& book) { std::string BookToJsonString(const Book& book) {
@ -44,9 +52,6 @@ bool JsonStringToBook(const std::string& json_str, Book* book) {
return false; return false;
} }
book->id = json["id"].asString(); *book = JsonToBook(json);
book->title = json["title"].asString();
book->price = json["price"].asDouble();
return true; return true;
} }

@ -12,6 +12,7 @@ std::string JsonToString(const Json::Value& json);
Json::Value StringToJson(const std::string& str); Json::Value StringToJson(const std::string& str);
Json::Value BookToJson(const Book& book); Json::Value BookToJson(const Book& book);
Book JsonToBook(const Json::Value& json);
std::string BookToJsonString(const Book& book); std::string BookToJsonString(const Book& book);
bool JsonStringToBook(const std::string& json_str, Book* book); bool JsonStringToBook(const std::string& json_str, Book* book);

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

@ -14,18 +14,22 @@
static BookStore g_book_store; static BookStore g_book_store;
static void Sleep(int seconds) {
if (seconds > 0) {
LOG_INFO("Sleep %d seconds...", seconds);
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Return all books as a JSON array. // Return all books as a JSON array.
// TODO: Support query parameters.
void BookListService::Get(const webcc::UrlQuery& /*query*/, void BookListService::Get(const webcc::UrlQuery& /*query*/,
webcc::RestResponse* response) { webcc::RestResponse* response) {
if (sleep_seconds_ > 0) { Sleep(sleep_seconds_);
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
Json::Value json(Json::arrayValue); Json::Value json(Json::arrayValue);
for (const Book& book : g_book_store.books()) { for (const Book& book : g_book_store.books()) {
json.append(BookToJson(book)); json.append(BookToJson(book));
} }
@ -36,10 +40,7 @@ void BookListService::Get(const webcc::UrlQuery& /*query*/,
void BookListService::Post(const std::string& request_content, void BookListService::Post(const std::string& request_content,
webcc::RestResponse* response) { webcc::RestResponse* response) {
if (sleep_seconds_ > 0) { Sleep(sleep_seconds_);
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
Book book; Book book;
if (JsonStringToBook(request_content, &book)) { if (JsonStringToBook(request_content, &book)) {
@ -58,17 +59,15 @@ void BookListService::Post(const std::string& request_content,
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void BookDetailService::Get(const std::vector<std::string>& url_sub_matches, void BookDetailService::Get(const webcc::UrlSubMatches& url_sub_matches,
const webcc::UrlQuery& query, const webcc::UrlQuery& query,
webcc::RestResponse* response) { webcc::RestResponse* response) {
if (sleep_seconds_ > 0) { Sleep(sleep_seconds_);
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
if (url_sub_matches.size() != 1) { if (url_sub_matches.size() != 1) {
// TODO: kNotFound? // Using kNotFound means the resource specified by the URL cannot be found.
response->status = webcc::HttpStatus::kBadRequest; // kBadRequest could be another choice.
response->status = webcc::HttpStatus::kNotFound;
return; return;
} }
@ -84,18 +83,13 @@ void BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
response->status = webcc::HttpStatus::kOK; response->status = webcc::HttpStatus::kOK;
} }
// Update a book. void BookDetailService::Put(const webcc::UrlSubMatches& url_sub_matches,
void BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
const std::string& request_content, const std::string& request_content,
webcc::RestResponse* response) { webcc::RestResponse* response) {
if (sleep_seconds_ > 0) { Sleep(sleep_seconds_);
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
if (url_sub_matches.size() != 1) { if (url_sub_matches.size() != 1) {
// TODO: kNotFound? response->status = webcc::HttpStatus::kNotFound;
response->status = webcc::HttpStatus::kBadRequest;
return; return;
} }
@ -113,17 +107,12 @@ void BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
response->status = webcc::HttpStatus::kOK; response->status = webcc::HttpStatus::kOK;
} }
void BookDetailService::Delete( void BookDetailService::Delete(const webcc::UrlSubMatches& url_sub_matches,
const std::vector<std::string>& url_sub_matches,
webcc::RestResponse* response) { webcc::RestResponse* response) {
if (sleep_seconds_ > 0) { Sleep(sleep_seconds_);
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
if (url_sub_matches.size() != 1) { if (url_sub_matches.size() != 1) {
// TODO: kNotFound? response->status = webcc::HttpStatus::kNotFound;
response->status = webcc::HttpStatus::kBadRequest;
return; return;
} }

@ -8,12 +8,6 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// BookListService handles the HTTP GET and returns the book list based on
// query parameters specified in the URL.
// The URL should be like:
// - /books
// - /books?name={BookName}
// The query parameters could be regular expressions.
class BookListService : public webcc::RestListService { class BookListService : public webcc::RestListService {
public: public:
explicit BookListService(int sleep_seconds) explicit BookListService(int sleep_seconds)
@ -22,18 +16,16 @@ class BookListService : public webcc::RestListService {
protected: protected:
// Get a list of books based on query parameters. // Get a list of books based on query parameters.
// URL examples: // Support query parameters.
// - /books void Get(const webcc::UrlQuery& query, webcc::RestResponse* response) final;
// - /books?name={BookName}
void Get(const webcc::UrlQuery& query,
webcc::RestResponse* response) final;
// Create a new book. // Create a new book.
void Post(const std::string& request_content, void Post(const std::string& request_content,
webcc::RestResponse* response) final; webcc::RestResponse* response) final;
private: private:
// Sleep for the client to test timeout control. // Sleep some seconds before send back the response.
// For testing timeout control in client side.
int sleep_seconds_; int sleep_seconds_;
}; };
@ -48,19 +40,23 @@ class BookDetailService : public webcc::RestDetailService {
} }
protected: protected:
void Get(const std::vector<std::string>& url_sub_matches, // Get the detailed information of a book.
void Get(const webcc::UrlSubMatches& url_sub_matches,
const webcc::UrlQuery& query, const webcc::UrlQuery& query,
webcc::RestResponse* response) final; webcc::RestResponse* response) final;
void Put(const std::vector<std::string>& url_sub_matches, // Update a book.
void Put(const webcc::UrlSubMatches& url_sub_matches,
const std::string& request_content, const std::string& request_content,
webcc::RestResponse* response) final; webcc::RestResponse* response) final;
void Delete(const std::vector<std::string>& url_sub_matches, // Delete a book.
void Delete(const webcc::UrlSubMatches& url_sub_matches,
webcc::RestResponse* response) final; webcc::RestResponse* response) final;
private: private:
// Sleep for the client to test timeout control. // Sleep some seconds before send back the response.
// For testing timeout control in client side.
int sleep_seconds_; int sleep_seconds_;
}; };

@ -24,30 +24,34 @@ class RestClient {
timeout_seconds_ = timeout_seconds; timeout_seconds_ = timeout_seconds;
} }
// Requests // HTTP GET request.
// The return value indicates if the socket communication is successful
// or not. If it's failed, check error() and timed_out() for more details.
// For HTTP status, check response_status() instead.
inline bool Get(const std::string& url) { inline bool Get(const std::string& url) {
return Request(kHttpGet, url, ""); return Request(kHttpGet, url, "");
} }
// HTTP POST request.
inline bool Post(const std::string& url, std::string&& content) { inline bool Post(const std::string& url, std::string&& content) {
return Request(kHttpPost, url, std::move(content)); return Request(kHttpPost, url, std::move(content));
} }
// HTTP PUT request.
inline bool Put(const std::string& url, std::string&& content) { inline bool Put(const std::string& url, std::string&& content) {
return Request(kHttpPut, url, std::move(content)); return Request(kHttpPut, url, std::move(content));
} }
// HTTP PATCH request.
inline bool Patch(const std::string& url, std::string&& content) { inline bool Patch(const std::string& url, std::string&& content) {
return Request(kHttpPatch, url, std::move(content)); return Request(kHttpPatch, url, std::move(content));
} }
// HTTP DELETE request.
inline bool Delete(const std::string& url) { inline bool Delete(const std::string& url) {
return Request(kHttpDelete, url, ""); return Request(kHttpDelete, url, "");
} }
// Response & Result
HttpResponsePtr response() const { return response_; } HttpResponsePtr response() const { return response_; }
int response_status() const { int response_status() const {

@ -20,6 +20,9 @@ namespace webcc {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Regex sub-matches of the URL.
typedef std::vector<std::string> UrlSubMatches;
struct RestRequest { struct RestRequest {
// HTTP method (GET, POST, etc.). // HTTP method (GET, POST, etc.).
const std::string& method; const std::string& method;
@ -31,7 +34,7 @@ struct RestRequest {
const std::string& url_query_str; const std::string& url_query_str;
// Regex sub-matches of the URL (usually resource ID's). // Regex sub-matches of the URL (usually resource ID's).
std::vector<std::string> url_sub_matches; UrlSubMatches url_sub_matches;
}; };
struct RestResponse { struct RestResponse {
@ -76,22 +79,22 @@ class RestDetailService : public RestService {
void Handle(const RestRequest& request, RestResponse* response) final; void Handle(const RestRequest& request, RestResponse* response) final;
protected: protected:
virtual void Get(const std::vector<std::string>& url_sub_matches, virtual void Get(const UrlSubMatches& url_sub_matches,
const UrlQuery& query, const UrlQuery& query,
RestResponse* response) { RestResponse* response) {
} }
virtual void Put(const std::vector<std::string>& url_sub_matches, virtual void Put(const UrlSubMatches& url_sub_matches,
const std::string& request_content, const std::string& request_content,
RestResponse* response) { RestResponse* response) {
} }
virtual void Patch(const std::vector<std::string>& url_sub_matches, virtual void Patch(const UrlSubMatches& url_sub_matches,
const std::string& request_content, const std::string& request_content,
RestResponse* response) { RestResponse* response) {
} }
virtual void Delete(const std::vector<std::string>& url_sub_matches, virtual void Delete(const UrlSubMatches& url_sub_matches,
RestResponse* response) { RestResponse* response) {
} }
}; };

Loading…
Cancel
Save