LetItBeAndre
LetItBeAndre

Reputation: 151

Authentification on DHL-SOAP API with zeep

I just started working with one of the DHL-SOAP APIs and use zeep to run requests against the API. The API expects and Element Authentification like this:

...
<soapenv:Header>
    <cis:Authentification>
        <cis:user>USER</cis:user>
        <cis:signature>PASSWORD</cis:signature>
    </cis:Authentification>
</soapenv:Header>
...

I tried to pass the authentification as part of the _soapheaders as described in the zeep documentation, neigher the dict notation nor the xsd.Element notation seem to work.

from zeep import Client
from zeep import xsd

client = Client('<URL_TO_WSDL>')
auth_header = {'user': 'user', 'signature': 'signature'}
# dict approach
client.service.DHL_SERVICE(_soapheaders={'Authentification': auth_header})

# xsd approach
header = xsd.Element('Authentification',
    xsd.ComplexType([
        xsd.Element('user', xsd.String()),
        xsd.Element('signature', xsd.String())
    ])
)
header_values = header(user='user', signature='signature')
client.service.DHL_SERVICE(_soapheaders=[header_values])

I don't find helpful information in the DHL docs not in the zeep documentation.

Thank you in advance!

Regards

Upvotes: 3

Views: 1148

Answers (1)

LetItBeAndre
LetItBeAndre

Reputation: 151

Just in case someone ever encounters the same problems. It turned out that the client needs to authenticate at a gateway using HTTPBasicAuth. Additionally, the client had to be created using a Transport with a session in it, carrying the gateway authentication headers. What made the xsd API header approach work was the addition {http://test.python-zeep.org}. This setup made communication with the API working smoothly.

from requests import Session
from requests.auth import HTTPBasicAuth
from zeep import Client
from zeep.transports import Transport

session = Session()

# Authenticate  with gateway
session.auth = HTTPBasicAuth(user, password)
client = Client(WSDL_PATH, transport=Transport(session=session))

# Build Authentification header for API-Endpoint using zeep xsd
header = xsd.Element(
    '{http://test.python-zeep.org}Authentification',
    xsd.ComplexType([
        xsd.Element(
            '{http://test.python-zeep.org}user',
            xsd.String()),
        xsd.Element(
            '{http://test.python-zeep.org}signature',
            xsd.String()),
    ])
)
header_value = header(user=USER, signature=PASSWORD)
client.service.DHL_SERVICE(_soapheaders=[header_value])

Upvotes: 1

Related Questions