How to create arrayType for WSDL in Python (using suds)?

Uri picture Uri · Jul 15, 2009 · Viewed 10.1k times · Source

Environment:

  • Python v2.6.2
  • suds v0.3.7

The WSDL (server) I work with, have the following schema sub-sections (I tried to write it clearly using plain text) -


[ sub-section #1 ]

searchRequest: (searchRequest){
    userIdentification = (userIdentification){
        username = ""
        password = ""
        }
    itineraryArr = (itineraryArray){
        _arrayType = ""
        _offset = ""
        _id = ""
        _href = ""
        _arrayType = ""
        }
   ...
   ...

[ sub-section #2 ]

itinerary: (itinerary){
    departurePoint = (locationPoint){
        locationId = None
        radius = None
        }
    arrivalPoint = (locationPoint){
        locationId = None
        radius = None
        }
   ...
   ...

There is no problem with 'userIdentification' (which is a "simple" type)

But, 'itineraryArr' is an array of 'itinerary', and I don't know how to use python to create XML array.

I tried few combinations, for example

itinerary0 = self.client.factory.create('itinerary')
itineraryArray = self.client.factory.create('itineraryArray')
itineraryArray = [itinerary0]
searchRequest.itineraryArr = itineraryArray

But all my trials resulted with the same server error -

    Server raised fault: 'Cannot use object of type itinerary as array'
    (Fault){
       faultcode = "SOAP-ENV:Server"
       faultstring = "Cannot use object of type itinerary as array"
     }

Answer

llemeur picture llemeur · Jan 20, 2011

I'm in the same case, with a RPC/encoded style WS and a method that contains a soap array. a print request (where request = client.factory.create('Request')) gives:

(Request){
  requestid = None
  option = 
    (ArrayOfOption){
     _arrayType = ""
     _offset = ""
     _id = ""
     _href = ""
     _arrayType = ""
  }
 }

The solution given by Jacques (1request.option.append(option1)1) does not work, as it ends with an error message ArrayOfOption instance has no attribute append.

The solution given by mcauth looks like this:

array = client.factory.create('ArrayOfOption')
array.item = [option1,  option2,  option3,  option4,  option5,  option6]
request.option=array

It works so so, as the resulting SOAP message shows no arrayType attribute:

<option xsi:type="ns3:ArrayOfOption">

The best solution I found is also the simplest:

request.option = [option1,  option2,  option3,  option4,  option5,  option6]

It ends with a good SOAP message:

<option xsi:type="ns0:ArrayOfOption" ns3:arrayType="ns0:Option[6]">

as expected by the server side WS.