Rename ErrorCode to Error; refine class SoapClient.

This commit is contained in:
Adam Gu
2018-01-12 14:27:42 +08:00
parent 091f06e3a6
commit dc030207df
16 changed files with 98 additions and 53 deletions
+2 -2
View File
@@ -18,8 +18,8 @@ const std::string kTextXmlUtf8 = "text/xml; charset=utf-8";
////////////////////////////////////////////////////////////////////////////////
const char* GetErrorMessage(ErrorCode error_code) {
switch (error_code) {
const char* GetErrorMessage(Error error) {
switch (error) {
case kHostResolveError:
return "Cannot resolve the host.";
+3 -2
View File
@@ -35,7 +35,8 @@ const std::size_t kInvalidLength = std::string::npos;
////////////////////////////////////////////////////////////////////////////////
enum ErrorCode {
// Error codes.
enum Error {
kNoError = 0, // OK
kHostResolveError,
@@ -59,7 +60,7 @@ enum ErrorCode {
};
// Return a descriptive message for the given error code.
const char* GetErrorMessage(ErrorCode error_code);
const char* GetErrorMessage(Error error);
////////////////////////////////////////////////////////////////////////////////
+1 -1
View File
@@ -50,7 +50,7 @@ void Connection::HandleRead(boost::system::error_code ec,
return;
}
ErrorCode error = request_parser_.Parse(buffer_.data(), bytes_transferred);
Error error = request_parser_.Parse(buffer_.data(), bytes_transferred);
if (error != kNoError) {
// Bad request.
+4 -3
View File
@@ -56,8 +56,8 @@ HttpClient::HttpClient()
: timeout_seconds_(15) {
}
ErrorCode HttpClient::SendRequest(const HttpRequest& request,
HttpResponse* response) {
Error HttpClient::SendRequest(const HttpRequest& request,
HttpResponse* response) {
assert(response != NULL);
using boost::asio::ip::tcp;
@@ -71,6 +71,7 @@ ErrorCode HttpClient::SendRequest(const HttpRequest& request,
port = "80";
}
// TODO: IPv4 or both IPv4 and IPv6
boost::system::error_code ec;
tcp::resolver::results_type endpoints =
resolver.resolve(/*tcp::v4(), */request.host(), port, ec);
@@ -130,7 +131,7 @@ ErrorCode HttpClient::SendRequest(const HttpRequest& request,
// Parse the response piece just read.
// If the content has been fully received, next time flag "finished_"
// will be set.
ErrorCode error = parser.Parse(buffer_.data(), length);
Error error = parser.Parse(buffer_.data(), length);
if (error != kNoError) {
return error;
+2 -2
View File
@@ -25,8 +25,8 @@ public:
}
// Send an HTTP request, wait until the response is received.
ErrorCode SendRequest(const HttpRequest& request,
HttpResponse* response);
Error SendRequest(const HttpRequest& request,
HttpResponse* response);
private:
boost::asio::io_context io_context_;
+2 -2
View File
@@ -18,7 +18,7 @@ void HttpParser::Reset() {
// TODO: Reset parsing state.
}
ErrorCode HttpParser::Parse(const char* data, size_t len) {
Error HttpParser::Parse(const char* data, size_t len) {
if (header_parsed_) {
// Add the data to the content.
message_->AppendContent(data, len);
@@ -50,7 +50,7 @@ ErrorCode HttpParser::Parse(const char* data, size_t len) {
if (!start_line_parsed_) {
start_line_parsed_ = true;
ErrorCode error = ParseStartLine(line);
Error error = ParseStartLine(line);
if (error != kNoError) {
return error;
}
+3 -3
View File
@@ -21,11 +21,11 @@ public:
// Reset parsing state.
void Reset();
ErrorCode Parse(const char* data, size_t len);
Error Parse(const char* data, size_t len);
protected:
// Parse HTTP start line.
virtual ErrorCode ParseStartLine(const std::string& line) = 0;
virtual Error ParseStartLine(const std::string& line) = 0;
void ParseContentLength(const std::string& line);
@@ -33,7 +33,7 @@ protected:
// The result HTTP message.
HttpMessage* message_;
ErrorCode error_;
Error error_;
// Data waiting to be parsed.
std::string pending_data_;
+1 -1
View File
@@ -12,7 +12,7 @@ HttpRequestParser::HttpRequestParser(HttpRequest* request)
, request_(request) {
}
ErrorCode HttpRequestParser::ParseStartLine(const std::string& line) {
Error HttpRequestParser::ParseStartLine(const std::string& line) {
// Example: POST / HTTP/1.1
std::vector<std::string> strs;
+1 -1
View File
@@ -12,7 +12,7 @@ public:
explicit HttpRequestParser(HttpRequest* request);
private:
ErrorCode ParseStartLine(const std::string& line) override;
Error ParseStartLine(const std::string& line) override;
private:
HttpRequest* request_;
+1 -1
View File
@@ -10,7 +10,7 @@ HttpResponseParser::HttpResponseParser(HttpResponse* response)
}
// TODO: Use split.
ErrorCode HttpResponseParser::ParseStartLine(const std::string& line) {
Error HttpResponseParser::ParseStartLine(const std::string& line) {
size_t off = 0;
size_t pos = line.find(' ');
+1 -1
View File
@@ -13,7 +13,7 @@ public:
private:
// Parse HTTP start line; E.g., "HTTP/1.1 200 OK".
ErrorCode ParseStartLine(const std::string& line) override;
Error ParseStartLine(const std::string& line) override;
private:
// The result response message.
+20 -26
View File
@@ -13,63 +13,57 @@
namespace csoap {
bool SoapClient::Call(const std::string& operation,
const csoap::Parameter* parameters,
size_t count,
std::string* result) {
Error SoapClient::Call(const std::string& operation,
const Parameter* parameters,
std::size_t count,
std::string* result) {
assert(!url_.empty() &&
!host_.empty() &&
!result_name_.empty() &&
service_ns_.IsValid());
csoap::SoapRequest soap_request;
SoapRequest soap_request;
soap_request.set_soapenv_ns(kSoapEnvNamespace);
soap_request.set_soapenv_ns(kSoapEnvNamespace); // TODO: Configurable
soap_request.set_service_ns(service_ns_);
soap_request.set_operation(operation);
for (size_t i = 0; i < count; ++i) {
for (std::size_t i = 0; i < count; ++i) {
soap_request.AddParameter(parameters[i]);
}
std::string http_request_body;
soap_request.ToXml(&http_request_body);
csoap::HttpRequest http_request;
HttpRequest http_request;
http_request.set_version(csoap::kHttpV11);
http_request.set_version(kHttpV11); // TODO
http_request.set_url(url_);
http_request.set_content_length(http_request_body.size());
http_request.set_content(http_request_body); // TODO: move
http_request.set_host(host_, port_);
http_request.set_soap_action(operation);
csoap::HttpResponse http_response;
HttpResponse http_response;
csoap::HttpClient http_client;
csoap::ErrorCode ec = http_client.SendRequest(http_request, &http_response);
if (ec != csoap::kNoError) {
std::cerr << csoap::GetErrorMessage(ec) << std::endl;
HttpClient http_client;
Error error = http_client.SendRequest(http_request, &http_response);
if (ec == csoap::kHttpStatusError) {
//std::cerr << "\t"
// << http_response.status() << ", "
// << http_response.reason() << std::endl;
}
return false;
if (error != kNoError) {
return error;
}
csoap::SoapResponse soap_response;
SoapResponse soap_response;
soap_response.set_result_name(result_name_);
if (soap_response.FromXml(http_response.content())) {
*result = soap_response.result();
return true;
if (!soap_response.FromXml(http_response.content())) {
return kXmlError; // TODO: Some SOAP error?
}
return false;
*result = soap_response.result();
return kNoError;
}
} // namespace csoap
+4 -4
View File
@@ -19,10 +19,10 @@ protected:
}
// A generic wrapper to make a call.
bool Call(const std::string& operation,
const csoap::Parameter* parameters,
size_t count,
std::string* result);
Error Call(const std::string& operation,
const Parameter* parameters,
std::size_t count,
std::string* result);
protected:
// Request URL.