如何使用 Python Suds 客户端添加 XML 元素类型

Posted

技术标签:

【中文标题】如何使用 Python Suds 客户端添加 XML 元素类型【英文标题】:How to Add XML Element Types using Python Suds Client 【发布时间】:2019-05-20 09:40:01 【问题描述】:

我一直无法弄清楚如何使用 Suds 工厂添加元素类型。我的代码如下:

from xml.etree import cElementTree as ElementTree
from suds.client import Client
from suds.wsse import *
from suds.sax.text import Raw
import logging

logging.basicConfig(level=logging.INFO)

logging.getLogger('suds.client').setLevel(logging.DEBUG)

username = 'username@tenant'
password = 'password'

url = 'https://wd2-impl- 
services1.workday.com/ccx/service/[tenant]/Revenue_Management/v31.1?wsdl'

client = Client(url, username = username, password = password, faults = False)

CustomerObjectType = client.factory.create('ns0:CustomerObjectType')

CustomerObjectType.ID = 'CUS3466'

FinancialsBusinessProcess = client.factory.create('ns0:Financials_Business_Process_ParametersType')

FinancialsBusinessProcess.Auto_Complete = 'true'

CustomerData = client.factory.create('ns0:Customer_WWS_DataType')

CustomerData.Customer_Name = 'Test'
CustomerData.Worktag_Only = 'false'
CustomerData.Submit = 'true'

CustomerData.Payment_Terms_Reference.ID = ['NET30', 'Customer_ID']

CustomerData.Default_Payment_Type_Reference.ID = ['PTY_CHECK', 'Payment_Terms_ID']

CustomerData.Included_Children_Reference = ['CUS3029', 'Customer_ID']

CustomerData.Business_Entity_Data.Business_Entity_Name = 'Test'

security = Security()

token = UsernameToken(username, password)

security.tokens.append(token)

client.set_options(wsse=security)

client.service.Submit_Customer('true', CustomerObjectType, FinancialsBusinessProcess, CustomerData)

我收到的错误是:

ERROR:suds.client:<suds.sax.document.Document instance at 0x10eb0e488>

输出:

DEBUG:suds.client:headers = 'SOAPAction': '""', 'Content-Type': 'text/xml; charset=utf-8'
DEBUG:suds.client:HTTP failed - 500 - Internal Server Error:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    <SOAP-ENV:Fault xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wd="urn:com.workday/bsvc">
  <faultcode>SOAP-ENV:Client.validationError</faultcode>
  <faultstring>Validation error occurred. Element 'ID' is missing attribute 'type'</faultstring>
  <detail>
    <wd:Validation_Fault>
      <wd:Validation_Error>
        <wd:Message>Element 'ID' is missing attribute 'type'</wd:Message>
        <wd:Detail_Message>Element 'ID' is missing attribute 'type'</wd:Detail_Message>
        <wd:Xpath>/ns0:Submit_Customer_Request[1]/ns0:Customer_Data[1]/ns0:Payment_Terms_Reference[1]/ns0:ID[2]</wd:Xpath>
      </wd:Validation_Error>
    </wd:Validation_Fault>
  </detail>
</SOAP-ENV:Fault>

suds 工厂为“Payment_Terms_Reference”元素返回的格式如下:

   Payment_Terms_Reference = 
  (Payment_TermsObjectType)
     ID[] = 
        "NET30",
        "Customer_ID",
     _Descriptor = ""
  

我感到困惑的是它看起来像是在使用索引

Payment_Terms_Reference[1]/ns0:ID[2] 

要查找元素类型,但是当我将更多数据添加到复杂对象类型的列表中时,它并没有解决问题。

我运行此代码时为 Payment_Terms_Reference 生成的当前 XML 代码如下:

<ns0:Payment_Terms_Reference>
   <ns0:ID>PTY_CHECK</ns0:ID>
   <ns0:ID>Payment_Terms_ID</ns0:ID>
</ns0:Payment_Terms_Reference>

但我认为我需要将其格式化为:

<ns0:Payment_Terms_Reference>
    <ns0:ID ns0:type="Payment_Terms_ID">ID</ns0:ID>
</ns0:Payment_Terms_Reference>

如果有人知道什么可以帮助我解决这个问题,那将不胜感激。

谢谢!

【问题讨论】:

【参考方案1】:

您需要为 Payment_TermsObjectType 创建一个工厂

paymenttype = client.factory.create('ns0:Payment_TermsObjectIDType')
paymenttype._type = "PTY_CHECK"
paymenttype.value = "Payment_Type_ID"

CustomerData.Payment_Terms_Reference.ID = paymenttype

【讨论】:

以上是关于如何使用 Python Suds 客户端添加 XML 元素类型的主要内容,如果未能解决你的问题,请参考以下文章

Python如何使用suds MessagePlugin和lxml仅解码xml中的特定部分

如何使用python完成对WebService服务的调用?suds-py3插件安利一下!

使用 suds 在 Python 中使用 OAuth2 进行 Google Adwords API 身份验证

python 将Suds对象转换为Python字典

第七篇:suds.TypeNotFound: Type not found: '(string, http://schemas.xmlsoap.org/soap/encoding/, )

Soap 请求在 Soapui 中运行良好,但在 python 中运行良好 - ZEEP - SUDS