lib/savon/request.rb in savon-2.0.3 vs lib/savon/request.rb in savon-2.1.0
- old
+ new
@@ -1,115 +1,88 @@
require "httpi"
-require "savon/response"
-require "savon/log_message"
module Savon
- class Request
+ class HTTPRequest
- CONTENT_TYPE = {
- 1 => "text/xml;charset=%s",
- 2 => "application/soap+xml;charset=%s"
- }
-
- def initialize(operation_name, wsdl, globals, locals)
- @operation_name = operation_name
-
- @wsdl = wsdl
+ def initialize(globals, http_request = nil)
@globals = globals
- @locals = locals
- @http = create_http_client
+ @http_request = http_request || HTTPI::Request.new
end
- attr_reader :http
+ def build
+ @http_request
+ end
- def call(xml)
- @http.body = xml
- @http.headers["Content-Length"] = xml.bytesize.to_s
+ private
- log_request @http.url, @http.headers, @http.body
- response = HTTPI.post(@http)
- log_response response.code, response.body
+ def configure_proxy
+ @http_request.proxy = @globals[:proxy] if @globals.include? :proxy
+ end
- response
+ def configure_timeouts
+ @http_request.open_timeout = @globals[:open_timeout] if @globals.include? :open_timeout
+ @http_request.read_timeout = @globals[:read_timeout] if @globals.include? :read_timeout
end
- private
+ def configure_ssl
+ @http_request.auth.ssl.ssl_version = @globals[:ssl_version] if @globals.include? :ssl_version
+ @http_request.auth.ssl.verify_mode = @globals[:ssl_verify_mode] if @globals.include? :ssl_verify_mode
- def create_http_client
- http = HTTPI::Request.new
+ @http_request.auth.ssl.cert_key_file = @globals[:ssl_cert_key_file] if @globals.include? :ssl_cert_key_file
+ @http_request.auth.ssl.cert_file = @globals[:ssl_cert_file] if @globals.include? :ssl_cert_file
+ @http_request.auth.ssl.ca_cert_file = @globals[:ssl_ca_cert_file] if @globals.include? :ssl_ca_cert_file
- configure_request(http)
- configure_timeouts(http)
- configure_headers(http)
- configure_ssl(http)
- configure_auth(http)
-
- http
+ @http_request.auth.ssl.cert_key_password = @globals[:ssl_cert_key_password] if @globals.include? :ssl_cert_key_password
end
- def configure_request(http)
- http.url = @globals[:endpoint] || @wsdl.endpoint
- http.proxy = @globals[:proxy] if @globals.include? :proxy
- http.set_cookies @globals[:last_response] if @globals.include? :last_response
+ def configure_auth
+ @http_request.auth.basic(*@globals[:basic_auth]) if @globals.include? :basic_auth
+ @http_request.auth.digest(*@globals[:digest_auth]) if @globals.include? :digest_auth
end
- def configure_timeouts(http)
- http.open_timeout = @globals[:open_timeout] if @globals.include? :open_timeout
- http.read_timeout = @globals[:read_timeout] if @globals.include? :read_timeout
- end
+ end
- def configure_headers(http)
- http.headers = @globals[:headers] if @globals.include? :headers
- http.headers["SOAPAction"] ||= %{"#{soap_action}"} if soap_action
- http.headers["Content-Type"] = CONTENT_TYPE[@globals[:soap_version]] % @globals[:encoding]
- end
+ class WSDLRequest < HTTPRequest
- def configure_ssl(http)
- http.auth.ssl.ssl_version = @globals[:ssl_version] if @globals.include? :ssl_version
- http.auth.ssl.verify_mode = @globals[:ssl_verify_mode] if @globals.include? :ssl_verify_mode
+ def build
+ configure_proxy
+ configure_timeouts
+ configure_ssl
+ configure_auth
- http.auth.ssl.cert_key_file = @globals[:ssl_cert_key_file] if @globals.include? :ssl_cert_key_file
- http.auth.ssl.cert_file = @globals[:ssl_cert_file] if @globals.include? :ssl_cert_file
- http.auth.ssl.ca_cert_file = @globals[:ssl_ca_cert_file] if @globals.include? :ssl_ca_cert_file
+ @http_request
end
- def configure_auth(http)
- http.auth.basic(*@globals[:basic_auth]) if @globals.include? :basic_auth
- http.auth.digest(*@globals[:digest_auth]) if @globals.include? :digest_auth
- end
+ end
- def soap_action
- return if @locals.include?(:soap_action) && !@locals[:soap_action]
- return @soap_action if defined? @soap_action
+ class SOAPRequest < HTTPRequest
- soap_action = @locals[:soap_action]
- soap_action ||= @wsdl.soap_action(@operation_name.to_sym) if @wsdl.document?
- soap_action ||= Gyoku.xml_tag(@operation_name, :key_converter => @globals[:convert_request_keys_to])
+ CONTENT_TYPE = {
+ 1 => "text/xml;charset=%s",
+ 2 => "application/soap+xml;charset=%s"
+ }
- @soap_action = soap_action
- end
+ def build(options = {})
+ configure_proxy
+ configure_cookies options[:cookies]
+ configure_timeouts
+ configure_headers options[:soap_action]
+ configure_ssl
+ configure_auth
- def log_request(url, headers, body)
- logger.info "SOAP request: #{url}"
- logger.info headers_to_log(headers)
- logger.debug body_to_log(body)
+ @http_request
end
- def log_response(code, body)
- logger.info "SOAP response (status #{code})"
- logger.debug body_to_log(body)
- end
+ private
- def headers_to_log(headers)
- headers.map { |key, value| "#{key}: #{value}" }.join(", ")
+ def configure_cookies(cookies)
+ @http_request.set_cookies(cookies) if cookies
end
- def body_to_log(body)
- LogMessage.new(body, @globals[:filters], @globals[:pretty_print_xml]).to_s
- end
-
- def logger
- @globals[:logger]
+ def configure_headers(soap_action)
+ @http_request.headers = @globals[:headers] if @globals.include? :headers
+ @http_request.headers["SOAPAction"] ||= %{"#{soap_action}"} if soap_action
+ @http_request.headers["Content-Type"] ||= CONTENT_TYPE[@globals[:soap_version]] % @globals[:encoding]
end
end
end