Search code examples
pythonpython-3.xheaderwsdlzeep

how can I send header with each methods of wsdl in zeep?


I'm new to use zeep. and trying implement a wsdl:http://leon.leonardotravel.com/Leon.svc/wsdl

i have to send request like this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <Search xmlns="http://tempuri.org/">
      <Header>
        <AgentId>agentid</AgentId>
        <Password>password</Password>
        <Username>username</Username>
      </Header>
      <Search>
        <CheckIn>2017-06-10</CheckIn>
        <CheckOut>2017-06-12</CheckOut>
        <CityId>4339</CityId>
        <CurrencyId>2</CurrencyId>
        <HotelId>122079</HotelId>
        <PaxCountryId>2</PaxCountryId>
        <DefaultPhotoInclude>false</DefaultPhotoInclude>
        <GeoCoordinatesInclude>false</GeoCoordinatesInclude>
        <HotelAddressInclude>false</HotelAddressInclude>
        <HotelDescriptionInclude>false</HotelDescriptionInclude>
        <HotelNameInclude>true</HotelNameInclude>
        <MaxResponseTime>0</MaxResponseTime>
        <SearchRooms>
          <SearchRoom>
            <Adult>1</Adult>
            <Child1Age>0</Child1Age>
            <Child2Age>0</Child2Age>
            <Child3Age>0</Child3Age>
            <Quantity>1</Quantity>
          </SearchRoom>
        </SearchRooms>
      </Search>
    </Search>
  </s:Body>
</s:Envelope>

I should send header when i wanna to use each methods of this wsdl. can you help me?!


Solution

  • Since google lead me here, I'll post my findings.

    In order to send the same SOAP headers on every call a client makes, I did this, with some silly replacements for general consumption:

    from zeep import Client, xsd
    client = Client(
        wsdl_url,
        transport=transport,
    )
    
    header = xsd.ComplexType(
        xsd.Sequence([
            xsd.Element('SomeString', xsd.String()),
            xsd.Element('SomeBoolean', xsd.Boolean()),
        ])
    )
    
    headers = [ header(SomeString='Hello', SomeBoolean=True) ]
    client.set_default_soapheaders(headers)
    
    client.service.MakeTheMagic(Stuff=stuff)