Replace boost thread with std thread; update rest book example; move soap tutorials to wiki.

This commit is contained in:
Adam Gu
2018-07-23 15:40:58 +08:00
parent 6475a3fd35
commit 33f2b84f8c
11 changed files with 64 additions and 446 deletions
+32 -20
View File
@@ -20,12 +20,24 @@
// -----------------------------------------------------------------------------
// Write a JSON object to string.
std::string JsonToString(const Json::Value& json) {
static std::string JsonToString(const Json::Value& json) {
Json::StreamWriterBuilder builder;
return Json::writeString(builder, json);
}
static Json::Value StringToJson(const std::string& str) {
Json::Value json;
Json::CharReaderBuilder builder;
std::stringstream stream(str);
std::string errs;
if (!Json::parseFromStream(builder, stream, &json, &errs)) {
std::cerr << errs << std::endl;
}
return json;
}
// -----------------------------------------------------------------------------
class BookClientBase {
@@ -78,26 +90,25 @@ public:
return true;
}
bool CreateBook(const std::string& id,
const std::string& title,
double price) {
bool CreateBook(const std::string& title, double price, std::string* id) {
PrintSeparateLine();
std::cout << "CreateBook: " << id << ", " << title << ", " << price
<< std::endl;
std::cout << "CreateBook: " << title << ", " << price << std::endl;
Json::Value json(Json::objectValue);
json["id"] = id;
json["title"] = title;
json["price"] = price;
Json::Value req_json(Json::objectValue);
req_json["title"] = title;
req_json["price"] = price;
if (!rest_client_.Post("/books", JsonToString(json))) {
if (!rest_client_.Post("/books", JsonToString(req_json))) {
PrintError();
return false;
}
std::cout << rest_client_.response_status() << std::endl;
return true;
Json::Value rsp_json = StringToJson(rest_client_.response_content());
*id = rsp_json["id"].asString();
return !id->empty();
}
};
@@ -123,8 +134,7 @@ public:
return true;
}
bool UpdateBook(const std::string& id,
const std::string& title,
bool UpdateBook(const std::string& id, const std::string& title,
double price) {
PrintSeparateLine();
std::cout << "UpdateBook: " << id << ", " << title << ", " << price
@@ -187,12 +197,14 @@ int main(int argc, char* argv[]) {
BookDetailClient detail_client(host, port, timeout_seconds);
list_client.ListBooks();
list_client.CreateBook("1", "1984", 12.3);
detail_client.GetBook("1");
detail_client.UpdateBook("1", "1Q84", 32.1);
detail_client.GetBook("1");
detail_client.DeleteBook("1");
std::string id;
list_client.CreateBook("1984", 12.3, &id);
detail_client.GetBook(id);
detail_client.UpdateBook(id, "1Q84", 32.1);
detail_client.GetBook(id);
detail_client.DeleteBook(id);
list_client.ListBooks();
+16 -10
View File
@@ -2,8 +2,8 @@
#include <iostream>
#include <list>
#include <thread>
#include "boost/thread/thread.hpp"
#include "json/json.h"
#include "webcc/logger.h"
@@ -42,11 +42,11 @@ static bool JsonToBook(const std::string& json, Book* book) {
// Return all books as a JSON array.
// TODO: Support query parameters.
bool BookListService::Get(const webcc::UrlQuery& /* query */,
bool BookListService::Get(const webcc::UrlQuery& /*query*/,
std::string* response_content) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
Json::Value root(Json::arrayValue);
@@ -61,17 +61,23 @@ bool BookListService::Get(const webcc::UrlQuery& /* query */,
}
// Add a new book.
// No response content.
bool BookListService::Post(const std::string& request_content,
std::string* /* response_content */) {
std::string* response_content) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
Book book;
if (JsonToBook(request_content, &book)) {
g_book_store.AddBook(book); // TODO: return ID
std::string id = g_book_store.AddBook(book);
Json::Value root;
root["id"] = id;
Json::StreamWriterBuilder builder;
*response_content = Json::writeString(builder, root);
return true;
}
@@ -85,7 +91,7 @@ bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
std::string* response_content) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
if (url_sub_matches.size() != 1) {
@@ -110,7 +116,7 @@ bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
std::string* response_content) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
if (url_sub_matches.size() != 1) {
@@ -132,7 +138,7 @@ bool BookDetailService::Delete(
const std::vector<std::string>& url_sub_matches) {
if (sleep_seconds_ > 0) {
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
}
if (url_sub_matches.size() != 1) {
+1 -2
View File
@@ -79,8 +79,7 @@ class CalcClient {
int main() {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
// Default port 80.
CalcClient calc("ws1.parasoft.com", "");
CalcClient calc("ws1.parasoft.com", ""); // Use default port 80
double x = 1.0;
double y = 2.0;
+5 -14
View File
@@ -11,7 +11,11 @@ bool CalcService::Handle(const webcc::SoapRequest& soap_request,
webcc::SoapResponse* soap_response) {
double x = 0.0;
double y = 0.0;
if (!GetParameters(soap_request, &x, &y)) {
try {
x = std::stod(soap_request.GetParameter("x"));
y = std::stod(soap_request.GetParameter("y"));
} catch (const std::exception& e) {
LOG_ERRO("SoapParameter cast error: %s", e.what());
return false;
}
@@ -61,16 +65,3 @@ bool CalcService::Handle(const webcc::SoapRequest& soap_request,
return true;
}
bool CalcService::GetParameters(const webcc::SoapRequest& soap_request,
double* x, double* y) {
try {
*x = std::stod(soap_request.GetParameter("x"));
*y = std::stod(soap_request.GetParameter("y"));
} catch (const std::exception& e) {
LOG_ERRO("SoapParameter cast error: %s", e.what());
return false;
}
return true;
}
-4
View File
@@ -10,10 +10,6 @@ class CalcService : public webcc::SoapService {
bool Handle(const webcc::SoapRequest& soap_request,
webcc::SoapResponse* soap_response) override;
private:
bool GetParameters(const webcc::SoapRequest& soap_request,
double* x, double* y);
};
#endif // CALC_SERVICE_H_