1

在我的一个应用程序中,我在 GAE python 上使用SUDS WSDL(SOAP) Web 服务。

我的Py 代码

url = 'http://203.215.51.43/Gateway/Execute?WSDL'
client = Client(url)
print client

所以它在服务器控制台打印

Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913
Service ( GatewayService ) tns="urn:fks:jcaps:gateway"
   Prefixes (2)
      ns0 = "http://fks.com/gtwy"
      ns1 = "urn:fks:jcaps:gateway:JavaException"
   Ports (1):
      (Execute)
         Methods (1):
            redeem(xs:string SessionId, xs:string TerminalId, xs:string StoreCod
e, xs:string CashMemoNo, xs:string CashMemoDate, xs:double InvoiceAmount, xs:str
ing TimeStamp, xs:string CashierId, xs:string MerchantId, ns0:SPCReqDtls SPCReqD
tls, ns0:VoucherReqDtls[] VoucherReqDtls, ns0:CardReqDtls CardReqDtls, )
         Types (1):
            ns1:JavaExceptionType

实际上可以在兑换方法中传递字符串、双精度类型的值,但不能传递数组/列表类型,

result = client.service.redeem(SessionId=result['SessionId'], StoreCode='4739', TerminalId='T1081', TimeStamp='01-01-2011 01:01:00.000')

但我无法在这里传递VoucherReqDtls数组

这个 VoucherReqDtls的WSDL 的 XML代码如下,

    <!--Zero or more repetitions:-->
             <gtwy:VoucherReqDtls>
                <gtwy:VoucherType>GV</gtwy:VoucherType>
                <!--1 or more repetitions:-->
                <gtwy:VoucherReq>
                   <gtwy:VoucherNo>344234242</gtwy:VoucherNo>
                </gtwy:VoucherReq>
                <gtwy:VoucherReq>
                   <gtwy:VoucherNo>675685858</gtwy:VoucherNo>
                </gtwy:VoucherReq>
             </gtwy:VoucherReqDtls>

那么我如何在兑换方法中传递VoucherReqDtls数组/列表,这里 VoucherType 是GV并且VoucherNo的计数可能不止一个。

4

1 回答 1

5

我认为问题在于,如果您直观地检查 WSDL,redeem 方法期望GatwayReq可以使用以下代码创建复杂类型:

>>> it = client.factory.create('ns0:GatewayReq')
>>> it
(GatewayReq){
   SessionId = None
   TerminalId = None
   StoreCode = None
   CashMemoNo = None
   CashMemoDate = None
   InvoiceAmount = None
   TimeStamp = None
   CashierId = None
   MerchantId = None
   SPCReqDtls = 
      (SPCReqDtls){
         SPCNo = None
         SPCAmt = None
      }
   VoucherReqDtls[] = <empty>
   CardReqDtls = 
      (CardReqDtls){
         CardType = None
         CardReq = 
            (CardReq){
               CardNo = None
               TransAmt = None
            }
      }
 }

您可以像添加任何对象属性一样将元素添加到 GatewayReq 对象,即

>>> it.SessionId = 'blahablhabalh'
>>> it
(GatewayReq){
   SessionId = "blahablhabalh"
   # the rest removed for readability

然后,您将像这样调用兑换方法:

>>>> client.service.redeem(it)

编辑

您可以使用以下语法创建所需的类型:

entry1 = client.factory.create('ns0:GatewayReq.ns0:VoucherReqDtls')
>>> entry1
(VoucherReqDtls){
   VoucherType = None
   VoucherReq[] = <empty>
 }

这使用机制来访问此处指定的非顶级类型suds docs - FACTORY

然后您可以将其添加到主请求对象,如下所示:

>>> entry1.VoucherType = 'GV'
>>> entry1.VoucherReq.append([12,34,56])
>>> entry1
(VoucherReqDtls){
   VoucherType = "GV"
   VoucherReq[] = 

      12,
      34,
      56,
 }
>>> entry1.VoucherReq.append(23)
>>> entry1
    (VoucherReqDtls){
       VoucherType = "GV"
       VoucherReq[] = 

      12,
      34,
      56,
      23,
 }

>>> it.VoucherReqDtls.append(entry1)
>>> it
(GatewayReq){
   SessionId = None
   TerminalId = None
   StoreCode = None
   CashMemoNo = None
   CashMemoDate = None
   InvoiceAmount = None
   TimeStamp = None
   CashierId = None
   MerchantId = None
   SPCReqDtls = 
      (SPCReqDtls){
         SPCNo = None
         SPCAmt = None
      }
   VoucherReqDtls[] = 
      (VoucherReqDtls){
         VoucherType = "GV"
         VoucherReq[] = 

            12,
            34,
            56,
            23,
  },
   CardReqDtls = 
      (CardReqDtls){
         CardType = None
         CardReq = 
            (CardReq){
               CardNo = None
               TransAmt = None
            }
      }
  }

这会创建如下的 xml,其中包含所需的语法:

<SOAP-ENV:Header/>
   <ns0:Body>
      <ns1:GatewayReq>
         <ns1:SessionId>
            <ns1:SessionId/>
            <ns1:TerminalId/>
            <ns1:StoreCode/>
            <ns1:CashMemoNo/>
            <ns1:CashMemoDate/>
            <ns1:TimeStamp/>
            <ns1:CashierId/>
            <ns1:VoucherReqDtls>
               <ns1:VoucherType>GV</ns1:VoucherType>
               <ns1:VoucherReq>12</ns1:VoucherReq>
               <ns1:VoucherReq>34</ns1:VoucherReq>
               <ns1:VoucherReq>56</ns1:VoucherReq>
               <ns1:VoucherReq>23</ns1:VoucherReq>
            </ns1:VoucherReqDtls>
            <ns1:VoucherReqDtls>
               <ns1:VoucherType>GV</ns1:VoucherType>
               <ns1:VoucherReq>12</ns1:VoucherReq>
               <ns1:VoucherReq>34</ns1:VoucherReq>
               <ns1:VoucherReq>56</ns1:VoucherReq>
               <ns1:VoucherReq>23</ns1:VoucherReq>
            </ns1:VoucherReqDtls>
         </ns1:SessionId>
         <ns1:TerminalId/>
         <ns1:StoreCode/>
         <ns1:CashMemoNo/>
         <ns1:CashMemoDate/>
         <ns1:TimeStamp/>
         <ns1:CashierId/>
      </ns1:GatewayReq>
   </ns0:Body>
</SOAP-ENV:Envelope>

或者至少我认为它应该是这样工作的

希望能帮助到你

于 2012-08-08T14:31:34.113 回答