2014-09-24 44 views
0

有沒有辦法讓發泡體返回SoapRequest(XML)而不發送它?創建SoapRequest而不用Suds/Python發送它們

這個想法是,我的程序的上層可以調用我的API與一個額外的布爾參數(模擬)。

If simulation == false then process the other params and send the request via suds 
If simulation == false then process the other params, create the XML using suds (or any other way) and return it to the caller without sending it to the host. 

我已經實現了一個MessagePlugin follwing https://fedorahosted.org/suds/wiki/Documentation#MessagePlugin,但我不能夠得到XML,停止請求和XML發回給調用者...

問候

回答

0

的解決方案,我實現的是:

class CustomTransportClass(HttpTransport): 
def __init__(self, *args, **kwargs): 
    HttpTransport.__init__(self, *args, **kwargs) 
    self.opener = MutualSSLHandler() # I use a special opener to enable a mutual SSL authentication 

def send(self,request): 
    print "===================== 1-* request is going ====================" 
    is_simulation = request.headers['simulation'] 
    if is_simulation == "true": 
     # don't actually send the SOAP request, just return its XML 
     print "This is a simulation :" 
     print request.message 
     return Reply(200, request.headers, request.message) 

    return HttpTransport.send(self,request) 


sim_transport = CustomTransportClass() 
client = Client(url, transport=sim_transport, 
      headers={'simulation': is_simulation}) 

感謝您的幫助,

1

泡沫用途默認情況下稱爲「運輸」類HttpAuthenticated。這是實際發送的地方。所以理論上你可以嘗試子類:

from suds.client import Client 
from suds.transport import Reply 
from suds.transport.https import HttpAuthenticated 

class HttpAuthenticatedWithSimulation(HttpAuthenticated): 

    def send(self, request): 
     is_simulation = request.headers.pop('simulation', False) 
     if is_simulation: 
      # don't actually send the SOAP request, just return its XML 
      return Reply(200, request.headers.dict, request.msg) 

     return HttpAuthenticated(request) 

... 
sim_transport = HttpAuthenticatedWithSimulation() 
client = Client(url, transport=sim_transport, 
       headers={'simulation': is_simulation}) 

這是一個有點哈克。 (例如,這依賴於HTTP頭將布爾模擬選項傳遞給傳輸級別。)但我希望這可以說明這個想法。

+0

您好感謝您的答覆。我已經使用另一個HttpTransport類來執行Ssl相互認證http://stackoverflow.com/questions/6277027/suds-over-https-with-cert。理論上,如果我只是用你的示例聲明發送方法,它應該工作?我明天會試一試 – hzrari 2014-09-24 18:58:23

+0

你的建議對我來說非常合適。 我剛做了一些小的修改。我將用解決方案編輯我的帖子 – hzrari 2014-09-25 11:14:52

相關問題