我试图使用 Zeep 来描述给定 WSDL 中的操作和类型,以便程序知道操作名称、它们的参数名称、参数类型和参数属性。
此信息将用于为给定的 WSDL 动态生成 UI。
到目前为止,我得到的只是操作和类型的字符串表示。使用与此答案中的代码类似的代码。
这是一个例子:
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())
这给出如下输出(为简洁起见缩短)
[...]
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() 遍历每个对象 attrs,但它们不包含此信息。它似乎嵌套得更深。
我可以自己解析字符串表示,但是我也错过了参数是否是可选的(更具体地说,如果它具有属性 minOccurs=0
似乎SOAPpy 实际上具有此功能,但不再维护。
那么有没有办法用 zeep 自省 WSDL,它提供关于每个操作的详细信息,它的参数名称、类型和类似于 SOAPpy 实现的属性?或者我应该解析签名,还是使用常规 XML 解析器解析 WSDL。