Search code examples
pythonxmlsoapspyne

How to describe element attributes with Spyne


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?


Solution

  • 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)