2014-09-24 81 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

相关问题