Refine REST book examples.

This commit is contained in:
Chunting Gu
2018-08-28 13:03:25 +08:00
parent 11551a0193
commit 7f692a9602
7 changed files with 138 additions and 93 deletions
+22 -33
View File
@@ -14,18 +14,22 @@
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.
// TODO: Support query parameters.
void BookListService::Get(const webcc::UrlQuery& /*query*/,
webcc::RestResponse* response) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
Sleep(sleep_seconds_);
Json::Value json(Json::arrayValue);
for (const Book& book : g_book_store.books()) {
json.append(BookToJson(book));
}
@@ -36,10 +40,7 @@ void BookListService::Get(const webcc::UrlQuery& /*query*/,
void BookListService::Post(const std::string& request_content,
webcc::RestResponse* response) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
Sleep(sleep_seconds_);
Book 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,
webcc::RestResponse* response) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
Sleep(sleep_seconds_);
if (url_sub_matches.size() != 1) {
// TODO: kNotFound?
response->status = webcc::HttpStatus::kBadRequest;
// Using kNotFound means the resource specified by the URL cannot be found.
// kBadRequest could be another choice.
response->status = webcc::HttpStatus::kNotFound;
return;
}
@@ -84,18 +83,13 @@ void BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
response->status = webcc::HttpStatus::kOK;
}
// Update a book.
void BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
void BookDetailService::Put(const webcc::UrlSubMatches& url_sub_matches,
const std::string& request_content,
webcc::RestResponse* response) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
Sleep(sleep_seconds_);
if (url_sub_matches.size() != 1) {
// TODO: kNotFound?
response->status = webcc::HttpStatus::kBadRequest;
response->status = webcc::HttpStatus::kNotFound;
return;
}
@@ -113,17 +107,12 @@ void BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
response->status = webcc::HttpStatus::kOK;
}
void BookDetailService::Delete(
const std::vector<std::string>& url_sub_matches,
webcc::RestResponse* response) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
void BookDetailService::Delete(const webcc::UrlSubMatches& url_sub_matches,
webcc::RestResponse* response) {
Sleep(sleep_seconds_);
if (url_sub_matches.size() != 1) {
// TODO: kNotFound?
response->status = webcc::HttpStatus::kBadRequest;
response->status = webcc::HttpStatus::kNotFound;
return;
}
+13 -17
View File
@@ -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 {
public:
explicit BookListService(int sleep_seconds)
@@ -22,19 +16,17 @@ class BookListService : public webcc::RestListService {
protected:
// Get a list of books based on query parameters.
// URL examples:
// - /books
// - /books?name={BookName}
void Get(const webcc::UrlQuery& query,
webcc::RestResponse* response) final;
// Support query parameters.
void Get(const webcc::UrlQuery& query, webcc::RestResponse* response) final;
// Create a new book.
void Post(const std::string& request_content,
webcc::RestResponse* response) final;
private:
// Sleep for the client to test timeout control.
int sleep_seconds_;
// Sleep some seconds before send back the response.
// For testing timeout control in client side.
int sleep_seconds_;
};
// -----------------------------------------------------------------------------
@@ -48,19 +40,23 @@ class BookDetailService : public webcc::RestDetailService {
}
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,
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,
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;
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_;
};