-->

creating any object with python zeep

2019-08-16 03:18发布

问题:

i am pretty new to zeep, and soap. i`am trying to make client request to soap ws function. wsdl of function:

-<s:element name="GetRetailTransactions">
    -<s:complexType>
        -<s:sequence>
            -<s:element name="parameters" maxOccurs="1" minOccurs="0">
                -<s:complexType mixed="true">
                      -<s:sequence>
                             <s:any/>
                       </s:sequence>
                </s:complexType>
           </s:element>
       </s:sequence>
    </s:complexType>
</s:element>

i dont fully understand how to create object with type any in zeep. i have tried :

    wsdl = 'http://domain/app.asmx?WSDL'
    client = Client(wsdl=wsdl)
    params = {

        'RetailTransactionsParameters': {
            'GetNotExportedOnly': '0',
            'GetNotRetrunsOnly': '0',
            'FromDate': '20170518',
            'ToDate': '20170518',
            'TransactionTypeFilter': {
            'TransactionType': '2'
            },
        },
    }
    parameters = client.get_element('ns0:GetRetailTransactions')
    param = xsd.AnyObject(parameters, parameters(params))
    result = client.service.GetRetailTransactions(param)

but i get error:

File "/home/user/lib/python3.6/site-packages/zeep/xsd/elements/indicators.py", line 227, in render
    if name in value:
TypeError: argument of type 'AnyObject' is not iterable

on soapui i can make request and successfully get answer with :

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <GetRetailTransactions xmlns="example.com/">
            <parameters>
                <RetailTransactionsParameters>
                    <GetNotExportedOnly>0</GetNotExportedOnly>
                    <GetNotRetrunsOnly>0</GetNotRetrunsOnly>
                    <FromDate>20170518</FromDate>
                    <ToDate>20170518</ToDate>
                    <TransactionTypeFilter>
                     <TransactionType>2</TransactionType>
                    </TransactionTypeFilter>
                </RetailTransactionsParameters>
            </parameters>
        </GetRetailTransactions>
    </Body>
</Envelope>

maybe someone can guide me how to correctly make such a request with zeep.. thanks in advance

回答1:

I had the same problem today. The get_element method returns the type. To create the object you need to instantiate it. You can either do:

parameters = client.get_element('ns0:GetRetailTransactions')(params)

or you can explicitly set each property:

parameters = client.get_element('ns0:GetRetailTransactions')()
parameters.GetNotExportedOnly = 0
parameters.GetNotRetrunsOnly = 0
...

or you can pass the dict object and zeep does the conversion to the type http://docs.python-zeep.org/en/master/datastructures.html#creating-objects