如何使用 wsdl2py 生成的复杂类型从 python 中的 Web 服务发送响应
我正在使用 ZSI 2.1 并使用一个 wsdl 文件工作,该文件指定类型为“appStatusResponse”的“postAppStatusResponse”消息,其定义为:
<xsd:complexType name="appStatusResponse">
<xsd:sequence>
<xsd:element name="response" type="tns:webServiceResponse" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
并且“webServiceResponse”显示如下:
<xsd:complexType name="webServiceResponse">
<xsd:sequence>
<xsd:element name="responseCode" type="xsd:long"/>
<xsd:element name="responseMessage" type="xsd:string"/>
<xsd:element name="exceptionCode" type="xsd:long"/>
<xsd:element name="exceptionMessage" type="xsd:string" nillable="true" />
<xsd:element name="transactionIdentifier" type="xsd:long"/>
<xsd:element name="traceIdentifier" type="xsd:string"/>
<xsd:element name="transactionTimestamp" type="xsd:dateTime" nillable="true" />
</xsd:sequence>
</xsd:complexType>
要发送 postAppStatusResponse 消息,我显然需要填充它使用 webServiceResponse 和 appStatusResponse 结构。但是,为 postAppStatusResponse 消息生成的类不包含任何支持这一点的内容。 wsdl2py 生成的类型文件包括 appStatusResponse_Def 和 webServiceResponse_Def 类,但我一直无法弄清楚如何使用它们来生成我需要的内容。
生成的 postAppStatusResponse 消息如下所示:
_postAppStatusResponseTypecode = Struct(pname=("http://cig.jpmchase.net/20110218/card/acq/","postAppStatusResponse"), ofwhat=[ns0.appStatusResponse_Def(pname="postAppStatusResponse", aname="_postAppStatusResponse", typed=False, encoded=None, minOccurs=1, maxOccurs=1, nillable=True)], pyclass=None, encoded="http://cig.jpmchase.net/20110218/card/acq/")
class postAppStatusResponse:
typecode = _postAppStatusResponseTypecode
__metaclass__ = pyclass_type
def __init__(self, **kw):
"""Keyword parameters:
postAppStatusResponse -- part postAppStatusResponse
"""
self._postAppStatusResponse = kw.get("postAppStatusResponse")
postAppStatusResponse.typecode.pyclass = postAppStatusResponse
postAppStatusResponse 的 dir() 是:
['PostAppStatusResponse', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_postAppStatusResponse', 'get_element_postAppStatusResponse', 'new_postAppStatusResponse', 'set_element_postAppStatusResponse', 'typecode']
postAppStatusResponse._postAppStatusResponse 的 dir() 是:
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
我使用“wsdl2py --complexType”生成了文件,并且能够使用包含的“get_element_._postAppStatusResponse”。 ..” 函数可以轻松提取请求消息数据。我还编写了其他 ZSI Web 服务,它们可以毫无问题地返回更简单的消息。但是这个......我尝试了很多不同的方法来发送此消息,但还没有找到一种有效的方法。
ZSI 的存在肯定是为了让这种事情变得更简单吗?谁能帮我解决我在这里缺少的东西吗? (我已经尽力避免您从长 xml 文件和代码列表中获取无关数据,但当然可以根据要求提供它们。)
I'm using ZSI 2.1 and working from a wsdl file that specifies a 'postAppStatusResponse' message of type 'appStatusResponse', which is defined as:
<xsd:complexType name="appStatusResponse">
<xsd:sequence>
<xsd:element name="response" type="tns:webServiceResponse" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
and "webServiceResponse" appears as follows:
<xsd:complexType name="webServiceResponse">
<xsd:sequence>
<xsd:element name="responseCode" type="xsd:long"/>
<xsd:element name="responseMessage" type="xsd:string"/>
<xsd:element name="exceptionCode" type="xsd:long"/>
<xsd:element name="exceptionMessage" type="xsd:string" nillable="true" />
<xsd:element name="transactionIdentifier" type="xsd:long"/>
<xsd:element name="traceIdentifier" type="xsd:string"/>
<xsd:element name="transactionTimestamp" type="xsd:dateTime" nillable="true" />
</xsd:sequence>
</xsd:complexType>
To send the postAppStatusResponse message, I evidently need to populate it with the webServiceResponse and appStatusResponse structures. However, the class generated for the postAppStatusResponse message contains nothing to support this. The wsdl2py-generated types file includes appStatusResponse_Def and webServiceResponse_Def classes, but I've been unable to figure out how to use these to generate what I need.
The generated postAppStatusResponse message looks like this:
_postAppStatusResponseTypecode = Struct(pname=("http://cig.jpmchase.net/20110218/card/acq/","postAppStatusResponse"), ofwhat=[ns0.appStatusResponse_Def(pname="postAppStatusResponse", aname="_postAppStatusResponse", typed=False, encoded=None, minOccurs=1, maxOccurs=1, nillable=True)], pyclass=None, encoded="http://cig.jpmchase.net/20110218/card/acq/")
class postAppStatusResponse:
typecode = _postAppStatusResponseTypecode
__metaclass__ = pyclass_type
def __init__(self, **kw):
"""Keyword parameters:
postAppStatusResponse -- part postAppStatusResponse
"""
self._postAppStatusResponse = kw.get("postAppStatusResponse")
postAppStatusResponse.typecode.pyclass = postAppStatusResponse
The dir() of postAppStatusResponse is:
['PostAppStatusResponse', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_postAppStatusResponse', 'get_element_postAppStatusResponse', 'new_postAppStatusResponse', 'set_element_postAppStatusResponse', 'typecode']
and the dir() of postAppStatusResponse._postAppStatusResponse is:
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
I generated the files using "wsdl2py --complexType", and have been able to use the included "get_element_..." functions to extract the request message data without any trouble. I have also written other ZSI web services that return simpler messages without any trouble. But this one... I have tried many many different approaches to sending this message, and have not found one that works.
Surely ZSI exists to make this sort of thing simpler? Can anyone help me with whatever I'm missing here? (I've tried to spare you the extraneous data from long xml files and code listings, but of course can supply them on request.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能有一些代码可以帮助你。我在尝试从 WSDL 生成 python 代码并联系 Outlook OMS SMS 网关时遇到了类似的问题。引用复杂类型的消息将获得您应该调用的 new_complexType 构造函数。但是,如果 WSDL 由于某种原因没有引用它(例如它在调用中被序列化为字符串),则它不会自动生成新的构造函数。复杂类型仍然可以实例化。检查以下代码示例,完整源代码位于 Phonero SMS:
因此,正如您在这一行中看到的那样,您我可以这样构造一个 xmsData Holder 吗:
I might have some code to help you out. I was working with a similar issue in trying to generate python code from a WSDL and contact an Outlook OMS SMS Gateway. Messages that reference complex types will get a new_complexType constructor that you should call. However, if the WSDL doesn't reference it for some reason, like it get serialized as a string in an call, it won't auto-generate the new constructors. The complex type can still be instantiated. Check the following code example, full source at Phonero SMS:
So as you can see on this line you can I construct an xmsData Holder this way:
我想我已经明白了; “简单”的是创建一个与响应消息结构匹配的 Python 类结构,将值写入类成员,并设置 response._postAppStatusResponse = myMsg,其中 myMsg 是新的类结构。
I think I've got it; it was 'simply' a matter of creating a Python class structure matching the response message structure, writing values to the class members, and setting response._postAppStatusResponse = myMsg, where myMsg is the new class structure.