无法在suds

时间:2017-06-16 10:27:30

标签: python soap suds

我有一个SOAP请求,它接受XML主体

<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>

我可以使用Boomerang发出成功的请求。

现在我真的想在我的python代码中使用它。所以我试过了,

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请求

<ns1:ExportFilters>
    <ns1:ExportFilter>updatedSince</ns1:ExportFilter>
</ns1:ExportFilters>

所以我为ExportFilter尝试了很多其他的事情,比如

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之前,它的值是

的形式
ExportFilters: (ExportFilters){
  ExportFilter[] = <empty>
}

请帮忙。

1 个答案:

答案 0 :(得分:1)

经过调试和浏览一些suds代码后,我找到了修复程序。

修复程序的完整代码段:

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例外,我找了所有raise TypeNotFound里面的地方。我把调试点放在我的PyCharm中。

我发现start内的Typedsuds/mx/literal.py方法引发了我的错误。

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:\n%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:\n%s', content)
        self.resolver.pop()
        return False
    else:
        return True

所以从这个逻辑来看,我来修复了。

但是,如果有人建议采用标准程序,那将是非常好的。