如何使用Python Suds Client添加XML元素类型

时间:2018-12-19 20:51:13

标签: python xml python-3.x suds workday-api

我一直很难弄清楚如何使用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>

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

谢谢!

0 个答案:

没有答案