簡體   English   中英

Python - 泡沫。 如何編輯 SOAP-ENV:Envelope 的命名空間

[英]Python - Suds. How to edit the namespaces of SOAP-ENV:Envelope

我正在創建一個請求,以在 Django 中使用 SUDS (4.0) 來使用 Web 服務。 然而,Suds 沒有放置正確的命名空間。

我得到的肥皂信封:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope 
    xmlns:ns3="http://www.w3.org/2001/XMLSchema" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:ns1="http://factura360.com/invoice_webservice" 
    xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Header>
        <ns2:LoginObject>
            <soapusername>USER</soapusername>
            <soappassword>PASSWORD</soappassword>
        </ns2:LoginObject>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:generate_invoice>
            <document xsi:type="xsd:string">
                <?xml version="1.0" encoding="UTF-8"?><cfdi:Comprobante></cfdi:Comprobante>
            </document>
            <documenttype xsi:type="xsd:int">0</documenttype>
            <filetype xsi:type="xsd:int">1</filetype>
            <cer xsi:nil="true"></cer><key xsi:nil="true">
            </key><password xsi:type="xsd:string">PASSWORD</password>
        </ns1:generate_invoice>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SOAP 信封應該是什么:

<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://factura360.com/invoice_webservice" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ns2="namespace" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Header>
           <ns2:LoginObject> 
               <soapusername>USER</soapusername> 
               <soappassword>PASSWORD</soappassword> 
           </ns2:LoginObject>
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body>
        <ns1:generate_invoice> 
            <document xsi:type="xsd:string"> 
                <?xml version="1.0" encoding="UTF-8"?><cfdi:Comprobante>    </cfdi:Comprobante>
           </document>
           <documenttype xsi:type="xsd:int">0</documenttype> 
           <filetype xsi:type="xsd:int">1</filetype>
           <cer xsi:nil="true"></cer> 
           <key xsi:nil="true"></key>
           <password xsi:type="xsd:string">PASSWORD</password> 
        </ns1:generate_invoice> 
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

因此,根據所需的 SOAP 信封,不應該有“xmlns:ns0”,沒有“xmlns:ns3”,應該有值為“http://www.w3.org/2001/XMLSchema”的 xmlns:xsd 和'xmlns:ns2' 的值應該是 'namespace'。

我編輯信封的實際功能是這樣的:

class CorrectNamespace(MessagePlugin):
    def marshalled(self, context):
        soap_env_parent = context.envelope
        soap_env_parent.set('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema')
        soap_env_parent.unset('xmlns:ns0')
        soap_env_parent.unset('xmlns:ns3')
        soap_env_parent.unset('xmlns:ns2')
        soap_env_parent.set('xmlns:ns2', 'namespace')

factura_client = Client(settings.FACTURA360_URL, plugins=[CorrectNamespace()])

#Setting the Login Object
lg_element = Element('ns2:LoginObject')
soapuser_element = Element('soapusername').setText(settings.FACTURA360_USER)
soappass_element = Element('soappassword').setText(settings.FACTURA360_PASS)
lg_element.append(soapuser_element)
lg_element.append(soappass_element)
factura_client.set_options(soapheaders=lg_element)

#The call to the webservice
invoice = factura_client.service.\
    generate_invoice(document, document_type, filetype,
                     cer, key, settings.FACTURA360_INVOICEPASSWORD)

不幸的是,我的插件函數添加了“xmlns:xsd”和“xmlns:ns2”,但並沒有刪除其他函數。

這是帶有插件功能的結果信封:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope 
    xmlns:ns3="http://www.w3.org/2001/XMLSchema" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:ns1="http://factura360.com/invoice_webservice" 
    xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:ns2="namespace">

如何刪除其他命名空間?

class CorrectNamespace(MessagePlugin):
    def marshalled(self, context):
        del context.envelope.nsprefixes['ns3']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM