Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soap Client using Suds

Tags:

python

soap

suds

Soap call in Python

Hi above is my previous question regarding soap. In there i am passing a 1D array. Now my problem is i need to pass the 2D array to the following Soap schema.

Request Schema

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CalculateWeb2DObjectArray xmlns="http://tempuri.org/">
      <HCID>string</HCID>
      <jaggedobjDataMICRO>
        <ArrayOfAnyType>
          <anyType />
          <anyType />
        </ArrayOfAnyType>
        <ArrayOfAnyType>
          <anyType />
          <anyType />
        </ArrayOfAnyType>
      </jaggedobjDataMICRO>
      <numeratorID>int</numeratorID>
    </CalculateWeb2DObjectArray>
  </soap:Body>
</soap:Envelope>

Response Schema

 <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <CalculateWeb2DObjectArrayResponse xmlns="http://tempuri.org/">
          <CalculateWeb2DObjectArrayResult>
            <ArrayOfAnyType>
              <anyType />
              <anyType />
            </ArrayOfAnyType>
            <ArrayOfAnyType>
              <anyType />
              <anyType />
            </ArrayOfAnyType>
          </CalculateWeb2DObjectArrayResult>
        </CalculateWeb2DObjectArrayResponse>
      </soap:Body>
    </soap:Envelope>

My Code

from suds.xsd.doctor import Import, ImportDoctor
from suds.client import Client

# enable logging to see transmitted XML
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

# fix broken wsdl
# add <s:import namespace="http://www.w3.org/2001/XMLSchema"/> to the wsdl
imp = Import('http://www.w3.org/2001/XMLSchema',
             location='http://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://tempuri.org/')
wsdl_url = 'http://204.9.76.243/nuCast.DataFeedService/Service1.asmx?WSDL'
client = Client(wsdl_url, doctor=ImportDoctor(imp))

# make request
arrayofstring1 = client.factory.create('ArrayOfString')
arrayofstring1.string = [1,2]
arrayofstring2 = client.factory.create('ArrayOfString')
arrayofstring2.string = [5,6]
arrayofstring = client.factory.create('ArrayOfString')
arrayofstring.string = [arrayofstring1,arrayofstring2]


print client.service.CalculateWeb2DObjectArray(1073757, arrayofstring, 99)

But i got empty value in output.Plz help to solve this.

Thanks

like image 744
Milin Avatar asked Apr 24 '11 06:04

Milin


People also ask

How do you install suds?

Download the suds egg from https://pypi.python.org/pypi/suds. Run cmd.exe as an administrator to open a command prompt. Change directory to where you installed Python, and then change directory to the Scripts subdirectory. To install SUDS, run the easy_install.exe program and point it to the suds egg that you download.

How do I make a SOAP request in Python?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

How to implement soap RPC in Python with suds?

SOAP is an RPC (Remote Procedure Call) that uses object-oriented protocol. Suds is actually lightweight SOAP python client that provides a service proxy for web services. For implementing the suds client follow the below steps. Install suds library. Suds can be installed as, Import Client from suds as shown in the example below.

What is Suds client in Python?

In this article i am going to explain briefly the implementation of Suds Client. Suds is a lightweight library that uses SOAP based clients for python. SOAP is an RPC (Remote Procedure Call) that uses object-oriented protocol. Suds is actually lightweight SOAP python client that provides a service proxy for web services.

What is Suds?

Suds is a lightweight library that uses SOAP based clients for python. SOAP is an RPC (Remote Procedure Call) that uses object-oriented protocol. Suds is actually lightweight SOAP python client that provides a service proxy for web services.

What is the purpose of the [Suds client] factory?

This object is used to invoke operations (methods) provided by the service endpoint. The [suds.client.Factory-class.html factory] namespace provides a factory that may be used to create instances of objects and types defined in the WSDL. You will need to know the url for WSDL for each service used.


1 Answers

You pass invalid arguments to CalculateWeb2DObjectArray() function.

To find out what type of arguments CalculateWeb2DObjectArray() accepts, you could add to your script:

print client

The output contains:

CalculateWeb2DObjectArray(xs:string HCID,
                          ArrayOfArrayOfAnyType jaggedobjDataMICRO,
                          xs:int numeratorID, )

So the second argument should be ArrayOfArrayOfAnyType, use client.factory to create it:

aoaoat = client.factory.create('ArrayOfArrayOfAnyType')

To find out how to populate aoaoat, just print it:

print aoaoat

The output:

(ArrayOfArrayOfAnyType){
  ArrayOfAnyType[] = <empty>
}

Repeating the same procedure for ArrayOfAnyType you get:

(ArrayOfAnyType){
  anyType[] = <empty>
}

Putting it all together:

aoaoat = client.factory.create('ArrayOfArrayOfAnyType')
lst = aoaoat.ArrayOfAnyType = []
for L in [[1,2], [5,6]]:
    aoat = client.factory.create('ArrayOfAnyType')
    aoat.anyType = L
    lst.append(aoat)
response = client.service.CalculateWeb2DObjectArray(1073757, aoaoat, 99)
print response

Request

DEBUG:suds.client:sending to (
  http://204.9.76.243/nuCast.DataFeedService/Service1.asmx)
message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://tempuri.org/"
  xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:CalculateWeb2DObjectArray>
         <ns0:HCID>1073757</ns0:HCID>
         <ns0:jaggedobjDataMICRO>
            <ns0:ArrayOfAnyType>
               <ns0:anyType>1</ns0:anyType>
               <ns0:anyType>2</ns0:anyType>
            </ns0:ArrayOfAnyType>
            <ns0:ArrayOfAnyType>
               <ns0:anyType>5</ns0:anyType>
               <ns0:anyType>6</ns0:anyType>
            </ns0:ArrayOfAnyType>
         </ns0:jaggedobjDataMICRO>
         <ns0:numeratorID>99</ns0:numeratorID>
      </ns0:CalculateWeb2DObjectArray>
   </ns1:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {
  'SOAPAction': u'"http://tempuri.org/CalculateWeb2DObjectArray"',
  'Content-Type': 'text/xml; charset=utf-8'}

Response

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <CalculateWeb2DObjectArrayResponse xmlns="http://tempuri.org/">
      <CalculateWeb2DObjectArrayResult>
        <ArrayOfAnyType>
          <anyType>1</anyType>
          <anyType>2</anyType>
        </ArrayOfAnyType>
        <ArrayOfAnyType>
          <anyType>5</anyType>
          <anyType>6</anyType>
        </ArrayOfAnyType>
      </CalculateWeb2DObjectArrayResult>
    </CalculateWeb2DObjectArrayResponse>
  </soap:Body>
</soap:Envelope>

Output

(ArrayOfArrayOfAnyType){
   ArrayOfAnyType[] = 
      (ArrayOfAnyType){
         anyType[] = 
            "1",
            "2",
      },
      (ArrayOfAnyType){
         anyType[] = 
            "5",
            "6",
      },
 }
like image 124
jfs Avatar answered Oct 03 '22 00:10

jfs