diff --git a/example/soap_book_client/book_client.cc b/example/soap_book_client/book_client.cc index 1af9fc1..c997c43 100644 --- a/example/soap_book_client/book_client.cc +++ b/example/soap_book_client/book_client.cc @@ -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 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&& 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&& 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 callback) { pugi::xml_document xdoc; diff --git a/example/soap_book_client/book_client.h b/example/soap_book_client/book_client.h index db6738c..203a83d 100644 --- a/example/soap_book_client/book_client.h +++ b/example/soap_book_client/book_client.h @@ -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&& parameters, - std::string* result_str); + // Simple wrapper of SoapClient::Request() to log error if any. + bool Call(const std::string& operation, + std::vector&& parameters, + std::string* result_str); + + void PrintError(); bool ParseResultXml(const std::string& result_xml, std::function callback); + webcc::SoapClient soap_client_; + // Last status. int code_; std::string message_; diff --git a/example/soap_calc_client/main.cc b/example/soap_calc_client/main.cc index 8270253..b60e288 100644 --- a/example/soap_calc_client/main.cc +++ b/example/soap_calc_client/main.cc @@ -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_; }; // ----------------------------------------------------------------------------- diff --git a/example/soap_calc_client_parasoft/main.cc b/example/soap_calc_client_parasoft/main.cc index f08b782..c0c086b 100644 --- a/example/soap_calc_client_parasoft/main.cc +++ b/example/soap_calc_client_parasoft/main.cc @@ -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_; }; // ----------------------------------------------------------------------------- diff --git a/webcc/rest_client.cc b/webcc/rest_client.cc index 511fe95..e283b18 100644 --- a/webcc/rest_client.cc +++ b/webcc/rest_client.cc @@ -6,10 +6,8 @@ namespace webcc { RestClient::RestClient(const std::string& host, const std::string& port) - : host_(host), - port_(port), - timeout_seconds_(0), - timed_out_(false), + : host_(host), port_(port), + timeout_seconds_(0), timed_out_(false), error_(kNoError) { } @@ -20,18 +18,18 @@ bool RestClient::Request(const std::string& method, const std::string& url, error_ = kNoError; timed_out_ = false; - HttpRequest request; + HttpRequest http_request; - request.set_method(method); - request.set_url(url); - request.SetHost(host_, port_); + http_request.set_method(method); + http_request.set_url(url); + http_request.SetHost(host_, port_); if (!content.empty()) { - request.SetContent(std::move(content), true); - request.SetContentType(kAppJsonUtf8); + http_request.SetContent(std::move(content), true); + http_request.SetContentType(kAppJsonUtf8); } - request.UpdateStartLine(); + http_request.UpdateStartLine(); HttpClient http_client; @@ -39,7 +37,7 @@ bool RestClient::Request(const std::string& method, const std::string& url, http_client.set_timeout_seconds(timeout_seconds_); } - if (!http_client.Request(request)) { + if (!http_client.Request(http_request)) { error_ = http_client.error(); timed_out_ = http_client.timed_out(); return false; diff --git a/webcc/soap_client.cc b/webcc/soap_client.cc index 881ffd1..5a11441 100644 --- a/webcc/soap_client.cc +++ b/webcc/soap_client.cc @@ -13,20 +13,18 @@ namespace webcc { SoapClient::SoapClient(const std::string& host, const std::string& port) : host_(host), port_(port), - format_raw_(true), timeout_seconds_(0), timed_out_(false) { + soapenv_ns_(kSoapEnvNamespace), + format_raw_(true), timeout_seconds_(0), timed_out_(false), + error_(kNoError) { } -Error SoapClient::Call(const std::string& operation, - std::vector&& parameters, - std::string* result) { +bool SoapClient::Request(const std::string& operation, + std::vector&& parameters, + std::string* result) { assert(service_ns_.IsValid()); assert(!url_.empty() && !host_.empty()); assert(!result_name_.empty()); - if (!soapenv_ns_.IsValid()) { - soapenv_ns_ = kSoapEnvNamespace; - } - SoapRequest soap_request; soap_request.set_soapenv_ns(soapenv_ns_); @@ -58,20 +56,22 @@ Error SoapClient::Call(const std::string& operation, } if (!http_client.Request(http_request)) { + error_ = http_client.error(); timed_out_ = http_client.timed_out(); - return http_client.error(); + return false; } SoapResponse soap_response; soap_response.set_result_name(result_name_); if (!soap_response.FromXml(http_client.response()->content())) { - return kXmlError; + error_ = kXmlError; + return false; } *result = soap_response.result_moved(); - return kNoError; + return true; } } // namespace webcc diff --git a/webcc/soap_client.h b/webcc/soap_client.h index 72ded96..2f1526d 100644 --- a/webcc/soap_client.h +++ b/webcc/soap_client.h @@ -9,13 +9,27 @@ namespace webcc { -// Base class for your SOAP client. -// Set URL, host, port, etc. in your sub-class before make the call. class SoapClient { public: - virtual ~SoapClient() = default; + SoapClient(const std::string& host, const std::string& port); - bool timed_out() const { return timed_out_; } + ~SoapClient() = default; + + DELETE_COPY_AND_ASSIGN(SoapClient); + + void set_timeout_seconds(int timeout_seconds) { + timeout_seconds_ = timeout_seconds; + } + + void set_url(const std::string& url) { url_ = url; } + + void set_service_ns(const SoapNamespace& service_ns) { + service_ns_ = service_ns; + } + + void set_result_name(const std::string& result_name) { + result_name_ = result_name; + } void set_format_raw(bool format_raw) { format_raw_ = format_raw; } @@ -23,23 +37,23 @@ class SoapClient { indent_str_ = indent_str; } - protected: - SoapClient(const std::string& host, const std::string& port); + bool timed_out() const { return timed_out_; } - // A generic wrapper to make a call. - // NOTE: The parameters should be movable. - Error Call(const std::string& operation, - std::vector&& parameters, - std::string* result); + Error error() const { return error_; } - SoapNamespace soapenv_ns_; // SOAP envelope namespace. - SoapNamespace service_ns_; // Namespace for your web service. + bool Request(const std::string& operation, + std::vector&& parameters, + std::string* result); + + private: + std::string host_; + std::string port_; // Leave this empty to use default 80. // Request URL. std::string url_; - std::string host_; - std::string port_; // Leave this empty to use default 80. + SoapNamespace soapenv_ns_; // SOAP envelope namespace. + SoapNamespace service_ns_; // Namespace for your web service. // Response result XML node name. // E.g., "Result". @@ -57,6 +71,8 @@ class SoapClient { // If the error was caused by timeout or not. bool timed_out_; + + Error error_; }; } // namespace webcc