Reading SOAP response from file using zeep

100 Views Asked by At

I am building an integration with a SOAP service using Python and zeep. Each received SOAP response requires some post-processing on my side. For debugging purposes, I am saving the raw XML requests/responses to files.

I am looking for a way to parse the saved XML response back to the same object that would have been produced if I had made the request to the SOAP service.

Sample code:

import lxml
from zeep import Client
from zeep.plugins import HistoryPlugin

# Create history-enabled client from WSDL.
wsdl = 'https://soap.endpoint.com/service?WSDL'
history = zeep.plugins.HistoryPlugin(maxlen=1)
client = Client(wsdl=wsdl, plugins=[history])

# Send a request.
response = client.service.M12000(...)

# Get raw response xml.
envelope = history.last_received['envelope']
response_xml = lxml.etree.tostring(envelope, encoding='unicode', pretty_print=True)

# Print raw/parsed response.
print(response_xml)
print(type(response), response)

This produces:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <M12000Response xmlns="https://soap.endpoint.com/">
      <TransactionID>123456</TransactionID>
      <ExitCode>0</ExitCode>
    </M12000Response>
  </soap:Body>
</soap:Envelope>
<class 'zeep.objects.M12000Response'> {
    'TransactionID': 123456,
    'ExitCode': '0'
}

Assuming that the raw XML response is saved in response.xml, I want to be able to do something like:

def response_from_file(client, filename):
    ...
    return ...

response = response_from_file(client, 'response.xml')
print(type(response), response)

And get the same object as if I had made the request to the service:

<class 'zeep.objects.M12000Response'> {
    'TransactionID': 123456,
    'ExitCode': '0'
}

The client and client.wsdl objects do not seem to offer a direct way to do this. So any clues/guidance are welcome.

0

There are 0 best solutions below