【问题标题】:python soap api using suds: create complex variablepython soap api使用suds:创建复杂变量
【发布时间】:2016-07-21 07:42:17
【问题描述】:
我正在尝试为肥皂 API 编写肥皂客户端。
我正在为此使用 Pythons suds 图书馆。
我想创建一个复杂的变量,它将在soap request中给出以下输出:
<pay:idInfo>
<api:TagValuePair>
<api:tag>phoneNumber</<api:tag>
<api:value>tel:+11234567890</value><api:value>
</api:TagValuePair>
</pay:idInfo>
下面是idInfo的类型:
>>> id_info = client.factory.create("ns0:ArrayOfTagValuePair")
>>> id_info
(ArrayOfTagValuePair){
TagValuePair[] = <empty>
}
如何在其中添加标签和值对?
【问题讨论】:
标签:
python-2.7
web-services
soap
suds
【解决方案1】:
尝试以下方法:
class ArrayOfTagValuePair(suds.plugin.DocumentPlugin):
"""Plugin to add element to AccountAssignment that is not defined in WSDL."""
def __init__(self, *args):
self.args = args
def parsed(self, context):
complexTypes = context.document.getRoot().getChild('types').getChild('schema').getChildren('complexType')
# Get the complex type with attribute 'name' == 'ArrayOfTagValuePair'.
for ct in complexTypes:
if ct.get('name') == 'ArrayOfTagValuePair':
array_TagValuePair = ct
sequenceElements = array_TagValuePair.getChild('complexContent').getChild('extension').getChild('sequence')
for key in self.args:
sequenceElements.append(e)
def TagValuePair(self):
module_name = 'WS3Trans'
service_name = 'TagValuePair'
service_url = '%s/%s/%s.asmx?WSDL' %(self.webservice_url, module_name, service_name)
client = suds.client.Client(service_url, plugins=[ArrayOfTagValuePair('tag', 'value')])
return client
参考以下文档here & here