Python Zeep - How to use sequence -> choice

1.6k views Asked by At

I am trying to use Zeep with a choice inside a sequence but I cannot manage to make this work.

This is my code:

#create trade ref element
tradeReference_element = client.get_element('ns4:tradeReference')
trade_ref_object = xsd.AnyObject(tradeReference_element, tradeReference_element(tradeId="NKY 12/08/17 C21000"))

#create export element
export_element = client.get_element('ns32:export')
export_element_object = xsd.AnyObject(export_element, export_element(_value_1={'tradeReference' : trade_ref_object}))

#use exportEntities service
client.service.exportEntities(export=export_element_object)

and I get the following error:

client.service.exportEntities(export=export_element_object)
Traceback (most recent call last):

  File "<ipython-input-14-3cee211bc26a>", line 1, in <module>
    client.service.exportEntities(export=export_element_object)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\client.py", line 41, in __call__
    self._op_name, args, kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\wsdl\bindings\soap.py", line 110, in send
    options=options)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\wsdl\bindings\soap.py", line 68, in _create
    serialized = operation_obj.create(*args, **kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\wsdl\definitions.py", line 197, in create
    return self.input.serialize(*args, **kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\wsdl\messages\soap.py", line 63, in serialize
    body_value = self.body(*args, **kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\elements\element.py", line 48, in __call__
    instance = self.type(*args, **kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\types\complex.py", line 42, in __call__
    return self._value_class(*args, **kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\valueobjects.py", line 89, in __init__
    items = _process_signature(self._xsd_type, args, kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\valueobjects.py", line 199, in _process_signature
    values = element.parse_kwargs(kwargs, None, available_kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\elements\indicators.py", line 204, in parse_kwargs
    sub_result = element.parse_kwargs(kwargs, elm_name, available_kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\elements\element.py", line 118, in parse_kwargs
    kwargs, name or self.attr_name, available_kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\types\complex.py", line 278, in parse_kwargs
    value = self._create_object(value, name)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\types\complex.py", line 303, in _create_object
    return self(value)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\types\complex.py", line 42, in __call__
    return self._value_class(*args, **kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\valueobjects.py", line 89, in __init__
    items = _process_signature(self._xsd_type, args, kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\valueobjects.py", line 176, in _process_signature
    values, args, index = element.parse_args(args, index)

  File "C:\ProgramData\Anaconda2\lib\site-packages\zeep\xsd\elements\indicators.py", line 130, in parse_args
    raise TypeError("Choice elements only work with keyword arguments")

TypeError: Choice elements only work with keyword arguments

So it seems that the issue is "Choice elements only work with keyword arguments"

My export element_object is the following:

<AnyObject(type=<Element(name=u'export', type=<zeep.xsd.dynamic_types.ExportMessage object at 0x0D966490>)>, value={
    'header': None,
    'validation': [],
    '_value_1': [
        {
            'tradeReference': <AnyObject(type=<Element(name=u'tradeReference', type=<zeep.xsd.dynamic_types.TradeReference object at 0x0D394C30>)>, value={
                'tradeId': {
                    '_value_1': 'NKY 12/08/17 C21000',
                    'tradeIdScheme': None
                },
                'href': None,
                'ID': None
            }
        }
    ],
    'version': None,
    'batchType': 'NoSession',
    'followDependencies': 'false',
    'exportReferencesOnly': 'false'
})>

I assume the issue is "_value_1" but it was created by Zeep and the keyword 'tradeReference' is there.

Could anyone please point me to what I am doing wrong?

export element:

<xsd:complexType name="ExportMessage">
  <xsd:complexContent>
    <xsd:extension base="fpml:Message">
      <xsd:sequence>
        <xsd:choice minOccurs="0" maxOccurs="unbounded">
          <xsd:element ref="party:partyReference">...</xsd:element>
          <xsd:element ref="trade:tradeReference">...</xsd:element>
          <xsd:element ref="valuation:market">...</xsd:element>
        </xsd:choice>
      </xsd:sequence>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>

<xsd:element name="export" type="tns:ExportMessage" substitutionGroup="tns:message"/>

tradeReference Element:

<xsd:complexType name="TradeReference">
  <xsd:sequence>
    <xsd:element name="tradeId" type="tns:TradeId" maxOccurs="unbounded" minOccurs="0"/>
  </xsd:sequence>
  <xsd:attribute name="href" use="optional"/>
  <xsd:attribute name="ID" type="xsd:ID" use="optional"/>
</xsd:complexType>

<xsd:element name="tradeReference" type="tns:TradeReference"/>

and the service:

exportEntities(export: ns32:ExportMessage) -> message: ns30:Message, error: ns34:Error
0

There are 0 answers