1

我正在尝试使用 DHL 的 API 添加货件详细信息并获得标签作为回报。我正在使用带有 SoapUI 的肥皂盒并能够发出请求。现在我想在 Python 中做到这一点。我不太确定这些步骤。我偶然发现了这个:DHL Soap Request Python

有人可以帮我让它运行吗?身份验证正在工作,但我不知道如何构建肥皂请求。

第一次尝试是使用 SoapUI 软件来做一些简单的请求或测试一切。凭借我的凭据,我能够发送请求并获得标签作为回报。

作为端点,使用了以下 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 是纪录片中所述的请求之一。它返回一个状态正常的 XML、一个货运编号和一个用于打印的标签的 URL。在我看来,现在我必须传递 XML 中所述的地址等。在 SoupUI 中,请求被发送到端点 URL https://cig.dhl.de/services/sandbox/soap。但是在我的 Python 代码中没有说明端点。我需要它还是它是我本地存储的 wsdl 文件的一部分?

4

1 回答 1

2

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

我需要几个小时来理解 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 文件保存到一个文件夹中,并将您的脚本链接到它,如下所示:

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

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

你现在需要的只是肥皂请求:

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

你把标题和字典放在哪里。如果您运行所有程序,那么您将收到一条回复,上面写着“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
于 2021-09-29T20:31:40.677 回答