【发布时间】:2022-01-10 10:14:24
【问题描述】:
我正在使用 Python3.7 和 zeep 连接到 SOAP v2 网络服务。一个创建对象的调用需要一个复杂的结构作为参数传递。这是调用的 WSDL 及其参数:
<message name="catalogProductAttributeAddOptionRequest">
<part name="sessionId" type="xsd:string"/>
<part name="attribute" type="xsd:string"/>
<part name="data" type="typens:catalogProductAttributeOptionEntityToAdd"/>
</message>
<complexType name="catalogProductAttributeOptionEntityToAdd">
<all>
<element name="label" type="typens:catalogProductAttributeOptionLabelArray"/>
<element name="order" type="xsd:int"/>
<element name="is_default" type="xsd:int"/>
</all>
</complexType>
<complexType name="catalogProductAttributeOptionLabelArray">
<complexContent>
<restriction base="soapenc:Array">
<attribute ref="soapenc:arrayType" wsdl:arrayType="typens:catalogProductAttributeOptionLabelEntity[]"/>
</restriction>
</complexContent>
</complexType>
<complexType name="catalogProductAttributeOptionLabelEntity">
<all>
<element name="store_id" type="typens:ArrayOfString"/>
<element name="value" type="xsd:string"/>
</all>
</complexType>
我遇到的问题是如何使用 zeep 将“数据”参数传递给 Python 3 中的函数。我有一个关于如何在 php 中执行此操作的示例:
$label = array (
array(
"store_id" => array("0"),
"value" => "some random data"
)
);
$data = array(
"label" => $label,
"order" => "10",
"is_default" => "1"
);
$orders = $client->catalogProductAttributeAddOption($session, $attributeCode, $data);
这个代码应该可以工作,尽管我还没有测试过。因此,$data 结构在 python dict 中应该与这个结构等效:
data=[
{
"label": [
[
{
"store_id":["0"],
"value":"some random data"
}
]
],
"order":10,
"is_default":1
}
]
我这样调用函数:
client.service.catalogProductAttributeAddOption(sessionId=sessionid,attribute="manufacturer",data=data)
如果我这样做,我会收到此异常:
TypeError: Any element received object of type 'list', expected lxml.etree._Element or builtins.dict or zeep.objects.catalogProductAttributeOptionLabelEntity
See http://docs.python-zeep.org/en/master/datastructures.html#any-objects for more information
所以我开始研究 Any 元素,我发现了一种将结构的一部分转换为 wsdl 命名空间中的类型的方法,所以我这样做了:
entity={
"store_id":["0"],
"value":"some random data"
}
catalogProductAttributeOptionLabelEntity=client.get_type('ns0:catalogProductAttributeOptionLabelEntity')
retyped_entity=catalogProductAttributeOptionLabelEntity(entity)
label=[
retyped_entity
]
catalogProductAttributeOptionLabelArray = client.get_type('ns0:catalogProductAttributeOptionLabelArray')
retyped_label=catalogProductAttributeOptionLabelArray(label)
data=[
{
"label": retyped_label,
"order":10,
"is_default":1
}
]
catalogProductAttributeOptionEntityToAdd=client.get_type('ns0:catalogProductAttributeOptionEntityToAdd')
retyped_data=catalogProductAttributeOptionEntityToAdd(data)
client.service.catalogProductAttributeAddOption(sessionId=sessionid,attribute="manufacturer",data=retyped_data)
然后,我得到了这个错误:
ValidationError: 'Missing element for Any'
我已经调查过,似乎这个错误是在结构不是所需格式时出现的......我的意思是,如果我之前写的 dict 不等同于之前写的 php 结构,如果使用所需类型的铸件创建的新结构不在所需的结构中。
此时我被卡住了,我不知道该怎么做。任何专家都可以看出我的错误在哪里?
顺便说一句,如果解决了这个问题,这也回答了“如何使用带有 Python 和 Zeep 的 SOAP v2 Web 服务在 Magento 1 中编写制造商”的问题。任何地方都没有解决的问题。
提前致谢。
【问题讨论】:
-
我无法帮助您详细说明您的情况,但我会尝试在一开始就在soapUI中提出正确的请求。 soapui.org
标签: python-3.x magento soap zeep