【问题标题】:How to specify request parameters with "ids" in spyne?如何在 spyne 中使用“ids”指定请求参数?
【发布时间】:2019-08-23 13:13:44
【问题描述】:

我的任务是为我们的一个合作伙伴创建一个 SOAP 服务。合作伙伴向我提供了一个 WSDL 作为规范,我应该在我们这边实施。我已经取得了相当大的进步,但现在我碰壁了。

我怎样才能实现这个(<ns:Criteria id="?"> 行是问题)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://...">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:SearchRequest>
         <!--1 or more repetitions:-->
         <ns:Criteria id="?">
            <!--Optional:-->
            <ns:LocationIds>?</ns:LocationIds>
            <ns:Type>?</ns:Type>
         </ns:Criteria>
         <ns:Channel>?</ns:Channel>
         <!--You may enter ANY elements at this point-->
      </ns:SearchRequest>
   </soapenv:Body>
</soapenv:Envelope>

我提供服务的 Python 代码目前如下所示:

class CriteriaModel(ComplexModel):
    id = String
    LocationIds = String
    Type = String

class SomeService(Service):

    @rpc(
        Array(CriteriaModel, wrapped=False),
        _returns=Container,
        _in_message_name='SearchRequest',
        _out_message_name='SearchResponse'
    )
    def Search(ctx, criterias):
        # pass
        # TODO: implement the logic

但通过这种方法,我得到了这个结果:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ava="http://..." xmlns:book="...">
   <soapenv:Header/>
   <soapenv:Body>
      <ava:SearchRequest>
         <!--Zero or more repetitions:-->
         <ava:Criteria>
            <!--Optional:-->
            <book:id>?</book:id>
            <book:LocationIds>?</book:LocationIds>
            <book:Type>?</book:Type>
         </ava:Criteria>
      </ava:SearchRequest>
   </soapenv:Body>
</soapenv:Envelope>

这是我必须在这里实现的自定义模型吗?

非常感谢您!

【问题讨论】:

    标签: python-3.x soap spyne


    【解决方案1】:

    我能够解决这个问题。那个“id”的东西叫做属性。

    我换了

    class CriteriaModel(ComplexModel):
        id = String
        LocationIds = String
        Type = String
    

    与:

    class CriteriaModel(ComplexModel):
        id = XmlAttribute(String)
        LocationIds = String
        Type = String
    

    并将其呈现为:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ava="http://..." xmlns:book="...">
       <soapenv:Header/>
       <soapenv:Body>
          <ava:SearchRequest>
             <!--Zero or more repetitions:-->
             <ava:Criteria id="?">
                <!--Optional:-->
                <book:LocationIds>?</book:LocationIds>
                <book:Type>?</book:Type>
             </ava:Criteria>
          </ava:SearchRequest>
       </soapenv:Body>
    </soapenv:Envelope>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      相关资源
      最近更新 更多