在suds中添加属性

dax*_*axa 6 python web-services suds

我必须用肥皂水和Python做肥皂请求

<soap:Body> 
    <registerOrder> 
        <order merchantOrderNumber="" description="" amount=""  currency=""  language=""  xmlns=""> 
             <returnUrl>http://mysafety.com</returnUrl> 
        </order> 
    </registerOrder> 
</soap:Body>
Run Code Online (Sandbox Code Playgroud)

如何在registerOrder中添加属性?

小智 8

MessagePlugin的更动态版本是:

from suds.sax.attribute import Attribute
from suds.plugin import MessagePlugin

class _AttributePlugin(MessagePlugin):
    """
    Suds plug-in extending the method call with arbitrary attributes.
    """
    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def marshalled(self, context):
        method = context.envelope.getChild('Body')[0]
        for key, item in self.kwargs.iteritems():
            method.attributes.append(Attribute(key, item))
Run Code Online (Sandbox Code Playgroud)

用法:

client = Client(url)
# method 1
client.options.plugins = [_AttributePlugin(foo='bar')]
response = client.service.method1()
client.options.plugins = []
# method 2
response = client.service.method2()
Run Code Online (Sandbox Code Playgroud)


Gan*_*ndi 5

suds文档中搜索MessagePlugin.编组选项是您正在搜索的内容.您需要将其作为插件添加到客户端:

self.client = Client(url, plugins=[MyPlugin()])
Run Code Online (Sandbox Code Playgroud)

在编组方法中搜索context.envelope子项.python的vars()函数在这个地方非常有用.我想,它应该像你这样的东西:

from suds.sax.attribute import Attribute
from suds.plugin import MessagePlugin
class MyPlugin(MessagePlugin):
    def marshalled(self, context):
        foo = context.envelope.getChild('Body').getChild('registerOrder')[0]
        foo.attributes.append(Attribute("foo", "bar"))
Run Code Online (Sandbox Code Playgroud)

上周我坐在这里,所以它可能会节省一些时间给你:)