在suds SOAP请求

时间:2016-06-22 12:24:19

标签: python soap suds

一旦调用soap WS,我的suds请求会产生无效消息 有两件事要做,我花了几个小时没有成功:

  • 为复杂的obejcts(securityObject
  • 之一添加命名空间和前缀
  • 将属性添加到另一个复杂对象(Document

我的客户:

Suds ( https://fedorahosted.org/suds/ )  version: 0.6

Service ( MyService ) tns="http://path/to/tns"
  Prefixes (4)
      ns0 = "http://path/to/v3/"
      ns1 = "http://path/to/tns"
      ns2 = "http://www.path/to/file.001.xsd"
      ns3 = "http://www.path/to/file.002.xsd"
   Ports (1):
      (MyServiceSOAP)
         Methods (1):
            processAction(ns2:Document Document, ns0:securityObject securityObject PriorityType priority)
         Types (233):
            ns0:securityObject
            PriorityType
            ns3:Document
            ns2:Document
            ns3:AccountIdentification4Choice
            …

第一个方法参数:

由于ns2:Document Document非常庞大且复杂且我已经拥有有效的XML,我将以下列形式将其作为processAction参数传递:

from suds.sax.text import Raw
_doc.open('file.xml').read()
doc = Raw(_doc) # ready to pass 

这就是这个doc的样子: <Document xmlns="http://www.path/to/file.001.xsd">...</Document>

第二个方法参数:

是安全对象:

s_o = client.factory.create('{http://path/to/v3/}securityObject')
s_o.name = token_data.Name
s_o.user = token_data.Tenant
s_o.timestamp = token_data.Time.isoformat()
s_o.pass = token_data.Passwd

让我们发送:

try:
    process_data = client.service.processAction(s_o, doc, prio.NORMAL)
except suds.WebFault as e:
    print e

传出(无效)讯息:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/"    # in client its "http://path/to/v3/"
                   xmlns:ns1="http://path/to/tns"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
                   # missing xmlns:v3="http://path/to/v3/" here!
    <SOAP-ENV:Header/>
    <ns0:Body>
        <ns1:payment>
            <securityObject>    # wrong! shall be <v3:securityObject> 
                <user>1</user>
                <name>MyName</name>
                <timestamp>2016-06-22T11:40:58.115000+02:00</timestamp>
                <pass>Password</pass>
            </securityObject>
            <Document>                                             # wrong, missing attribute here, xmlns= 
                <Document xmlns="http://www.path/to/file.001.xsd"> # comes from orginal xml, can be trimmed

成功的消息需要两个修复:

  1. 添加一个namspace和前缀,如下所示:

    xmlns:v3="http://path/to/v3/"  # this to be added in Envelope
    <v3:securityObject>
        ...
    </v3:securityObject>
    
  2. ns2:Document对象添加属性,所以它看起来像:
    <Document xmlns="http://www.path/to/file.001.xsd">
    (这将需要从我创建的原始消息中剥离标记。我可以这样做但看起来不太优雅。) 理想情况是用一种方法将我的XML内容映射到ns2:Document对象。

  3. 以下是suds处理这些对象的方法:

    (Content){
       tag = "Document"
       value = "<Document xmlns="http://www.path/to/file.001.xsd">
                    <CfInitn>
                        <GrpHdr>
                        ...
    
    (Content){
       tag = "securityObject"
       value = 
          (securityObject){
             user= 1
             name = "MyName"
             timestamp = "2016-06-22T12:51:29.820000+02:00"
             pass= "Password"
          }
    

    任何帮助太多了,谢谢!

0 个答案:

没有答案