消息插件的一般描述可以在Suds documentation 中找到。更多细节在class documentation。你不叫 marshalled,suds 可以。为了更好地理解如何实现编组方法,请阅读Element 的文档。 suds 插件本质上是一个监听器。我的示例使用公共 Web 服务进行演示。
假设您的请求如下所示:
...
<ns0:Body>
<ns1:GetStatistics>
<ns1:X>
...
但你需要它看起来像这样:
....
<ns0:Body>
<ns1:GetStatistics type="specialType">
<ns1:X>
...
这是一个将 type 属性添加到 GetStatistics 元素的插件。当元素发送了子元素和属性时,这可能是必需的。 Suds 0.4 不支持这一点,但它是有效的 SOAP。可能有一个分支确实支持这一点。
*** Settings ***
Library SudsLibrary
Library c:/SudsLibraryExtensions.py
*** Test Cases ***
Message Plugin
Create Soap Client http://www.webservicex.net/Statistics.asmx?WSDL
Attach My Plugin
Set GetStats Type specialType
${dbl array}= Create Wsdl Object ArrayOfDouble
Append To List ${dbl array.double} 2.0
Append To List ${dbl array.double} 3.0
${result}= Call Soap Method GetStatistics ${dbl array}
Should Be Equal As Numbers ${result.Average} 2.5
c:/SudsLibraryExtensions.py: 的内容:
from robot.libraries.BuiltIn import BuiltIn
from suds.plugin import MessagePlugin
class _MyPlugin(MessagePlugin):
def __init__(self):
self._type = 'defaultType'
def marshalled(self, context):
body = context.envelope.getChild('Body')
call = body.getChild('GetStatistics')
call.set('type', self._type)
def set_getstats_type(self, value):
self._type = value
class SudsLibraryExtensions(object):
def attach_my_plugin(self):
client = BuiltIn().get_library_instance("SudsLibrary")._client()
plugins = client.options.plugins
if any(isinstance(x, _MyPlugin) for x in plugins):
return
# prepend so SudsLibrary's plugin is left in place
plugins.insert(0, _MyPlugin())
client.set_options(plugins=plugins)
def set_getstats_type(self, value):
self._get_plugin().set_getstats_type(value)
def _get_plugin(self):
client = BuiltIn().get_library_instance("SudsLibrary")._client()
plugins = client.options.plugins
my_plugin = next((plugin for plugin in plugins if isinstance(plugin, _MyPlugin)), None)
if my_plugin is None:
raise RuntimeError("Plugin not found. Did you call Attach My Plugin?")
return my_plugin
只要插件使用关键字附加我的插件附加,类型属性将始终设置。有一个默认类型。要更改类型的值,请使用关键字 Set GetStats Type。任何类型集都将在未来的所有请求中使用,直到它被更改。这里使用两个类的唯一原因是防止“marshalled”成为暴露的关键字。