Class: Savon::Request

Inherits:
Object show all
Includes:
Logger
Defined in:
lib/savon/request.rb

Overview

Savon::Request

Savon::Request handles both WSDL and SOAP requests.

The Net::HTTP object

You can access the Net::HTTP object used for both WSDL and SOAP requests via:

client.request.http

Here’s an example of how to set open and read timeouts on the Net::HTTP object.

client.request.http.open_timeout = 30
client.request.http.read_timeout = 30

Please refer to the Net::HTTP documentation for more information.

HTTP basic authentication

Setting credentials for HTTP basic authentication:

client.request.basic_auth "username", "password"

SSL client authentication

You can use the methods provided by Net::HTTP to set SSL client authentication or use a shortcut:

client.request.http.ssl_client_auth(
  :cert => OpenSSL::X509::Certificate.new(File.read("client_cert.pem")),
  :key => OpenSSL::PKey::RSA.new(File.read("client_key.pem"), "password if one exists"),
  :ca_file => "cacert.pem",
  :verify_mode => OpenSSL::SSL::VERIFY_PEER
)

HTTP headers

There’s an accessor for the Hash of HTTP headers sent with any SOAP call:

client.request.headers["custom"] = "header"

Constant Summary collapse

ContentType =

Content-Types by SOAP version.

{ 1 => "text/xml;charset=UTF-8", 2 => "application/soap+xml;charset=UTF-8" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

included, #log

Constructor Details

#initialize(endpoint, options = {}) ⇒ Request

Expects a WSDL or SOAP endpoint and accepts a custom proxy address.



50
51
52
53
54
# File 'lib/savon/request.rb', line 50

def initialize(endpoint, options = {})
  @endpoint = URI endpoint
  @proxy = URI options[:proxy] || ""
  headers["Accept-encoding"] = "gzip,deflate" if options[:gzip]
end

Instance Attribute Details

#endpointObject (readonly)

Returns the endpoint URI.



57
58
59
# File 'lib/savon/request.rb', line 57

def endpoint
  @endpoint
end

#proxyObject (readonly)

Returns the proxy URI.



60
61
62
# File 'lib/savon/request.rb', line 60

def proxy
  @proxy
end

Instance Method Details

#basic_auth(username, password) ⇒ Object

Sets the username and password for HTTP basic authentication.



73
74
75
# File 'lib/savon/request.rb', line 73

def basic_auth(username, password)
  @basic_auth = [username, password]
end

#headersObject

Returns the HTTP headers for a SOAP request.



63
64
65
# File 'lib/savon/request.rb', line 63

def headers
  @headers ||= {}
end

#headers=(headers) ⇒ Object

Sets the HTTP headers for a SOAP request.



68
69
70
# File 'lib/savon/request.rb', line 68

def headers=(headers)
  @headers = headers if headers.kind_of? Hash
end

#httpObject

Returns the Net::HTTP object.



100
101
102
# File 'lib/savon/request.rb', line 100

def http
  @http ||= Net::HTTP::Proxy(@proxy.host, @proxy.port).new @endpoint.host, @endpoint.port
end

#soap(soap) ⇒ Object

Executes a SOAP request using a given Savon::SOAP instance and returns the Net::HTTP response.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/savon/request.rb', line 86

def soap(soap)
  @soap = soap
  http.endpoint @soap.endpoint.host, @soap.endpoint.port
  http.use_ssl = @soap.endpoint.ssl?

  log_request
  @response = http.start do |h|
    h.request request(:soap) { |request| request.body = @soap.to_xml }
  end
  log_response
  @response
end

#wsdlObject

Retrieves WSDL document and returns the Net::HTTP response.



78
79
80
81
82
83
# File 'lib/savon/request.rb', line 78

def wsdl
  log "Retrieving WSDL from: #{@endpoint}"
  http.endpoint @endpoint.host, @endpoint.port
  http.use_ssl = @endpoint.ssl?
  http.start { |h| h.request request(:wsdl) }
end