Use composite instead of inheritance for SoapClient.
This commit is contained in:
+10
-12
@@ -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;
|
||||
|
||||
+11
-11
@@ -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<SoapParameter>&& parameters,
|
||||
std::string* result) {
|
||||
bool SoapClient::Request(const std::string& operation,
|
||||
std::vector<SoapParameter>&& 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
|
||||
|
||||
+31
-15
@@ -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<SoapParameter>&& 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<SoapParameter>&& 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
|
||||
|
||||
Reference in New Issue
Block a user