Use composite instead of inheritance for SoapClient.
This commit is contained in:
@@ -13,14 +13,14 @@ static void PrintSeparateLine() {
|
||||
}
|
||||
|
||||
BookClient::BookClient(const std::string& host, const std::string& port)
|
||||
: webcc::SoapClient(host, port), code_(0) {
|
||||
url_ = "/book";
|
||||
service_ns_ = { "ser", "http://www.example.com/book/" };
|
||||
result_name_ = "Result";
|
||||
: soap_client_(host, port), code_(0) {
|
||||
soap_client_.set_url("/book");
|
||||
soap_client_.set_service_ns({ "ser", "http://www.example.com/book/" });
|
||||
soap_client_.set_result_name("Result");
|
||||
|
||||
// Customize response XML format.
|
||||
format_raw_ = false;
|
||||
indent_str_ = " ";
|
||||
soap_client_.set_format_raw(false);
|
||||
soap_client_.set_indent_str(" ");
|
||||
}
|
||||
|
||||
bool BookClient::CreateBook(const std::string& title, double price, std::string* id) {
|
||||
@@ -87,7 +87,7 @@ bool BookClient::DeleteBook(const std::string& id) {
|
||||
}
|
||||
|
||||
bool BookClient::Call0(const std::string& operation, std::string* result_str) {
|
||||
return CallX(operation, {}, result_str);
|
||||
return Call(operation, {}, result_str);
|
||||
}
|
||||
|
||||
|
||||
@@ -96,25 +96,27 @@ bool BookClient::Call1(const std::string& operation, webcc::SoapParameter&& para
|
||||
std::vector<webcc::SoapParameter> parameters{
|
||||
{ std::move(parameter) }
|
||||
};
|
||||
return CallX(operation, std::move(parameters), result_str);
|
||||
return Call(operation, std::move(parameters), result_str);
|
||||
}
|
||||
|
||||
bool BookClient::CallX(const std::string& operation,
|
||||
std::vector<webcc::SoapParameter>&& parameters,
|
||||
std::string* result_str) {
|
||||
webcc::Error error = webcc::SoapClient::Call(operation,
|
||||
std::move(parameters),
|
||||
result_str);
|
||||
|
||||
if (error != webcc::kNoError) {
|
||||
LOG_ERRO("Operation '%s' failed: %s",
|
||||
operation, webcc::DescribeError(error));
|
||||
bool BookClient::Call(const std::string& operation,
|
||||
std::vector<webcc::SoapParameter>&& parameters,
|
||||
std::string* result_str) {
|
||||
if (!soap_client_.Request(operation, std::move(parameters), result_str)) {
|
||||
PrintError();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BookClient::PrintError() {
|
||||
std::cout << webcc::DescribeError(soap_client_.error());
|
||||
if (soap_client_.timed_out()) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
bool BookClient::ParseResultXml(const std::string& result_xml,
|
||||
std::function<bool(pugi::xml_node)> callback) {
|
||||
pugi::xml_document xdoc;
|
||||
|
||||
@@ -8,12 +8,10 @@
|
||||
|
||||
#include "example/common/book.h"
|
||||
|
||||
class BookClient : public webcc::SoapClient {
|
||||
class BookClient {
|
||||
public:
|
||||
BookClient(const std::string& host, const std::string& port);
|
||||
|
||||
~BookClient() override = default;
|
||||
|
||||
int code() const { return code_; }
|
||||
const std::string& message() const { return message_; }
|
||||
|
||||
@@ -37,14 +35,18 @@ class BookClient : public webcc::SoapClient {
|
||||
bool Call1(const std::string& operation, webcc::SoapParameter&& parameter,
|
||||
std::string* result_str);
|
||||
|
||||
// Simple wrapper of webcc::SoapClient::Call() to log error if any.
|
||||
bool CallX(const std::string& operation,
|
||||
std::vector<webcc::SoapParameter>&& parameters,
|
||||
std::string* result_str);
|
||||
// Simple wrapper of SoapClient::Request() to log error if any.
|
||||
bool Call(const std::string& operation,
|
||||
std::vector<webcc::SoapParameter>&& parameters,
|
||||
std::string* result_str);
|
||||
|
||||
void PrintError();
|
||||
|
||||
bool ParseResultXml(const std::string& result_xml,
|
||||
std::function<bool(pugi::xml_node)> callback);
|
||||
|
||||
webcc::SoapClient soap_client_;
|
||||
|
||||
// Last status.
|
||||
int code_;
|
||||
std::string message_;
|
||||
|
||||
@@ -5,19 +5,21 @@
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class CalcClient : public webcc::SoapClient {
|
||||
class CalcClient {
|
||||
public:
|
||||
CalcClient(const std::string& host, const std::string& port)
|
||||
: webcc::SoapClient(host, port) {
|
||||
timeout_seconds_ = 5; // Override the default timeout.
|
||||
: soap_client_(host, port) {
|
||||
soap_client_.set_timeout_seconds(5);
|
||||
|
||||
url_ = "/calculator";
|
||||
service_ns_ = { "ser", "http://www.example.com/calculator/" };
|
||||
result_name_ = "Result";
|
||||
soap_client_.set_url("/calculator");
|
||||
soap_client_.set_service_ns({
|
||||
"ser", "http://www.example.com/calculator/"
|
||||
});
|
||||
soap_client_.set_result_name("Result");
|
||||
|
||||
// Customize request XML format.
|
||||
format_raw_ = false;
|
||||
indent_str_ = " ";
|
||||
soap_client_.set_format_raw(false);
|
||||
soap_client_.set_indent_str(" ");
|
||||
}
|
||||
|
||||
bool Add(double x, double y, double* result) {
|
||||
@@ -41,7 +43,7 @@ class CalcClient : public webcc::SoapClient {
|
||||
return Calc("unknown", "x", "y", x, y, result);
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
bool Calc(const std::string& operation,
|
||||
const std::string& x_name, const std::string& y_name,
|
||||
double x, double y,
|
||||
@@ -52,11 +54,8 @@ class CalcClient : public webcc::SoapClient {
|
||||
};
|
||||
|
||||
std::string result_str;
|
||||
webcc::Error error = Call(operation, std::move(parameters), &result_str);
|
||||
|
||||
if (error != webcc::kNoError) {
|
||||
LOG_ERRO("Operation '%s' failed: %s", operation.c_str(),
|
||||
webcc::DescribeError(error));
|
||||
if (!soap_client_.Request(operation, std::move(parameters), &result_str)) {
|
||||
PrintError();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -68,6 +67,16 @@ class CalcClient : public webcc::SoapClient {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void PrintError() {
|
||||
std::cout << webcc::DescribeError(soap_client_.error());
|
||||
if (soap_client_.timed_out()) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
webcc::SoapClient soap_client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -5,19 +5,21 @@
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class CalcClient : public webcc::SoapClient {
|
||||
class CalcClient {
|
||||
public:
|
||||
CalcClient(const std::string& host, const std::string& port)
|
||||
: webcc::SoapClient(host, port) {
|
||||
timeout_seconds_ = 5; // Override the default timeout.
|
||||
: soap_client_(host, port) {
|
||||
soap_client_.set_timeout_seconds(5);
|
||||
|
||||
url_ = "/glue/calculator";
|
||||
service_ns_ = { "cal", "http://www.parasoft.com/wsdl/calculator/" };
|
||||
result_name_ = "Result";
|
||||
soap_client_.set_url("/glue/calculator");
|
||||
soap_client_.set_service_ns({
|
||||
"cal", "http://www.parasoft.com/wsdl/calculator/"
|
||||
});
|
||||
soap_client_.set_result_name("Result");
|
||||
|
||||
// Customize request XML format.
|
||||
format_raw_ = false;
|
||||
indent_str_ = " ";
|
||||
soap_client_.set_format_raw(false);
|
||||
soap_client_.set_indent_str(" ");
|
||||
}
|
||||
|
||||
bool Add(double x, double y, double* result) {
|
||||
@@ -36,7 +38,7 @@ class CalcClient : public webcc::SoapClient {
|
||||
return Calc("divide", "numerator", "denominator", x, y, result);
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
bool Calc(const std::string& operation,
|
||||
const std::string& x_name, const std::string& y_name,
|
||||
double x, double y,
|
||||
@@ -47,11 +49,8 @@ class CalcClient : public webcc::SoapClient {
|
||||
};
|
||||
|
||||
std::string result_str;
|
||||
webcc::Error error = Call(operation, std::move(parameters), &result_str);
|
||||
|
||||
if (error != webcc::kNoError) {
|
||||
LOG_ERRO("Operation '%s' failed: %s", operation.c_str(),
|
||||
webcc::DescribeError(error));
|
||||
if (!soap_client_.Request(operation, std::move(parameters), &result_str)) {
|
||||
PrintError();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -63,6 +62,16 @@ class CalcClient : public webcc::SoapClient {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void PrintError() {
|
||||
std::cout << webcc::DescribeError(soap_client_.error());
|
||||
if (soap_client_.timed_out()) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
webcc::SoapClient soap_client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user