在调用(远程)方法和下载附件时,我已经成功地使用了 zeep。
我现在遇到了一种需要我上传文件的方法。该文件需要作为附件传递。我通常如下调用远程 Web 服务方法:
client.service.fooMethod(arg1,arg2,...)
在我的特殊情况下,arg1 是一个 URI,一个我希望上传到服务器的文件。它需要作为附件上传。我该怎么做呢?
这是一个更具体的例子:
方法名称为 UploadPortfolios,具有以下架构:
<xs:element name="UploadPortfolio">
<xs:complexType>
<xs:sequence>
<xs:element ref="tns:URI"/>
<xs:element ref="tns:PortfolioID"/>
<xs:element ref="tns:AsOfDate"/>
<xs:element minOccurs="0" ref="tns:SuppressPositionLog"/>
<xs:element minOccurs="0" ref="tns:PositionDetailLogAsAttachment"/>
<xs:element minOccurs="0" ref="tns:UploadSharedPortfolio"/>
</xs:sequence>
</xs:complexType>
</xs:element>
我在 python 中使用 zeep 来调用 UploadPortfolios。uri 参数需要包含附件内容 IDcid:<someContentID>
所在的字符串:<someContentID>
portfolio_management_wsdl = 'https://ondemand.uat.riskmetrics.com/ondemand/soap/PortfolioManagement?wsdl'
client_pfm = Client(portfolio_management_wsdl, transport=transport, wsse=wsse)
uri = r'cid://SomeDataFile.xml'
args = {'URI':uri, 'AsOfDate':'20160129'}
result = client_pfm.service.UploadPortfolios(**args)
不用说,上面的内容是行不通的,因为不知何故我需要发送附件。
使用 SoapUI ( https://www.soapui.org/downloads/soapui.html ) 我可以毫无问题地调用该函数。以下是SoapUI生成并发送到服务器的原始数据(部分已省略)
POST https://ondemand.uat.riskmetrics.com/ondemand/soap/PortfolioManagement.PortfolioManagementHttp sSoap12Endpoint/ HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: multipart/related; type="application/xop+xml"; start="<rootpart@soapui.org>"; start-info="application/soap+xml"; action="urn:RiskMetricsWS:1.0:PortfolioManagement:UploadPortfolio"; boundary="----=_Part_46_453204030.1495210657807"
MIME-Version: 1.0
Content-Length: 7668
Host: ondemand.uat.riskmetrics.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
------=_Part_46_453204030.1495210657807
Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"; action="UploadPortfolio"
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart@soapui.org>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:wsdl="http://..." xmlns:xsd="http://..." xmlns:xsd1="http://...">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" ... </soap:Header>
<soap:Body>
<wsdl:UploadPortfolio>
<wsdl:URI><inc:Include href="cid:530345234005" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></wsdl:URI>
<wsdl:PortfolioID>TestPtf_RML4</wsdl:PortfolioID>
<wsdl:AsOfDate>20170509</wsdl:AsOfDate>
<wsdl:SuppressPositionLog>false</wsdl:SuppressPositionLog>
<wsdl:PositionDetailLogAsAttachment>true</wsdl:PositionDetailLogAsAttachment>
<wsdl:UploadSharedPortfolio>true</wsdl:UploadSharedPortfolio>
</wsdl:UploadPortfolio>
</soap:Body>
</soap:Envelope>
------=_Part_46_453204030.1495210657807
Content-Type: text/xml; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-ID: <530345234005>
Content-Disposition: attachment; name="SomeDataFile.xml"
<someInformation>
...
</someInformation>
------=_Part_46_453204030.1495210657807--