Class: WSDL::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wsdl/client.rb

Overview

Main entry point for working with WSDL documents.

This class provides a high-level interface for parsing WSDL documents, inspecting services and operations, and executing SOAP requests.

Examples:

Basic usage

client = WSDL::Client.new('http://example.com/service?wsdl')
client.services
# => {"ExampleService" => {ports: {"ExamplePort" => {type: "...", location: "..."}}}}

Calling an operation (single service/port WSDL)

client = WSDL::Client.new('http://example.com/service?wsdl')
operation = client.operation('GetData')
operation.prepare do
  tag('GetData') { tag('id', 123) }
end
response = operation.invoke

Calling an operation (multi-service WSDL)

client = WSDL::Client.new('http://example.com/service?wsdl')
operation = client.operation('ExampleService', 'ExamplePort', 'GetData')

With custom HTTP adapter

client = WSDL::Client.new('http://example.com/service?wsdl', http: my_adapter)

Disable XML formatting for whitespace-sensitive servers

client = WSDL::Client.new('http://example.com/service?wsdl', format_xml: false)

Disable caching for this instance

client = WSDL::Client.new('http://example.com/service?wsdl', cache: false)

Custom sandbox paths for local imports spanning multiple directories

client = WSDL::Client.new('/path/to/service.wsdl',
                          sandbox_paths: ['/path/to', '/other/schemas'])

Reusable configuration

config = WSDL::Config.new(format_xml: false, strict_schema: false)
client = WSDL::Client.new('http://example.com/service?wsdl', config:)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wsdl, http: nil, cache: nil, config: nil) ⇒ Client

Creates a new Client instance.

Behavioral options (format_xml, strict_schema, sandbox_paths, limits) can be passed as keyword arguments or grouped into a WSDL::Config object via the config: parameter. When both are provided, keyword arguments take precedence.

Parameters:

  • wsdl (String)

    a URL or file path to the WSDL document

  • http (Object, nil) (defaults to: nil)

    an optional HTTP adapter instance (defaults to a new instance of WSDL.http_adapter)

  • cache (Cache, nil, false) (defaults to: nil)

    the cache to use for parsed definitions. Use nil (default) to use WSDL.cache, or false to disable caching.

  • config (Config, nil) (defaults to: nil)

    a reusable WSDL::Config instance grouping behavioral options. Any keyword arguments for Config options override the config object. Additional keyword arguments are forwarded to WSDL::Config#initialize. See WSDL::Config#initialize for the full list of supported options.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/wsdl/client.rb', line 61

def initialize(wsdl, http: nil, cache: nil, config: nil, **)
  @config = config ? config.with(**) : Config.new(**)

  source = Source.validate_wsdl!(wsdl)
  @http = http || WSDL.http_adapter.new

  validate_http_adapter!(@http)

  resolved_sandbox_paths = source.resolve_sandbox_paths(@config.sandbox_paths)
  @parser_result = Parser::CachedResult.load(
    wsdl:,
    http: @http,
    cache:,
    parse_options: ParseOptions.new(
      sandbox_paths: resolved_sandbox_paths,
      limits: @config.limits,
      strict_schema: @config.strict_schema
    )
  )
end

Instance Attribute Details

#configConfig (readonly)

Returns the behavioral configuration for this client.

Returns:

  • (Config)

    the configuration instance



86
87
88
# File 'lib/wsdl/client.rb', line 86

def config
  @config
end

Instance Method Details

#httpObject

Returns the HTTP adapter's config for customizing timeouts, SSL, etc.

Returns:

  • (Object)

    the adapter configuration object



92
93
94
# File 'lib/wsdl/client.rb', line 92

def http
  @http.config
end

#operation(service_name, port_name, operation_name) ⇒ Operation #operation(operation_name) ⇒ Operation

Returns an Operation instance for calling a SOAP operation.

Overloads:

  • #operation(service_name, port_name, operation_name) ⇒ Operation

    Returns an operation for the specified service, port, and operation.

    Parameters:

    • service_name (String, Symbol)

      the name of the service

    • port_name (String, Symbol)

      the name of the port

    • operation_name (String, Symbol)

      the name of the operation

  • #operation(operation_name) ⇒ Operation

    Returns an operation by name, auto-resolving the only service and port. Requires exactly one service with exactly one port.

    Parameters:

    • operation_name (String, Symbol)

      the name of the operation

Returns:

Raises:

  • (ArgumentError)

    if the service, port, or operation does not exist, or if auto-resolution is used with multiple services/ports

  • (UnsupportedStyleError)

    if the operation uses an unsupported style (e.g., rpc/encoded)



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/wsdl/client.rb', line 163

def operation(service_name_or_operation_name, port_name = nil, operation_name = nil)
  if port_name && !operation_name
    raise ArgumentError,
          'Pass 1 argument (operation_name) or 3 arguments (service_name, port_name, operation_name).'
  end

  if operation_name
    service_name = service_name_or_operation_name
  else
    operation_name = service_name_or_operation_name
    service_name, port_name = resolve_service_and_port(nil, nil)
  end

  operation_info = @parser_result.operation(service_name.to_s, port_name.to_s, operation_name.to_s)
  verify_operation_style!(operation_info)

  Operation.new(
    operation_info,
    @parser_result,
    @http,
    config: @config
  )
end

#operations(service_name, port_name) ⇒ Array<String> #operationsArray<String>

Returns an array of operation names for a service and port.

Overloads:

  • #operations(service_name, port_name) ⇒ Array<String>

    Returns operations for the specified service and port.

    Parameters:

    • service_name (String, Symbol)

      the name of the service

    • port_name (String, Symbol)

      the name of the port

  • #operationsArray<String>

    Returns operations for the only service and port. Requires exactly one service with exactly one port.

Returns:

  • (Array<String>)

    the list of operation names

Raises:

  • (ArgumentError)

    if the service or port does not exist, or if auto-resolution is used with multiple services/ports



136
137
138
139
140
141
142
143
# File 'lib/wsdl/client.rb', line 136

def operations(service_name = nil, port_name = nil)
  if service_name && !port_name
    raise ArgumentError, 'Pass 0 arguments (auto-resolve) or 2 arguments (service_name, port_name).'
  end

  service_name, port_name = resolve_service_and_port(service_name, port_name)
  @parser_result.operations(service_name.to_s, port_name.to_s)
end

#service_nameString?

Returns the name of the primary service defined by the WSDL.

Falls back to the first discovered service key when the root definitions element does not provide a name attribute.

Returns:

  • (String, nil)

    the service name, or nil if no service exists



114
115
116
117
118
119
# File 'lib/wsdl/client.rb', line 114

def service_name
  name = @parser_result.service_name
  return name if name && !name.empty?

  @parser_result.services.keys.first
end

#servicesHash

Returns the services and ports defined by the WSDL.

Examples:

client.services
# => {"ServiceName" => {ports: {"PortName" => {type: "...", location: "..."}}}}

Returns:

  • (Hash)

    a hash of service names to their port definitions



104
105
106
# File 'lib/wsdl/client.rb', line 104

def services
  @parser_result.services
end