require "httpi"
require "savon/soap/response"
module Savon
module SOAP
class Request
CONTENT_TYPE = { 1 => "text/xml;charset=UTF-8", 2 => "application/soap+xml;charset=UTF-8" }
def self.execute(config, http, soap)
new(config, http, soap).response
end
def initialize(config, http, soap)
self.config = config
self.soap = soap
self.http = configure(http)
end
attr_accessor :soap, :http, :config
def response
@response ||= begin
response = config.hooks.fire(:soap_request, self) { with_logging { HTTPI.post(http) } }
SOAP::Response.new(config, response)
end
end
private
def configure(http)
http.url = soap.endpoint
if soap.signature?
soap.wsse.signature.document = soap.to_xml(true)
soap.wsse.signature.document = soap.to_xml(true)
http.body = soap.to_xml(true)
else
http.body = soap.to_xml
end
http.["Content-Type"] = CONTENT_TYPE[soap.version]
http.["Content-Length"] = soap.to_xml.bytesize.to_s
http
end
def with_logging
log_request http.url, http., http.body
response = yield
log_response response.code, response.body
response
end
def log_request(url, , body)
config.logger.log "SOAP request: #{url}"
config.logger.log .map { |key, value| "#{key}: #{value}" }.join(", ")
config.logger.log body, :pretty => config.pretty_print_xml, :filter => true
end
def log_response(code, body)
config.logger.log "SOAP response (status #{code}):"
config.logger.log body, :pretty => config.pretty_print_xml
end
end
end
end