How to describe element attributes with Spyne

丶灬走出姿态 提交于 2019-12-08 05:50:41

问题


I'm ok with Spyne's hello world examples, but when it comes to something more complex I faced with lack of documentation and advanced examples. In my case I have a service method which accepts body like this

<OTA_HotelAvailRQ xmlns="http://www.opentravel.org/OTA/2003/05" Version="1.0" TimeStamp="2005-08-01T09:30:47+02:00" EchoToken="fb57388d" AvailRatesOnly="true">
  <AvailRequestSegments>
    <AvailRequestSegment AvailReqType="Room">
      <HotelSearchCriteria>
        <Criterion>
          <HotelRef HotelCode="HOTEL1"/>
        </Criterion>
      </HotelSearchCriteria>
    </AvailRequestSegment>
  </AvailRequestSegments>
</OTA_HotelAvailRQ>

Can you help me to implement a service that accepts this kind of request?


回答1:


Off the top of my head:

class HotelReference(ComplexModel):
    __namespace__ = 'http://www.opentravel.org/OTA/2003/05'

    HotelCode = XmlAttribute(Unicode)

class Criterion(ComplexModel):
    __namespace__ = 'http://www.opentravel.org/OTA/2003/05'

    HotelRef = HotelReference

class AvailRequestSegment(ComplexModel):
    __namespace__ = 'http://www.opentravel.org/OTA/2003/05'

    AvailReqType = XmlAttribute(Unicode(values=["Room", "House", "Condo", "Castle"]))
    HotelSearchCriteria = Criterion.customize(max_occurs='unbounded')

class HotelAvailRQ(ComplexModel):
    __namespace__ = 'http://www.opentravel.org/OTA/2003/05'

    Version = XmlAttribute(Unicode)
    TimeStamp = XmlAttribute(DateTime)
    EchoToken = XmlAttribute(ByteArray)
    AvailRatesOnly XmlAttribute(Boolean)

    AvailRequestSegments = Array(AvailRequestSegment)


来源:https://stackoverflow.com/questions/19514045/how-to-describe-element-attributes-with-spyne

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!