Refactor RestService interfaces.

This commit is contained in:
Adam Gu
2018-08-24 16:51:08 +08:00
parent e9096d4e53
commit a863b379d0
10 changed files with 227 additions and 265 deletions
+45 -31
View File
@@ -18,8 +18,8 @@ static BookStore g_book_store;
// Return all books as a JSON array.
// TODO: Support query parameters.
bool BookListService::Get(const webcc::UrlQuery& /*query*/,
std::string* response_content) {
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_));
@@ -30,14 +30,12 @@ bool BookListService::Get(const webcc::UrlQuery& /*query*/,
json.append(BookToJson(book));
}
*response_content = JsonToString(json);
return true;
response->content = JsonToString(json);
response->status = webcc::HttpStatus::kOK;
}
// Add a new book.
bool BookListService::Post(const std::string& request_content,
std::string* response_content) {
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_));
@@ -50,75 +48,91 @@ bool BookListService::Post(const std::string& request_content,
Json::Value json;
json["id"] = id;
*response_content = JsonToString(json);
return true;
response->content = JsonToString(json);
response->status = webcc::HttpStatus::kCreated;
} else {
// Invalid JSON
response->status = webcc::HttpStatus::kBadRequest;
}
return false;
}
// -----------------------------------------------------------------------------
bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
void BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
const webcc::UrlQuery& query,
std::string* response_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_));
}
if (url_sub_matches.size() != 1) {
return false;
// TODO: kNotFound?
response->status = webcc::HttpStatus::kBadRequest;
return;
}
const std::string& book_id = url_sub_matches[0];
const Book& book = g_book_store.GetBook(book_id);
if (!book.IsNull()) {
*response_content = BookToJsonString(book);
return true;
if (book.IsNull()) {
response->status = webcc::HttpStatus::kNotFound;
return;
}
return false;
response->content = BookToJsonString(book);
response->status = webcc::HttpStatus::kOK;
}
// Update a book.
bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
void BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
const std::string& request_content,
std::string* response_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_));
}
if (url_sub_matches.size() != 1) {
return false;
// TODO: kNotFound?
response->status = webcc::HttpStatus::kBadRequest;
return;
}
const std::string& book_id = url_sub_matches[0];
Book book;
if (JsonStringToBook(request_content, &book)) {
book.id = book_id;
return g_book_store.UpdateBook(book);
if (!JsonStringToBook(request_content, &book)) {
response->status = webcc::HttpStatus::kBadRequest;
return;
}
return false;
book.id = book_id;
g_book_store.UpdateBook(book);
response->status = webcc::HttpStatus::kOK;
}
bool BookDetailService::Delete(
const std::vector<std::string>& url_sub_matches) {
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_));
}
if (url_sub_matches.size() != 1) {
return false;
// TODO: kNotFound?
response->status = webcc::HttpStatus::kBadRequest;
return;
}
const std::string& book_id = url_sub_matches[0];
return g_book_store.DeleteBook(book_id);
if (!g_book_store.DeleteBook(book_id)) {
response->status = webcc::HttpStatus::kNotFound;
return;
}
response->status = webcc::HttpStatus::kOK;
}
+15 -13
View File
@@ -16,24 +16,25 @@
// The query parameters could be regular expressions.
class BookListService : public webcc::RestListService {
public:
explicit BookListService(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
explicit BookListService(int sleep_seconds)
: sleep_seconds_(sleep_seconds) {
}
protected:
// Return a list of books based on query parameters.
// Get a list of books based on query parameters.
// URL examples:
// - /books
// - /books?name={BookName}
bool Get(const webcc::UrlQuery& query,
std::string* response_content) override;
void Get(const webcc::UrlQuery& query,
webcc::RestResponse* response) final;
// Create a new book.
bool Post(const std::string& request_content,
std::string* response_content) override;
void Post(const std::string& request_content,
webcc::RestResponse* response) final;
private:
// Sleep for the client to test timeout control.
int sleep_seconds_ = 0;
int sleep_seconds_;
};
// -----------------------------------------------------------------------------
@@ -47,19 +48,20 @@ class BookDetailService : public webcc::RestDetailService {
}
protected:
bool Get(const std::vector<std::string>& url_sub_matches,
void Get(const std::vector<std::string>& url_sub_matches,
const webcc::UrlQuery& query,
std::string* response_content) override;
webcc::RestResponse* response) final;
bool Put(const std::vector<std::string>& url_sub_matches,
void Put(const std::vector<std::string>& url_sub_matches,
const std::string& request_content,
std::string* response_content) override;
webcc::RestResponse* response) final;
bool Delete(const std::vector<std::string>& url_sub_matches) override;
void Delete(const std::vector<std::string>& url_sub_matches,
webcc::RestResponse* response) final;
private:
// Sleep for the client to test timeout control.
int sleep_seconds_ = 0;
int sleep_seconds_;
};
#endif // EXAMPLE_REST_BOOK_SERVER_SERVICES_H_