2013-07-29 64 views
0

我無法弄清楚如何使用這個WSDL interface。我對WSDL(和一般的SOAP)沒有經驗。麻煩消費XLedger的SOAP/WSDL接口

這一切都完全在我的頭上。我的情況如下。我有一個使用REST接口與後端進行通信的Web應用程序。後端需要與提到的WSDL接口進行通信,以向Web應用程序提供其請求的信息。

所以

[Client] <-- REST --> [Server] <-- SOAP --> [XLedger] 

我想我需要的是針對完整的SOAP新手的教程。現在有太多的差距,我不能從文章中推斷出我需要的東西。或者,也許一個有用的SO會員可以向我展示一些示例代碼以使我開始?

更具體地說,我對GetTimesheetEntriesData及其提供的屬性感興趣。我只希望能夠調用getters並將數據發送到Web應用程序(在智能手機上運行)。

我甚至不確定我在這裏問的是正確的問題,但我如何使用WSDL接口獲取用戶時間表數據?

[編輯]

這裏的認證接口:https://ws.xledger.net/WS/Common/Lib/Authentication.asmx?WSDL

回答

1

好吧,我想通了。我必須先使用suds

import httplib 
import urllib2 as u2 
from suds.transport.http import HttpTransport 


class HTTPSClientAuthHandler(u2.HTTPSHandler): 
    def __init__(self, key, cert): 
     u2.HTTPSHandler.__init__(self) 
     self.key = key 
     self.cert = cert 

    def https_open(self, req): 
     # Rather than pass in a reference to a connection class, we pass in 
     # a reference to a function which, for all intents and purposes, 
     # will behave as a constructor 
     return self.do_open(self.getConnection, req) 

    def getConnection(self, host, timeout=300): 
     return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert) 


class HTTPSClientCertTransport(HttpTransport): 
    def __init__(self, key, cert, *args, **kwargs): 
     HttpTransport.__init__(self, *args, **kwargs) 
     self.key = key 
     self.cert = cert 

    def u2open(self, u2request): 
     """ 
     Open a connection. 

     @param u2request: A urllib2 request. 
     @type u2request: urllib2.Request. 
     @return: The opened file-like urllib2 object. 
     @rtype: fp 
     """ 
     url = u2.build_opener(HTTPSClientAuthHandler(self.key, self.cert)) 
     if self.u2ver() < 2.6: 
      return url.open(u2request) 
     else: 
      return url.open(u2request, timeout=self.options.timeout) 
. 
. 
. 
def consume_soap(): 
    from suds.client import Client 
    from datetime import date 
    from calendar import monthrange 

    transport = HTTPSClientCertTransport('auth/key_no_passphrase.pem', 'auth/cert.pem') 
    client = Client(XLedgerInterface.WSDL_EXPORT_PATH, transport=transport) 
    year = date.today().year 
    month = date.today().month 
    first_date = str(date(year, month, 1)) 
    last_date = str(date(year, month, monthrange(year, month)[1])) 
    xml = client.service.GetTimesheetEntriesData(sUserName=XLedgerInterface.USER_ID, 
               sKey=XLedgerInterface.KEY, 
               sApplication=XLedgerInterface.APPLICATION_NAME, 
               iEntityCode=XLedgerInterface.ENTITY_CODE, 
               dDateFrom=first_date, 
               dDateTo=last_date, 
               sFreeText='', 
               sFilter='', 
               eOption="Open") 
    return self._get_as_json(xml)