Add SoapAsyncClient.
parent
6809ab7d2a
commit
ab781a691f
@ -0,0 +1,91 @@
|
|||||||
|
#include "webcc/soap_async_client.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <utility> // for move()
|
||||||
|
|
||||||
|
#include "webcc/http_async_client.h"
|
||||||
|
#include "webcc/soap_request.h"
|
||||||
|
#include "webcc/soap_response.h"
|
||||||
|
|
||||||
|
namespace webcc {
|
||||||
|
|
||||||
|
SoapAsyncClient::SoapAsyncClient(boost::asio::io_context& io_context,
|
||||||
|
const std::string& host,
|
||||||
|
const std::string& port)
|
||||||
|
: io_context_(io_context),
|
||||||
|
host_(host), port_(port),
|
||||||
|
soapenv_ns_(kSoapEnvNamespace),
|
||||||
|
format_raw_(true), timeout_seconds_(0) {
|
||||||
|
if (port_.empty()) {
|
||||||
|
std::size_t i = host_.find_last_of(':');
|
||||||
|
if (i != std::string::npos) {
|
||||||
|
port_ = host_.substr(i + 1);
|
||||||
|
host_ = host_.substr(0, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SoapAsyncClient::Request(const std::string& operation,
|
||||||
|
std::vector<SoapParameter>&& parameters,
|
||||||
|
SoapResponseHandler soap_response_handler) {
|
||||||
|
assert(service_ns_.IsValid());
|
||||||
|
assert(!url_.empty() && !host_.empty());
|
||||||
|
assert(!result_name_.empty());
|
||||||
|
|
||||||
|
SoapRequest soap_request;
|
||||||
|
|
||||||
|
soap_request.set_soapenv_ns(soapenv_ns_);
|
||||||
|
soap_request.set_service_ns(service_ns_);
|
||||||
|
|
||||||
|
soap_request.set_operation(operation);
|
||||||
|
|
||||||
|
for (SoapParameter& p : parameters) {
|
||||||
|
soap_request.AddParameter(std::move(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string http_content;
|
||||||
|
soap_request.ToXml(format_raw_, indent_str_, &http_content);
|
||||||
|
|
||||||
|
HttpRequestPtr http_request;
|
||||||
|
|
||||||
|
http_request->set_method(kHttpPost);
|
||||||
|
http_request->set_url(url_);
|
||||||
|
http_request->SetContent(std::move(http_content), true);
|
||||||
|
http_request->SetContentType(kTextXmlUtf8);
|
||||||
|
http_request->SetHost(host_, port_);
|
||||||
|
http_request->SetHeader(kSoapAction, operation);
|
||||||
|
http_request->UpdateStartLine();
|
||||||
|
|
||||||
|
HttpAsyncClientPtr http_client(new HttpAsyncClient(io_context_));
|
||||||
|
|
||||||
|
if (timeout_seconds_ > 0) {
|
||||||
|
http_client->set_timeout_seconds(timeout_seconds_);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto http_response_handler = std::bind(&SoapAsyncClient::ResponseHandler,
|
||||||
|
this, soap_response_handler,
|
||||||
|
std::placeholders::_1,
|
||||||
|
std::placeholders::_2,
|
||||||
|
std::placeholders::_3);
|
||||||
|
|
||||||
|
http_client->Request(http_request, http_response_handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SoapAsyncClient::ResponseHandler(SoapResponseHandler soap_response_handler,
|
||||||
|
HttpResponsePtr http_response,
|
||||||
|
Error error, bool timed_out) {
|
||||||
|
if (error != kNoError) {
|
||||||
|
soap_response_handler("", error, timed_out);
|
||||||
|
} else {
|
||||||
|
SoapResponse soap_response;
|
||||||
|
soap_response.set_result_name(result_name_);
|
||||||
|
|
||||||
|
if (!soap_response.FromXml(http_response->content())) {
|
||||||
|
soap_response_handler("", kXmlError, false);
|
||||||
|
} else {
|
||||||
|
soap_response_handler(soap_response.result_moved(), kNoError, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace webcc
|
@ -0,0 +1,85 @@
|
|||||||
|
#ifndef WEBCC_SOAP_ASYNC_CLIENT_H_
|
||||||
|
#define WEBCC_SOAP_ASYNC_CLIENT_H_
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "webcc/http_async_client.h"
|
||||||
|
#include "webcc/soap_message.h"
|
||||||
|
#include "webcc/soap_parameter.h"
|
||||||
|
|
||||||
|
namespace webcc {
|
||||||
|
|
||||||
|
// Response handler/callback.
|
||||||
|
typedef std::function<void(std::string, Error, bool)> SoapResponseHandler;
|
||||||
|
|
||||||
|
class SoapAsyncClient {
|
||||||
|
public:
|
||||||
|
// If |port| is empty, |host| will be checked to see if it contains port or
|
||||||
|
// not (separated by ':').
|
||||||
|
SoapAsyncClient(boost::asio::io_context& io_context, // NOLINT
|
||||||
|
const std::string& host, const std::string& port = "");
|
||||||
|
|
||||||
|
~SoapAsyncClient() = default;
|
||||||
|
|
||||||
|
DELETE_COPY_AND_ASSIGN(SoapAsyncClient);
|
||||||
|
|
||||||
|
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; }
|
||||||
|
|
||||||
|
void set_indent_str(const std::string& indent_str) {
|
||||||
|
indent_str_ = indent_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Request(const std::string& operation,
|
||||||
|
std::vector<SoapParameter>&& parameters,
|
||||||
|
SoapResponseHandler soap_response_handler);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void ResponseHandler(SoapResponseHandler soap_response_handler,
|
||||||
|
HttpResponsePtr http_response,
|
||||||
|
Error error, bool timed_out);
|
||||||
|
|
||||||
|
boost::asio::io_context& io_context_;
|
||||||
|
|
||||||
|
std::string host_;
|
||||||
|
std::string port_; // Leave this empty to use default 80.
|
||||||
|
|
||||||
|
// Request URL.
|
||||||
|
std::string url_;
|
||||||
|
|
||||||
|
SoapNamespace soapenv_ns_; // SOAP envelope namespace.
|
||||||
|
SoapNamespace service_ns_; // Namespace for your web service.
|
||||||
|
|
||||||
|
// Response result XML node name.
|
||||||
|
// E.g., "Result".
|
||||||
|
std::string result_name_;
|
||||||
|
|
||||||
|
// Format request XML without any indentation or line breaks.
|
||||||
|
bool format_raw_;
|
||||||
|
|
||||||
|
// Indent string for request XML.
|
||||||
|
// Applicable when |format_raw_| is false.
|
||||||
|
std::string indent_str_;
|
||||||
|
|
||||||
|
// Timeout in seconds; only effective when > 0.
|
||||||
|
int timeout_seconds_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace webcc
|
||||||
|
|
||||||
|
#endif // WEBCC_SOAP_ASYNC_CLIENT_H_
|
Loading…
Reference in New Issue