DHL 肥皂请求

DHL SOAP Request

我正在尝试使用 DHL 的 API 添加货件详细信息并在 return 中获取标签。我正在使用带有 SoapUI 的肥皂盒并且能够发出请求。现在我想在 Python 中执行此操作。我不太确定这些步骤。我偶然发现了这个:DHL Soap Request Python

有人可以帮我把它送到 运行 吗?身份验证有效,但我不知道如何构建 soap 请求。

第一次试用是使用 SoapUI 软件做一些简单的请求或测试所有内容。使用我的凭据,我能够发送请求并在 return.

中获得标签

作为端点,使用了以下 URL,取自文档:https://cig.dhl.de/services/sandbox/soap

XML 看起来像这样:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cis="http://dhl.de/webservice/cisbase" xmlns:ns="http://dhl.de/webservices/businesscustomershipping/3.0"> 
   <soapenv:Header> 
      <cis:Authentification> 
         <cis:user>2222222222_01</cis:user> 
         <cis:signature>pass</cis:signature> 
      </cis:Authentification> 
   </soapenv:Header> 
   <soapenv:Body> 
      <ns:CreateShipmentOrderRequest> 
         <ns:Version> 
            <majorRelease>3</majorRelease> 
            <minorRelease>1</minorRelease> 
         </ns:Version> 
         <ShipmentOrder> 
            <sequenceNumber></sequenceNumber> 
            <Shipment> 
               <ShipmentDetails> 
                  <product>V62WP</product> 
                  <cis:accountNumber>${#Project#testAccountNumberV62WP}</cis:accountNumber> 
                  <customerReference>123456</customerReference> 
                  <shipmentDate>2021-09-03</shipmentDate> 
                  <costCentre></costCentre> 
                  <ShipmentItem> 
                     <weightInKG>1</weightInKG> 
                     <lengthInCM>25</lengthInCM> 
                     <widthInCM>15</widthInCM> 
                     <heightInCM>1</heightInCM> 
                  </ShipmentItem> 
                  <Service> 
                  </Service> 
                  <Notification> 
                     <recipientEmailAddress>empfaenger@test.de</recipientEmailAddress> 
                  </Notification> 
               </ShipmentDetails> 
               <Shipper> 
                  <Name> 
                     <cis:name1>Absender Zeile 1</cis:name1> 
                     <cis:name2>Absender Zeile 2</cis:name2> 
                     <cis:name3>Absender Zeile 3</cis:name3> 
                  </Name> 
                  <Address> 
                     <cis:streetName>Vegesacker Heerstr.</cis:streetName> 
                     <cis:streetNumber>111</cis:streetNumber> 
                     <cis:zip>28757</cis:zip> 
                     <cis:city>Bremen</cis:city> 
                     <cis:Origin> 
                        <cis:country></cis:country> 
                        <cis:countryISOCode>DE</cis:countryISOCode> 
                     </cis:Origin> 
                  </Address> 
                  <Communication> 
                     <!--Optional:--> 
                     <cis:phone>+49421987654321</cis:phone> 
                     <cis:email>absender@test.de</cis:email> 
                     <!--Optional:--> 
                     <cis:contactPerson>Kontaktperson Absender</cis:contactPerson> 
                  </Communication> 
               </Shipper> 
               <Receiver> 
                  <cis:name1>Name</cis:name1> 
                  <Address> 
                     <cis:name2>Empfänger Zeile 2</cis:name2> 
                     <cis:name3>Empfänger Zeile 3</cis:name3> 
                     <cis:streetName>Street</cis:streetName> 
                     <cis:streetNumber>Number</cis:streetNumber> 
                     <cis:zip>zipCode</cis:zip> 
                     <cis:city>City</cis:city> 
                     <cis:Origin> 
                        <cis:country></cis:country> 
                        <cis:countryISOCode>DE</cis:countryISOCode> 
                     </cis:Origin> 
                  </Address> 
                  <Communication> 
                     <cis:phone>+49421123456789</cis:phone> 
                     <cis:email>empfaenger@test.de</cis:email> 
                     <cis:contactPerson>Kontaktperson Empfänger</cis:contactPerson> 
                  </Communication> 
               </Receiver> 
            </Shipment> 
            <PrintOnlyIfCodeable active="1"/> 
         </ShipmentOrder> 
         <labelResponseType>URL</labelResponseType> 
         <groupProfileName></groupProfileName> 
         <labelFormat></labelFormat> 
         <labelFormatRetoure></labelFormatRetoure> 
         <combinedPrinting>0</combinedPrinting> 
      </ns:CreateShipmentOrderRequest> 
   </soapenv:Body> 
</soapenv:Envelope>

我的 Python 代码如下所示:

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

user = "my_user_name"
password = "my_password"
USER = "2222222222_01" #from doc
PASSWORD = "pass" #from doc
EKP = "2222222222"

wsdl = "./geschaeftskundenversand-api-3.1.8.wsdl" #downloaded and stored local

session = Session()

# Authenticate  with gateway
session.auth = HTTPBasicAuth(user, password)
client = Client(wsdl, 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.createShipmentOrder(_soapheaders=[header_value])

因此我得到了回溯:缺少元素版本

在我看来,身份验证工作正常,现在我需要插入 XML 的正文部分。 据我了解,下一步应该是:

client.service.createShipmentOrder

在这里我可以传递一本字典。 createShipmentOrder 是纪录片中所述的请求之一。它返回状态为 OK 的 XML、装运编号和用于打印的标签 URL。 在我看来,现在我必须传递 XML 中所述的地址等。 在 SoupUI 中,请求被发送到端点 URL https://cig.dhl.de/services/sandbox/soap。但是在我的 Python 代码中没有说明端点。我需要它还是我存储在本地的 wsdl 文件的一部分?

所以没有人能帮助我,我自己做了:)

我需要几个小时才能理解 API 但现在很清楚...

您可以使用我问题中的大部分代码。 您必须生成包含所有需要数据的嵌套字典。例如:

dict = {'Version':{'majorRelease' : '3', 'minorRelease' : '1'},
                    'ShipmentOrder':
                        {'sequenceNumber':'','Shipment':
                            {'ShipmentDetails':
                                {'product':product, 'accountNumber':accountNumber, 'customerReference':customerReference,
                                'shipmentDate':shipmentDate,
                                'ShipmentItem':{'weightInKG':'0.9', 'lengthInCM':'35', 'widthInCM':'24', 'heightInCM':'5'}
                                },
                            'Shipper':{
                                'Name':{'name1':'your name'},
                                'Address':{'streetName':'your street','streetNumber':'1234', 'zip':'11111', city':'New York',
                                    'Origin':{'country':'', 'countryISOCode':'DE'}}
                            },
                            'Receiver':{
                            'name1':receiver_name,
                                'Address':{'streetName':receiver_street,'streetNumber':receiver_streetnumber, 'zip':receiver_zip, 'city':receiver_city,
                                'Origin':{'country':'', 'countryISOCode':'DE'}}
                            }
                            }
                        },
                        'labelFormat':label_groesse
            }

将 wsdl 文件保存到一个文件夹中,然后 link 将您的脚本保存到其中:

wsdl = "./geschaeftskundenversand-api-3.1.8.wsdl"

您需要 header 用于网关的请求和身份验证。如问题中所示,身份验证是 运行 通过一个简单的请求。

您现在只需要 soap 请求:

response = client.service.createShipmentOrder(_soapheaders=[header_value], **dict)

你输入 header 和字典的地方。如果你 运行 所有东西,那么你会收到一个回复​​,上面写着 'OK' 并且有发货编号和你标签上的 url。

就是这样。

我在 Raspberry Pi4 上 运行 将脚本与一些流线型部分结合起来,让标签打印机打印所有内容。这是我打印标签的代码:

import subprocess
conn = cups.Connection() #Connection to CUPS
printers = conn.getPrinters() #Get all listed printers
use_printer = printers['QL-1110NWB']["device-uri"] #choose the one label printer
 
file = requests.get(label_url) #Save the label from url into pdf document locally

with open('label.pdf', 'wb') as pdf:
   pdf.write(file.content)

               
subprocess.run(['lp', '-d', 'QL-1110NWB', '-o fit-to-page', 'label.pdf']) # print the saved label

如果您通过 sandbox 出于测试目的进行此操作,请查看此内容。

即使您没有使用沙盒,也请检查 wsdl 文件中的端点 url 是否正确。

我也遇到了同样的问题。我还阅读了所有可用的答案,但问题有点傻。也许您发布到错误的 soap 端点。这对我来说是真实的。

它甚至可以使用以下最小代码 -

header = xsd.Element(
                    'Authentification',
                     xsd.ComplexType([
                        xsd.Element('user', xsd.String()),
                        xsd.Element('signature', xsd.String())]))

header_value = header(user="2222222222_01", signature="pass")

client.set_default_soapheaders([header_value])

# A test case
client.service.getVersion(majorRelease='3')