Bombardare per la pace è un po’ come trombare per la verginità…

Interrogare un servizio SOAP con Ruby

Come spesso accade Windows & Co. fanno le cose a modo loro. È il caso del web service SOAP (quando REST funziona bene e anche meglio) che, causa lavoro, ho dovuto interrogare. Per farlo ho utilizzato Ruby con l’aiuto della libreria soap/wsdlDriver.

Qui di seguito un codice di esempio per connettersi al web service e gestire la risposta alla chiamata a getCourses208913

Scarica il file SOAP-wsdlDriver.rb

#
# SOAP service query example
#
# Usage:
#
# course = CourseManagement.new
# course.getCourses
#
# Author::    Tommaso Visconti  (mailto:tommaso.visconti@kreations.it)
# Copyright:: Copyright (c) 2009 Kreations Agency
# License::   General Public License v.2

require 'soap/wsdlDriver'

class CourseManagement

  # Initialize the SOAP service
	def initialize
		@SOAPUrl = "http://HOST_URL/ws/ws208913.asmx?WSDL"
		@service=SOAP::WSDLDriverFactory.new(@SOAPUrl).create_rpc_driver
		@service.generate_explicit_type = true
		@service.wiredump_dev = STDOUT if $DEBUG
	end

	# Get the possible callbacks
	def getMethods
	 @service.methods.sort
	end

	#
	# Get the courses
	#
	# The first call use :IDParent => 0 which indicates the root of the tree
	#
	def getCourses
		result = @service.getCourses208913(:IDParent => 0)

		if result.getCourses208913Result.to_a.size > 0
			puts "# Code => " + result.getCourses208913Result.codeDescription.code
			puts "# Description => " + result.getCourses208913Result.codeDescription.description
			puts

			self.getCourse(result.getCourses208913Result.codeDescription.code, 1)
		end
	end

	def getCourse(idParent, count)
		result = @service.getCourses208913(:IDParent => idParent)

		if result.getCourses208913Result.to_a.size > 0

			result.getCourses208913Result.codeDescription.each do |course|
				puts " "*count*2 + "# Code => " + course.code
				puts " "*count*2 + "# Description => " + course.description
				puts

				# Recoursive call
				self.getCourse(course.code, count+1) if result.getCourses208913Result.to_a.size > 1
			end
		end
	end
end
Condividi questo post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Identi.ca
  • LinkedIn
  • Reddit
  • Twitter
  • Ping.fm
  • superion
    Ciao, ti è mai capitato di accedere ad un webservice SOAP sotto basic authentication?
    Io ne ho uno microsozz che però richiede uno username che contiene pure il carattere chiocciolina @
    Ti riporto il codice:

    require 'soap/wsdlDriver'
    WSDL_URL = 'http://grl.lloy.net/grlweb/services/grlws.asmx?WSDL'
    s = SOAP::WSDLDriverFactory.new(WSDL_URL).create_rpc_driver
    s.options['protocol.http.basic_auth'] << [WSDL_URL, 'websiteuser@lloy.net', 'zjikh4wxr10q']

    `follow_redirect': unexpected response: #<http::message::headers:0x3a3093c "1656"],="" "asp.net"],="" "basic="" "microsoft-iis="" "text="" "thu,="" ,="" 12:21:30="" 2009="" 23="" 6.0"],="" @body_charset="nil," @body_date="nil," @body_size="0," @body_type="nil," @chunked="false," @dumped="false," @header_item="[["Content-Length"," @http_version="1.1" @is_request="false," @reason_phrase="Unauthorized" @request_method="nil," @request_query="nil," @request_uri="nil" @request_via_proxy="nil," @status_code="401," ["content-type"="" ["date",="" ["server",="" ["www-authenticate",="" ["x-powered-by",="" gmt"]],="" html"],="" jul="" realm="\"grl.lloy.net\""],"> (HTTPClient::BadResponseError)

    Ovviamente perchè per accedere al WSDL devo prima autenticarmi.

    però il codice si schianta già nella riga dove istanzio WSDLDriverFactory.
    Nella console mi trovo:
    </http::message::headers:0x3a3093c>
blog comments powered by Disqus