wsdl2py 复杂类型

2 投票
1 回答
2389 浏览
提问于 2025-04-15 20:01

我该如何在SOAP请求中添加复杂类型?

我正在使用WSDL2py生成的请求,并尝试使用它在***_types.py文件中创建的其他类型定义(比如用于身份验证的AccountInfo,这个信息需要放到每个请求中)。然后把它传递给wsdl2py生成的服务器,但我遇到了这个错误:

>>> from AutomotiveDescriptionService6_client import *
>>> from AutomotiveDescriptionService6_types import *
>>> loc = AutomotiveDescriptionService6Locator()
>>> server = loc.getAutomotiveDescriptionService6Port()
>>> request = getDataVersions()
>>> auth = ns0.AccountInfo_Def()
>>> auth._accountName="**"
>>> auth._accountSecret="***"
>>> request._accountInfo = auth
>>> server.getDataVersions(request)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "AutomotiveDescriptionService6_client.py", line 38, in getDataVersions
    self.binding.Send(None, None, request, soapaction="", **kw)
  File "/usr/lib/pymodules/python2.6/ZSI/client.py", line 246, in Send
    sw.serialize(obj, tc)
  File "/usr/lib/pymodules/python2.6/ZSI/writer.py", line 117, in serialize
    elt = typecode.serialize(self.body, self, pyobj, **kw)
  File "/usr/lib/pymodules/python2.6/ZSI/TC.py", line 609, in serialize
    pyobj.typecode.serialize(elt, sw, pyobj, **kw)
  File "/usr/lib/pymodules/python2.6/ZSI/TCcompound.py", line 275, in serialize
    self.cb(elt, sw, pyobj, name=name, **kw)
  File "/usr/lib/pymodules/python2.6/ZSI/TCcompound.py", line 424, in cb
    what.serialize(elem, sw, v, **kw)
  File "/usr/lib/pymodules/python2.6/ZSI/TCcompound.py", line 275, in serialize
    self.cb(elt, sw, pyobj, name=name, **kw)
  File "/usr/lib/pymodules/python2.6/ZSI/TCcompound.py", line 437, in cb
    sw.Backtrace(elt))
ZSI.EvaluateException: Got None for nillable(False), minOccurs(1) element (urn:description6.kp.chrome.com,accountNumber), <ns1:accountInfo xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ZSI="http://www.zolera.com/schemas/ZSI/" xmlns:ns1="urn:description6.kp.chrome.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></ns1:accountInfo>
[Element trace: /SOAP-ENV:Body/ns1:DataVersionsRequest]

如你所见,生成请求对象很简单,wsdl2py使用GED()来提供这些对象,但它并没有暴露出这些请求所需的类。老实说,我完全搞不懂怎么才能把那个复杂类型正确地放进请求对象里,而不出现这个错误。我尝试在**_types.py文件中实例化定义,也尝试了普通的字典,但都没有成功。这里是自动生成的定义,看起来是这样的,有什么建议吗?

class ns0:
    targetNamespace = "urn:description6.kp.chrome.com"

    class AccountInfo_Def(ZSI.TCcompound.ComplexType, TypeDefinition):
        schema = "urn:description6.kp.chrome.com"
        type = (schema, "AccountInfo")
        def __init__(self, pname, ofwhat=(), attributes=None, extend=False, restrict=False, **kw):
            ns = ns0.AccountInfo_Def.schema
            TClist = [ZSI.TC.String(pname=(ns,"accountNumber"), aname="_accountNumber", minOccurs=1, maxOccurs=1, nillable=False, typed=False, encoded=kw.get("encoded")), ZSI.TC.String(pname=(ns,"accountSecret"), aname="_accountSecret", minOccurs=1, maxOccurs=1, nillable=False, typed=False, encoded=kw.get("encoded")), GTD("urn:description6.kp.chrome.com","Locale",lazy=False)(pname=(ns,"locale"), aname="_locale", minOccurs=1, maxOccurs=1, nillable=False, typed=False, encoded=kw.get("encoded"))]
            self.attribute_typecode_dict = attributes or {}
            if extend: TClist += ofwhat
            if restrict: TClist = ofwhat
            ZSI.TCcompound.ComplexType.__init__(self, None, TClist, pname=pname, inorder=0, **kw)
            class Holder:
                typecode = self
                def __init__(self):
                    # pyclass
                    self._accountNumber = None
                    self._accountSecret = None
                    return
            Holder.__name__ = "AccountInfo_Holder"
            self.pyclass = Holder

1 个回答

2

所以...我发现问题是我需要用 --complextypes 这个选项来运行 wsdl2py。这样做会在请求对象里面生成一大堆很棒的方法。这些方法的名字像是 new_XXXXX,其中的 X 是这个请求所需要的复杂类型的名字。

撰写回答