如何使用Zeep准备SOAP请求?

时间:2018-12-14 11:25:00

标签: python zeep

我正在尝试使用在此处发布的特别神秘的WSDL SOAP服务: https://api.business.govt.nz/registry/resource/_system/governance/apimgt/applicationdata/provider/mbiecreator/DiscussionCorrespondence/v1/documentation/files/DiscussionCorrespondence.zip

文档提供了一个示例Soap查询:

<soapenv:Envelope xmlns:list="http://www.iponz.govt.nz/XMLSchema/Discussion/List" xmlns:ser="http://www.iponz.govt.nz/services" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <soapenv:Header/>
 <soapenv:Body>
  <ser:getDiscussionList>
   <list:Transaction>
    <list:DiscussionTransactionBody>
     <list:TransactionContentDetails>
      <list:TransactionCode>Get Discussion List</list:TransactionCode>
      <list:TransactionData>
       <list:DiscussionList>
        <list:DiscussionListDetails>
         <list:DiscussionCreationDateFrom>2015-02-10</list:DiscussionCreationDateFrom>
         <list:DiscussionCreationDateTo>2015-02-10</list:DiscussionCreationDateTo>
        </list:DiscussionListDetails>
       </list:DiscussionList>
      </list:TransactionData>
     </list:TransactionContentDetails>
    </list:DiscussionTransactionBody>
   </list:Transaction>
  </ser:getDiscussionList>
 </soapenv:Body>
</soapenv:Envelope>

我想做的是使用Zeep复制此原始查询。我该怎么做呢? 谢谢

1 个答案:

答案 0 :(得分:1)

我找到了tutorial here,将我带入正确的轨道。这是我的代码:

from zeep import Client, Settings

#Creation of headerArr is excluded as it's not relevant to the xml produced
headerArr = {}
settings = Settings(strict=False, xml_huge_tree=True,extra_http_headers=headerArr)
client = Client('DiscussionCorrespondence/wsdls/DiscussionCorrespondence.wsdl',
    settings=settings)
requestData = {
    'Transaction': {
        'DiscussionTransactionBody': {
            'TransactionContentDetails': {
                'TransactionCode':'Get Discussion List',
                'TransactionData': {
                    'DiscussionList': {
                        'DiscussionListDetails': {
                                'DiscussionCreationDateFrom':'2015-02-10',
                                'DiscussionCreationDateTo':'2015-02-10',
                        },
                    },
                },
            },
        },
    },
}
res = client.service.getDiscussionList(**requestData)

(从调试日志中)发送的经过修饰的XML请求是:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
 <soap-env:Body>
  <ns0:getDiscussionList xmlns:ns0="http://www.iponz.govt.nz/services">
   <ns1:Transaction xmlns:ns1="http://www.iponz.govt.nz/XMLSchema/Discussion/List">
    <ns1:DiscussionTransactionBody>
     <ns1:TransactionContentDetails>
      <ns1:TransactionCode>Get Discussion List</ns1:TransactionCode>
      <ns1:TransactionData>
       <ns1:DiscussionList>
        <ns1:DiscussionListDetails>
         <ns1:DiscussionCreationDateFrom>2015-02-10</ns1:DiscussionCreationDateFrom>
         <ns1:DiscussionCreationDateTo>2015-02-10</ns1:DiscussionCreationDateTo>
        </ns1:DiscussionListDetails>
       </ns1:DiscussionList>
      </ns1:TransactionData>
     </ns1:TransactionContentDetails>
    </ns1:DiscussionTransactionBody>
   </ns1:Transaction>
  </ns0:getDiscussionList>
 </soap-env:Body>
</soap-env:Envelope>

此XML与问题中的XML不同,但在功能上应相同。