【问题标题】:How to handle complexType arguments in python soap module zeep?如何处理 python 肥皂模块 zeep 中的 complexType 参数?
【发布时间】:2016-07-23 08:02:12
【问题描述】:

Zeep 文档示例:

from zeep import Client

client = Client('http://my-enterprise-endpoint.com')
client.service.submit_order(user_id=1, order={
    'number': '1234',
    'price': 99,
})

我的用例:

我想调用一个需要参数'findCriteria'的网络服务

例子:

findcriteria =  {
        'Criteria' : [{
                        'ColumnName' : 'Closed',
                        'Value' : 0
                },
                {
                        'ColumnName' : 'AssignToQueueID',
                        'Value' : queueid
                },
                {
                        'ColumnName' : 'SupportCallType',
                        'Value' : 'I'
                }
                ]
        }

调用服务:

打印client.service.GetCount(findCriteria = findcriteria)

这是创建的 XML:

<soap-env:Body>
    <ns1:GetCount>
      <ns1:findCriteria/>
    </ns1:GetCount>
  </soap-env:Body>
</soap-env:Envelope>

问题:

虽然服务返回计数,但不应用条件。

当我向服务提供原始 XML 负载时,结果正常。

问题出在&lt;ns1:findCriteria/&gt; 部分。

应为每一列创建一个 Criteria 元素。

WSDL 上 grep GetCount 的结果:

<s:element name="GetCount">
      <s:element name="GetCountResponse">
            <s:element minOccurs="1" maxOccurs="1" name="GetCountResult" type="s:int" />
  <wsdl:message name="GetCountSoapIn">
    <wsdl:part name="parameters" element="tns:GetCount" />
  <wsdl:message name="GetCountSoapOut">
    <wsdl:part name="parameters" element="tns:GetCountResponse" />
    <wsdl:operation name="GetCount">
      <wsdl:input message="tns:GetCountSoapIn" />
      <wsdl:output message="tns:GetCountSoapOut" />
    <wsdl:operation name="GetCount">
      <soap:operation soapAction="http://<server>/webservices/SupportCall/GetCount" style="document" />
    <wsdl:operation name="GetCount">
      <soap12:operation soapAction="http://<server>/webservices/SupportCall/GetCount" style="document" />

【问题讨论】:

    标签: python web-services soap


    【解决方案1】:

    我遇到了类似的问题,我设法解决了它,但为了帮助您,我需要一个正确的 XML 示例(它应该是什么样子),或者至少需要一个使用 SoapUI 生成的默认请求。

    与此同时,也许下面的代码可能会对您有所帮助: 这里的复杂参数是凭据,它包含 2 个登录项,登录名和域。

    <soap-env:Envelope xmlns:ns1="http://xml.kamsoft.pl/ws/kaas/login_types" 
    xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
      <soap-env:Body>
        <ns1:login>
          <ns1:credentials>
            <ns1:item>
              <ns1:name>login</ns1:name>
              <ns1:value>
                 <ns1:stringValue>login_here</ns1:stringValue>
              </ns1:value>
            </ns1:item>
            <ns1:item>
               <ns1:name>domain</ns1:name>
               <ns1:value>
                 <ns1:stringValue>domain_here</ns1:stringValue>
               </ns1:value>
            </ns1:item>
          </ns1:credentials>
          <ns1:password>password_here</ns1:password>
        </ns1:login>
      </soap-env:Body>
    </soap-env:Envelope>
    

    这是生成此 XML 的代码:

    domain = "domain_here"
    login = "login_here"
    password = "password_here"
    credentials = {"item": [{"name": "login", 'value': {"stringValue": login}},
                            {"name": "domain", 'value': {"stringValue": domain}}]}
    client.service.login(credentials=credentials, password=password)
    

    【讨论】:

      【解决方案2】:

      使用以下命令检查您的 wsdl: python -mzeep &lt;wsdl&gt;

      您将在输出中找到您的服务的详细信息:ns1:GetCount(FindCriteria:findCriteria)

      基于上述检查输出的示例代码:

      find_criteria_type = client.get_type('ns1:findCriteria')
      find_criteria = find_criteria_type()
      client.service.GetCount(FindCriteria=find_criteria)
      

      如果你的 XML 是这样的:

      <soap-env:Body>
        <ns1:GetCount>
           <ns1:findCriteria>
             <ns1:param1>val1</ns1:param1>
             <ns1:param2>val2</ns1:param1>
           </ns1:findCriteria>
        </ns1:GetCount>
      </soap-env:Body>
      

      创建obj时需要传递参数:

      find_criteria = find_criteria_type(param1=val1, param2=val2)
      

      【讨论】:

      • 听从了您的建议,但仍然没有结果。问题出在 ('ns1:findCriteria.This 不是有效类型。感谢您的努力。
      • 你能分享命令python -mzeep &lt;wsdl&gt; | grep GetCount的输出吗?
      • 这个命令对我没有帮助,因为我的 wsdl 落后于身份验证。我将 wsdl 放置在本地,但随后它也尝试包含身份验证后面的其他模式。
      • 将 WSDL 放在本地并执行 grep。此评论的输出太长。
      • 即使在grep?
      【解决方案3】:

      当我运行以下命令时:

      GET_TYPE_PutPropDataMappings=CPRManager_url.get_type('ns1:PropDataMapperSetup')
      print(GET_TYPE_PutPropDataMappings)
      SET_Values_PutPropDataMappings=GET_TYPE_PutPropDataMappings(ActingUserId='388153')
      print(SET_Values_PutPropDataMappings)
      

      结果是:-

      PropDataMapperSetup({http://www.ABCd.com/CPR/Manager}PropDataMapperSetup(ActingUserId: xsd:int, Mappings: {http://www.ABCd.com/CPR/Manager}ArrayOfPropDataMapping, UserCompanyId: xsd:int))
      {
          'ActingUserId': '388153',
          'Mappings': None,
          'UserCompanyId': None
      }
      

      这看起来是正确的。但是,当我运行请求以获取值时,使用以下命令:

      Response_PutPropDataMappings=CPRManager_url.service.PutPropDataMappings(propDataMapperSetup=SET_Values_PutPropDataMappings)
      print(Response_PutPropDataMappings)
      

      我明白了:-

      {
          'ErrorCodes': None,
          'ErrorMessages': {
              'string': [
                  'Invalid acting userid :0'
              ]
          },
          'WarningMessages': None,
          'WasSuccessful': None
      }
      

      这与传递相同:-

      Response_PutPropDataMappings=CPRManager_url.service.PutPropDataMappings(propDataMapperSetup)
      print(Response_PutPropDataMappings)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多