XML属性中的sud和名称空间

2024-05-13 05:49:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在让SUDS很好地使用netsuitesoapi时遇到了一些困难。我已经尝试过SoapUI来发送netsuitexml,它适用于我想要的调用,但是我无法让它与Python一起工作。以下是工作的XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:urn="urn:messages_2015_1.platform.webservices.netsuite.com" 
xmlns:urn1="urn:core_2015_1.platform.webservices.netsuite.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
   <soapenv:Header>
      <urn:preferences>
      </urn:preferences>
      <urn:partnerInfo>
      </urn:partnerInfo>
      <urn:applicationInfo>

      </urn:applicationInfo>
      <urn:passport>
         <urn1:email>*****</urn1:email>
         <urn1:password>*****</urn1:password>
         <urn1:account>*****</urn1:account>
         <!--Optional:-->
         <urn1:role internalId=*****>
         </urn1:role>
      </urn:passport>
   </soapenv:Header>
   <soapenv:Body>
      <urn:get>
        <urn1:baseRef internalId="2026" type="customer" xsi:type="urn1:RecordRef"/>
      </urn:get>
   </soapenv:Body>
</soapenv:Envelope>

在Python中,我生成了一个RecordRef对象并填充了它,但它不关心我的输入:

^{pr2}$

当我看一看正在发送的XML时,我注意到xsi:类型是正在设置,但类型未设置。在

如果我设置客户(真的xsi:客户)对于在SoapUI中工作的相同值,我得到一个关于未设置customer的错误:

(ReadResponse){
   status = 
      (Status){
         _isSuccess = False
         statusDetail[] = 
            (StatusDetail){
               _type = "ERROR"
               code = "RCRD_TYPE_REQD"
               message = "The record type is required."
            },
      }
 }
>>> recordRef
(RecordRef){
   name = None
   _internalId = 2026
   _externalId = ""
   _type = "ns1:RecordRef"
 }

我已经有五年没有做过任何关于SOAP的事情了,以前也从来没有用过Python。如有任何建议,我们将不胜感激。在


Tags: orgwebhttptypexmlenvelopeplatformxmlns
1条回答
网友
1楼 · 发布于 2024-05-13 05:49:20

弄明白了,我需要编写一个插件来调用marshaled()方法:

class FixXML(suds.plugin.MessagePlugin):
        def __init__(self):
                pass

        def marshalled(self, context):
                #the attribute type is really xsi:type, which we need to set but is being handled wrong
                #get the Netsuite data type
                wsdlDataType = context.envelope.getChild('Body').getChild('get').getChild('baseRef').getAttribute('type').getValue()
                #now rewrite xsi:type so its something namespacey
                context.envelope.getChild('Body').getChild('get').getChild('baseRef').getAttribute('type').setValue('ns0:RecordRef')
                #next we need to add a type attribute without a namespace that contains the data type from the wsdl
                typeAttr = suds.sax.attribute.Attribute('type')
                typeAttr.setValue(wsdlDataType)
                context.envelope.getChild('Body').getChild('get').getChild('baseRef').append(typeAttr)

相关问题 更多 >