问题描述
我正在尝试使用 Zeep 来描述给定 WSDL 中的操作和类型,以便程序知道操作名称、它们的参数名称、参数类型和参数属性.
I am attempting to use Zeep to describe the operations and types in a given WSDL, so that a program knows the operation names, their parameter names, the parameter types, and parameter attributes.
此信息将用于为给定的 WSDL 动态生成 UI.
This info will be used to dynamically generate a UI for a given WSDL.
到目前为止我得到的只是操作和类型的字符串表示.使用类似于这个答案中的代码.
What I have got so far is just the string representations of the operations and types. Using code similar to what is found in this answer.
这是一个例子:
from zeep import Client
import operator
wsdl = 'http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl'
client = Client(wsdl)
# get each operation signature
for service in client.wsdl.services.values():
print("service:", service.name)
for port in service.ports.values():
operations = sorted(
port.binding._operations.values(),
key=operator.attrgetter('name'))
for operation in operations:
print("method :", operation.name)
print(" input :", operation.input.signature())
print()
print()
# get a specific type signature by name
complextype = client.get_type('ns0:CartGetRequest')
print(complextype.name)
print(complextype.signature())
这给出了如下输出(为简洁起见缩短)
This gives an output like the following (shortened for brevity)
[...]
method : CartCreate
input : MarketplaceDomain: xsd:string, AWSAccessKeyId: xsd:string, AssociateTag: xsd:string, Validate: xsd:string, XMLEscaping: xsd:string, Shared: ns0:CartCreateRequest, Request: ns0:CartCreateRequest[]
method : CartGet
input : MarketplaceDomain: xsd:string, AWSAccessKeyId: xsd:string, AssociateTag: xsd:string, Validate: xsd:string, XMLEscaping: xsd:string, Shared: ns0:CartGetRequest, Request: ns0:CartGetRequest[]
[...]
CartGetRequest
{http://webservices.amazon.com/AWSECommerceService/2011-08-01}CartGetRequest(CartId: xsd:string, HMAC: xsd:string, MergeCart: xsd:string, ResponseGroup: xsd:string[])
.signature() 返回的字符串表示有名称和类型,但我不知道如何单独解析它们.我也尝试过使用 dir() 循环遍历每个对象的属性,但它们不包含此信息.它似乎嵌套得更深.
The string representations returned by .signature() have the names and types, but I don't know how to parse them out individually. I have tried looping over each objects attrs with dir() as well, and they don't contain this info. It seems to be nested much deeper.
我可以自己解析字符串表示,但是我也缺少参数是否是可选的(更具体地说,如果它具有属性 minOccurs=0
I could parse the string representations themselves, but then I am also missing whether or not the parameter is optional (more specifically, if it has the attribute minOccurs=0
看起来 SOAPpy 实际上具有此功能,但不再维护.
It seems like SOAPpy actually has this functionality, but isn't maintained anymore.
那么,有没有一种方法可以使用 zeep 来内省 WSDL,该 zeep 提供有关每个操作的详细信息、参数名称、类型和类似于 SOAPpy 实现的属性?或者我应该解析签名,或者,使用常规 XML 解析器解析 WSDL.
So is there a way to introspect a WSDL with zeep that provides granular information about each operation, it's parameter names, types, and attributes similar to the SOAPpy implementation? Or should I parse the signature, or alternatively, parse the WSDL with a regular XML parser.
推荐答案
您可以使用 operation.input.body.type.elements
访问参数元素,operation.input.body.type.elements
是包含元素对象的元组列表.这些对象包含类型等信息.
You can access parameter elements with operation.input.body.type.elements
, which is a list of tuples containing element objects. These objects contain information such as the type.
(Pdb) operation.input.body.type.elements
[('MarketplaceDomain', <Element(name='MarketplaceDomain', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('AWSAccessKeyId', <Element(name='AWSAccessKeyId', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('AssociateTag', <Element(name='AssociateTag', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('Validate', <Element(name='Validate', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('XMLEscaping', <Element(name='XMLEscaping', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('Shared', <Element(name='Shared', type=<zeep.xsd.dynamic_types.BrowseNodeLookupRequest object at 0x7f1bd8177e48>)>), ('Request', <Element(name='Request', type=<zeep.xsd.dynamic_types.BrowseNodeLookupRequest object at 0x7f1bd8177e48>)>)]
(Pdb) operation.input.body.type.elements[0][1].name
'MarketplaceDomain'
(Pdb) operation.input.body.type.elements[0][1].type.name
'string'
(Pdb) operation.input.body.type.elements[0][1].is_optional
True
这篇关于使用 Python Zeep 内省 WSDL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!