我是 zeep 的新手。我有以下效果很好:
import zeep
from zeep.cache import SqliteCache
from zeep.transports import Transport
wsdl = 'https://emaapitest.eset.com/Services/v2015_1/MSPService.svc?singleWsdl'
transport = Transport(cache=SqliteCache())
client = zeep.Client(wsdl=wsdl, transport=transport )
Run Code Online (Sandbox Code Playgroud)
有了上面的内容,我可以将定义的 API 用于大多数调用。例如:
data = {'Username': 'xxxx123', 'Password': 'Secretpassword'}
loginreq = client.service.Login(data)
data = {'LoginID': 'xxxyyy', 'Token': 'gregrwevds543', 'CompanyID': 123}
company_details = client.service.GetCompanyDetails(data)
Run Code Online (Sandbox Code Playgroud)
这一切都很好。但是,对 UpdateSite 的 API 调用需要不同的格式,如下所示:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:msp="http://schemas.datacontract.org/2004/07/MSPApi.Services.v2015_1.Requests">
<soapenv:Header/>
<soapenv:Body>
<tem:UpdateSite>
<tem:request>
<msp:LoginID>123abc</msp:LoginID>
<msp:Token>hpjzncpduqyfreyakcsdilqv</msp:Token>
<msp:LicenseRequests>
<LicenseRequest xmlns:d7="http://schemas.datacontract.org/2004/07/MSPApi.Services.v2015_1.Requests"
i:type="d7:LicenseCreateRequest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MSPApi.Services.v2015_1.ViewModels">
<d7:ProductCode>112</d7:ProductCode>
<d7:Quantity>3</d7:Quantity>
<d7:Trial>false</d7:Trial>
</LicenseRequest>
</msp:LicenseRequests>
<msp:SiteID>123456</msp:SiteID>
</tem:request>
</tem:UpdateSite>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
那就是我需要更改 LicenseRequest 上的命名空间。有什么办法可以生成这个xml(比如使用etree),然后将它传递给zeep?确切的语法将是一个很大的帮助。提前感谢您的任何帮助。
小智 4
下面是根据 WSDL 格式化 SOAP Web 服务 XML 消息的示例:
client.wsdl.dump()在这个例子中,我在;中找到了 Zeep 分配的命名空间前缀。请参阅答案底部附近的示例。
>>> import zeep
>>> client = zeep.Client('https://emaapitest.eset.com/Services/v2015_1/MSPService.svc?singleWsdl')
>>> # note - the namespace ns2 was assigned by zeep - see client.wsdl.dump() tip later in this answer
>>> license_create_type = client.get_type('ns2:LicenseCreateRequest')
>>> # Creating an empty object reminds me of the property names, so I don't have to guess
>>> license_create_type()
{
'Note': None,
'ProductCode': None,
'Quantity': None,
'ServerName': None,
'Trial': None
}
>>> my_create = license_create_type(ProductCode=112,Quantity=3,Trial=False)
>>> my_create
{
'Note': None,
'ProductCode': 112,
'Quantity': 3,
'ServerName': None,
'Trial': False
}
>>> my_type = client.get_type('ns2:SiteUpdateRequest')
>>> # Reference for specifying arrays in Zeep: https://github.com/mvantellingen/python-zeep/issues/145#issuecomment-321614947
>>> req = my_type(LoginID="123",Token="mytoken",LicenseRequests={'LicenseRequest':[my_create]})
>>> # To call the service: (assuming valid credentials?)
>>> # client.service.UpdateSite(req)
Run Code Online (Sandbox Code Playgroud)
以下是 Web 服务请求的 Python 视图:
>>> req
{
'LoginID': '123',
'Token': 'mytoken',
'Active': None,
'LicenseRequests': {
'LicenseRequest': [
{
'Note': None,
'ProductCode': 112,
'Quantity': 3,
'ServerName': None,
'Trial': False
}
]
},
'Name': None,
'SecurityAdminRequests': None,
'SiteID': None,
'SupportLegacyKey': None
}
Run Code Online (Sandbox Code Playgroud)
以下是 Web 服务请求的 XML 视图:
>>> # Debugging output to see raw XML...
>>> node = client.create_message(client.service, 'UpdateSite', req)
>>> from lxml import etree
>>> xml_str = etree.tostring(node, pretty_print=True).decode()
>>> print(xml_str)
Run Code Online (Sandbox Code Playgroud)
>>> import zeep
>>> client = zeep.Client('https://emaapitest.eset.com/Services/v2015_1/MSPService.svc?singleWsdl')
>>> # note - the namespace ns2 was assigned by zeep - see client.wsdl.dump() tip later in this answer
>>> license_create_type = client.get_type('ns2:LicenseCreateRequest')
>>> # Creating an empty object reminds me of the property names, so I don't have to guess
>>> license_create_type()
{
'Note': None,
'ProductCode': None,
'Quantity': None,
'ServerName': None,
'Trial': None
}
>>> my_create = license_create_type(ProductCode=112,Quantity=3,Trial=False)
>>> my_create
{
'Note': None,
'ProductCode': 112,
'Quantity': 3,
'ServerName': None,
'Trial': False
}
>>> my_type = client.get_type('ns2:SiteUpdateRequest')
>>> # Reference for specifying arrays in Zeep: https://github.com/mvantellingen/python-zeep/issues/145#issuecomment-321614947
>>> req = my_type(LoginID="123",Token="mytoken",LicenseRequests={'LicenseRequest':[my_create]})
>>> # To call the service: (assuming valid credentials?)
>>> # client.service.UpdateSite(req)
Run Code Online (Sandbox Code Playgroud)
提示:使用client.wsdl.dump()
我通常通过阅读来client.wsdl.dump()查看 Zeep 生成的映射来开始一个项目,从底部的“绑定”和“操作”开始,然后向后遍历类型。这通常就是您开始所需的全部内容。
>>> import zeep
>>> client = zeep.Client('https://emaapitest.eset.com/Services/v2015_1/MSPService.svc?singleWsdl')
>>> client.wsdl.dump()
Prefixes:
myprefix: http://schemas.datacontract.org/2004/07/MSPApi.Services.v2015_1.Requests
xsd: http://www.w3.org/2001/XMLSchema
ns0: http://tempuri.org/
ns1: http://schemas.microsoft.com/2003/10/Serialization/
ns3: http://schemas.datacontract.org/2004/07/MSPApi.Services.v2015_1.Responses
ns4: http://schemas.microsoft.com/2003/10/Serialization/Arrays
ns5: http://schemas.datacontract.org/2004/07/MSPApi.Services.v2015_1.ViewModels
Global elements:
<SNIP - VERY LONG OUTPUT>
Global types:
<SNIP - VERY LONG OUTPUT>
ns2:SiteUpdateRequest(LoginID: xsd:string, Token: xsd:string, Active: xsd:boolean, LicenseRequests: ns5:ArrayOfLicenseRequest, Name: xsd:string, SecurityAdminRequests: ns2:ArrayOfSecurityAdminRequest, SiteID: xsd:long, SupportLegacyKey: xsd:boolean)
<SNIP - VERY LONG OUTPUT>
Service: MSPService
Port: BasicHttpBinding_IMSPService (Soap11Binding: {http://tempuri.org/}BasicHttpBinding_IMSPService)
Operations:
<SNIP - VERY LONG OUTPUT>
UpdateSite(request: ns2:SiteUpdateRequest) -> UpdateSiteResult: ns3:SiteDetailsResponse
<SNIP - VERY LONG OUTPUT>
>>>
Run Code Online (Sandbox Code Playgroud)