无法在 suds 中创建 SOAP 过滤器 [英] Not able to create a SOAP filter in suds
问题描述
我有一个接受以下 XML 正文的 SOAP 请求
I have a SOAP request that takes below XML body
<x:Body>
<ser:CreateExportJobRequest>
<ser:ExportJobTypeName>Products</ser:ExportJobTypeName>
<ser:ExportColumns>
<ser:ExportColumn>Id</ser:ExportColumn>
<ser:ExportColumn>itemName</ser:ExportColumn>
</ser:ExportColumns>
<ser:ExportFilters>
<ser:ExportFilter id="updatedSince">
<ser:Text>2.0</ser:Text>
</ser:ExportFilter>
</ser:ExportFilters>
<ser:Frequency>ONETIME</ser:Frequency>
</ser:CreateExportJobRequest>
</x:Body>
我可以使用 成功提出请求回旋镖.
现在我实际上想在我的 python 代码中使用它.所以我试过了,
Now I actually want to use it in my python code. So I tried,
inputElement = client.factory.create('CreateExportJobRequest')
inputElement.ExportJobTypeName = "Products"
inputElement.ExportColumns.ExportColumn = ["Id", "itemName"]
inputElement.Frequency = 'ONETIME'
if updatedSince:
inputElement.ExportFilters.ExportFilter = ['updatedSince']
t = client.service.CreateExportJob(inputElement.ExportJobTypeName, inputElement.ExportColumns, inputElement.ExportFilters, None, None, inputElement.Frequency)
我收到一个错误,
'list' object has no attribute 'id'
因为创建了一个有点错误的 XML 请求
Because a somewhat wrong XML request gets created
<ns1:ExportFilters>
<ns1:ExportFilter>updatedSince</ns1:ExportFilter>
</ns1:ExportFilters>
所以我为 ExportFilter
尝试了一些其他的东西,比如
So I tried few other things for ExportFilter
like
inputElement.ExportFilters.ExportFilter = [{'id': 'updatedSince', 'text': updatedSince}]
和
inputElement.ExportFilters.ExportFilter = [('updatedSince', updatedSince)]
和
inputElement.ExportFilters.ExportFilter = [{'updatedSince': updatedSince}]
# says, Type not found: 'updatedSince'
和
inputElement.ExportFilters.ExportFilter = [
{'key': 'updatedSince', 'value': {'key': 'eq', 'value': updatedSince}}
]
# says, Type not found: 'value'
但没有任何效果.
在设置ExportFilter之前,其值的形式为
Before setting ExportFilter, it's value is in the form of
ExportFilters: (ExportFilters){
ExportFilter[] = <empty>
}
请帮忙.
推荐答案
经过调试和一些 suds 代码后,我找到了修复方法.
After debugging and going through some suds code, I have found the fix.
修复的完整代码片段:
inputElement = client.factory.create('CreateExportJobRequest')
inputElement.ExportJobTypeName = "Products"
inputElement.ExportColumns.ExportColumn = ["Id", "itemName"]
inputElement.Frequency = 'ONETIME'
if updatedSince:
efilter = client.factory.create("ExportFilter")
efilter._id = 'updatedSince'
efilter.Text = updatedSince
inputElement.ExportFilters.ExportFilter.append(efilter)
t = client.service.CreateExportJob(inputElement.ExportJobTypeName, inputElement.ExportColumns, inputElement.ExportFilters, None, None, inputElement.Frequency)
调试:因为 suds 引发了 TypeNotFound
异常,所以我在 suds 中查找了 raise TypeNotFound
的所有地方.我将调试点放在了我的 PyCharm 中.
Debugging:
Because suds was raising TypeNotFound
exception, I looked for all the places that raise TypeNotFound
inside suds. I put debug points in my PyCharm.
我发现 suds/mx/literal.py
中 Typed
类的 start
方法引发了我遇到的错误.
I found that the start
method from Typed
class inside suds/mx/literal.py
was raising the error I was getting.
def start(self, content):
#
# Start marshalling the 'content' by ensuring that both the
# 'content' _and_ the resolver are primed with the XSD type
# information. The 'content' value is both translated and
# sorted based on the XSD type. Only values that are objects
# have their attributes sorted.
#
log.debug('starting content:
%s', content)
if content.type is None:
name = content.tag
if name.startswith('_'):
name = '@'+name[1:]
content.type = self.resolver.find(name, content.value)
if content.type is None:
raise TypeNotFound(content.tag)
else:
known = None
if isinstance(content.value, Object):
known = self.resolver.known(content.value)
if known is None:
log.debug('object has no type information', content.value)
known = content.type
frame = Frame(content.type, resolved=known)
self.resolver.push(frame)
frame = self.resolver.top()
content.real = frame.resolved
content.ancestry = frame.ancestry
self.translate(content)
self.sort(content)
if self.skip(content):
log.debug('skipping (optional) content:
%s', content)
self.resolver.pop()
return False
else:
return True
所以根据这个逻辑,我找到了解决办法.
So from this logic, I came to the fix.
但是,如果有人为此建议一个标准程序,那就太好了.
But, It would be really great if somebody suggests a standard procedure for this.
这篇关于无法在 suds 中创建 SOAP 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!