gpt4 book ai didi

python - 使用 Python Zeep 反省 WSDL

转载 作者:太空狗 更新时间:2023-10-29 21:41:27 27 4
gpt4 key购买 nike

我正在尝试使用 Zeep 来描述给定 WSDL 中的操作和类型,以便程序知道操作名称、它们的参数名称、参数类型和参数属性。

此信息将用于为给定的 WSDL 动态生成 UI。

到目前为止,我得到的只是操作和类型的字符串表示。使用类似于 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())

这给出如下输出(为简洁起见缩短)

[...]

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() 遍历每个对象属性,但它们不包含此信息。它似乎嵌套得更深。

我可以自己解析字符串表示,但是我也不知道参数是否是可选的(更具体地说,如果它具有属性 minOccurs=0

好像SOAPpy actually has this functionality , 但不再维护。

那么有没有一种方法可以使用 zeep 来检查 WSDL,它提供关于每个操作的详细信息,它的参数名称、类型和类似于 SOAPpy 实现的属性?或者我应该解析签名,或者使用常规 XML 解析器解析 WSDL。

最佳答案

根据 jordanm 的回答,我使用以下方法获取可用方法所需的所有数据

from zeep import Client
from pprint import pprint

wsdl = 'http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl'
client = Client(wsdl)


def parseElements(elements):
all_elements = {}
for name, element in elements:
all_elements[name] = {}
all_elements[name]['optional'] = element.is_optional
if hasattr(element.type, 'elements'):
all_elements[name]['type'] = parseElements(
element.type.elements)
else:
all_elements[name]['type'] = str(element.type)

return all_elements


interface = {}
for service in client.wsdl.services.values():
interface[service.name] = {}
for port in service.ports.values():
interface[service.name][port.name] = {}
operations = {}
for operation in port.binding._operations.values():
operations[operation.name] = {}
operations[operation.name]['input'] = {}
elements = operation.input.body.type.elements
operations[operation.name]['input'] = parseElements(elements)
interface[service.name][port.name]['operations'] = operations


pprint(interface)

关于python - 使用 Python Zeep 反省 WSDL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50089400/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com