Не удалось создать 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>
}

Пожалуйста помоги.


person Hussain    schedule 16.06.2017    source источник


Ответы (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 внутри suds. Я помещаю точки отладки в свой PyCharm.

Я обнаружил, что метод start из класса Typed внутри suds/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

Вот из этой логики я и пришел к исправлению.

Но было бы здорово, если бы кто-нибудь предложил стандартную процедуру для этого.

person Hussain    schedule 19.07.2017