def test_document_message_serializer(): wsdl = stub(schema=stub(_prefix_map={})) operation = stub(soapaction='my-actiGn') msg = messages.DocumentMessage(wsdl=wsdl, name=None, operation=operation, nsmap=soap.Soap11Binding.nsmap) namespace = 'http://test.python-zeep.org/tests/document' # Fake resolve() msg.body = xsd.Element( etree.QName(namespace, 'response'), xsd.ComplexType([ xsd.Element(etree.QName(namespace, 'arg1'), xsd.String()), xsd.Element(etree.QName(namespace, 'arg2'), xsd.String()), ])) msg.namespace = { 'body': 'http://test.python-zeep.org/tests/document', 'header': None, 'headerfault': None } serialized = msg.serialize(arg1='ah1', arg2='ah2') expected = """ <?xml version="1.0"?> <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Body> <ns0:response xmlns:ns0="http://test.python-zeep.org/tests/document"> <ns0:arg1>ah1</ns0:arg1> <ns0:arg2>ah2</ns0:arg2> </ns0:response> </soap-env:Body> </soap-env:Envelope> """ assert_nodes_equal(expected, serialized.content)
def resolve(self, definitions, abstract_message): """Override the default `SoapMessage.resolve()` since we need to wrap the parts in an element. """ self.abstract = abstract_message # If this message has no parts then we have nothing to do. This might # happen for output messages which don't return anything. if not self.abstract.parts and self.type != 'input': return parts = OrderedDict(self.abstract.parts) self.headers = self._resolve_header(self._info['header'], definitions, parts) self.headerfault = self._resolve_header(self._info['headerfault'], definitions, parts) # Each part is a parameter or a return value and appears inside a # wrapper element within the body named identically to the operation # name and its namespace is the value of the namespace attribute. body_info = self._info['body'] if body_info: namespace = self._info['body']['namespace'] if self.type == 'input': tag_name = etree.QName(namespace, self.operation.name) else: tag_name = etree.QName(namespace, self.abstract.name.localname) self.body = xsd.Element( tag_name, xsd.ComplexType( xsd.Sequence([ msg.element if msg.element else xsd.Element( name, msg.type) for name, msg in parts.items() ])))
def test_build_objects(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'username'), xsd.String()), xsd.Group( etree.QName('http://tests.python-zeep.org/', 'groupie'), xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'password'), xsd.String(), ) ]) ) ]) )) assert custom_type.signature() obj = custom_type(username='******', password='******') expected = """ <document> <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/"> <ns0:username>foo</ns0:username> <ns0:password>bar</ns0:password> </ns0:authentication> </document> """ node = etree.Element('document') custom_type.render(node, obj) assert_nodes_equal(expected, node) obj = custom_type.parse(node[0], None) assert obj.username == 'foo' assert obj.password == 'bar'
def test_build_group_min_occurs_1(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Group( etree.QName('http://tests.python-zeep.org/', 'foobar'), xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_1'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_2'), xsd.String()), ]), min_occurs=1) )) obj = custom_type(item_1='foo', item_2='bar') assert obj.item_1 == 'foo' assert obj.item_2 == 'bar' result = render_node(custom_type, obj) expected = load_xml(""" <document> <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2>bar</ns0:item_2> </ns0:authentication> </document> """) assert_nodes_equal(result, expected) obj = custom_type.parse(result[0], None) assert obj.item_1 == 'foo' assert obj.item_2 == 'bar' assert not hasattr(obj, 'foobar')
def __init__(self, client): self.client = client WDSL = "http://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2017-10-01" self.stations = {} with open("station_crs.txt", "r") as f: for line in f: self.stations[line.split(":")[0]] = ( line.split(":")[1]).replace("\n", "") history = HistoryPlugin() self.wdsl_client = Client(wsdl=WDSL, plugins=[history]) header = xsd.Element( '{http://thalesgroup.com/RTTI/2013-11-28/Token/types}AccessToken', xsd.ComplexType([ xsd.Element( '{http://thalesgroup.com/RTTI/2013-11-28/Token/types}TokenValue', xsd.String()), ])) self.header_value = header(TokenValue=os.environ.get("NRE_TOKEN")) self.tfl_api_token = json.loads(os.environ) self.line_id = { "bakerloo": 0, "central": 1, "circle": 2, "district": 3, "hammersmith-city": 4, "jubilee": 5, "metropolitan": 6, "northern": 7, "piccadilly": 8, "victoria": 9, "waterloo-city": 10 }
def test_default_soap_headers(): header = xsd.Element( None, xsd.ComplexType( xsd.Sequence([ xsd.Element('{http://tests.python-zeep.org}name', xsd.String()), xsd.Element('{http://tests.python-zeep.org}password', xsd.String()), ]))) header_value = header(name='ik', password='******') client_obj = client.Client('tests/wsdl_files/soap.wsdl') client_obj.set_default_soapheaders([header_value]) response = """ <?xml version="1.0"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://example.com/stockquote.xsd"> <soapenv:Header/> <soapenv:Body> <stoc:TradePrice> <price>120.123</price> </stoc:TradePrice> </soapenv:Body> </soapenv:Envelope> """.strip() with requests_mock.mock() as m: m.post('http://example.com/stockquote', text=response) client_obj.service.GetLastTradePrice('foobar') doc = load_xml(m.request_history[0].body) header = doc.find('{http://schemas.xmlsoap.org/soap/envelope/}Header') assert header is not None assert len(header.getchildren()) == 2
def test_create_node(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType(children=[ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'username'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'password'), xsd.String()), ])) obj = custom_type(username='******', password='******') expected = """ <document> <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/"> <ns0:username>foo</ns0:username> <ns0:password>bar</ns0:password> </ns0:authentication> </document> """ node = etree.Element('document') custom_type.render(node, obj) assert_nodes_equal(expected, node)
def test_nested_choice_optional(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_1'), xsd.String()), xsd.Choice([ xsd.Element('{http://tests.python-zeep.org/}item_2', xsd.String()), xsd.Element('{http://tests.python-zeep.org/}item_3', xsd.String()), ], min_occurs=0, max_occurs=1), ]))) expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2>bar</ns0:item_2> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2 == 'bar' expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2 is None assert obj.item_3 is None
def test_group_optional(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Group(etree.QName('http://tests.python-zeep.org/', 'foobar'), xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_1'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_2'), xsd.String()), ]), min_occurs=1))) expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2>bar</ns0:item_2> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2 == 'bar' assert not hasattr(obj, 'foobar')
def test_build_max_occurs_unbounded(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_1'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_2'), xsd.String()), ], max_occurs='unbounded'))) expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2>bar</ns0:item_2> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj._value_1 == [{ 'item_1': 'foo', 'item_2': 'bar', }]
def test_serialize_simple(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'name'), xsd.String()), xsd.Attribute( etree.QName('http://tests.python-zeep.org/', 'attr'), xsd.String()), ]) )) obj = custom_type(name='foo', attr='x') assert obj.name == 'foo' assert obj.attr == 'x' result = serialize_object(obj) assert result == { 'name': 'foo', 'attr': 'x', }
def test_duplicate_element_names(): custom_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "container"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item"), xsd.String(), ), ])), ) # sequences expected = "{http://tests.python-zeep.org/}container(item: xsd:string, item__1: xsd:string, item__2: xsd:string)" assert custom_type.signature() == expected obj = custom_type(item="foo", item__1="bar", item__2="lala") expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item>foo</ns0:item> <ns0:item>bar</ns0:item> <ns0:item>lala</ns0:item> </ns0:container> </document> """ node = render_node(custom_type, obj) assert_nodes_equal(expected, node)
def test_choice_sequences_optional_elms(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Sequence([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String(), min_occurs=0) ]), xsd.Sequence([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()), xsd.Element('item_3', xsd.String()) ]), ]) ]) ) args = tuple([]) kwargs = {'item_1': 'value-1'} result = valueobjects._process_signature(xsd_type, args, kwargs) assert result == { 'item_1': 'value-1', 'item_2': None, 'item_3': None, }
def _resolve_body(self, info, definitions, parts): name = etree.QName(self.nsmap["soap-env"], "Body") if not info or not parts: return None # If the part name is omitted then all parts are available under # the soap:body tag. Otherwise only the part with the given name. if info["part"]: part_name = info["part"] sub_elements = [parts[part_name].element] else: sub_elements = [] for part_name, part in parts.items(): element = part.element.clone() element.attr_name = part_name or element.name sub_elements.append(element) if len(sub_elements) > 1: self._is_body_wrapped = True return xsd.Element(name, xsd.ComplexType(xsd.All(sub_elements))) else: self._is_body_wrapped = False return sub_elements[0]
def test_mime_content_serialize_xml(): wsdl = stub(types=stub(_prefix_map={})) operation = stub(location='my-action', name='foo') element_1 = xsd.Element( 'arg1', xsd.ComplexType([xsd.Element('arg1_1', xsd.String())])) element_2 = xsd.Element('arg2', xsd.String()) abstract_message = definitions.AbstractMessage( etree.QName('{http://test.python-zeep.org/tests/msg}Method')) abstract_message.parts = OrderedDict([ ('xarg1', definitions.MessagePart(element=element_1, type=None)), ('xarg2', definitions.MessagePart(element=element_2, type=None)), ]) msg = messages.MimeContent(wsdl=wsdl, name=None, operation=operation, content_type='text/xml', part_name=None) msg._info = { 'body': { 'namespace': 'http://test.python-zeep.org/tests/rpc' }, 'header': None, 'headerfault': None } msg.resolve(wsdl, abstract_message) serialized = msg.serialize(xarg1={'arg1_1': 'uh'}, xarg2='bla') assert serialized.headers == {'Content-Type': 'text/xml'} assert serialized.path == 'my-action' assert_nodes_equal( load_xml(serialized.content), load_xml( "<foo><xarg1><arg1_1>uh</arg1_1></xarg1><xarg2>bla</xarg2></foo>"))
def test_element_attribute_name_conflict(): custom_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "container"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item"), xsd.String(), ) ]), [ xsd.Attribute("foo", xsd.String()), xsd.Attribute("item", xsd.String()) ], ), ) # sequences expected = "{http://tests.python-zeep.org/}container(item: xsd:string, foo: xsd:string, attr__item: xsd:string)" assert custom_type.signature() == expected obj = custom_type(item="foo", foo="x", attr__item="bar") expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/" foo="x" item="bar"> <ns0:item>foo</ns0:item> </ns0:container> </document> """ node = render_node(custom_type, obj) assert_nodes_equal(expected, node) obj = custom_type.parse(list(node)[0], None) assert obj.item == "foo" assert obj.foo == "x" assert obj.attr__item == "bar"
def test_serialize_with_header_and_custom_mixed(): wsdl_content = StringIO(""" <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://tests.python-zeep.org/tns" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://tests.python-zeep.org/tns"> <types> <xsd:schema targetNamespace="http://tests.python-zeep.org/tns" elementFormDefault="qualified"> <xsd:element name="Request"> <xsd:complexType> <xsd:sequence> <xsd:element name="arg1" type="xsd:string"/> <xsd:element name="arg2" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Authentication"> <xsd:complexType> <xsd:sequence> <xsd:element name="username" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </types> <message name="Input"> <part element="tns:Request"/> <part element="tns:Authentication" name="Header"/> </message> <portType name="TestPortType"> <operation name="TestOperation"> <input message="Input"/> </operation> </portType> <binding name="TestBinding" type="tns:TestPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="TestOperation"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> <soap:header message="tns:Input" part="Header" use="literal"/> </input> </operation> </binding> </definitions> """.strip()) root = wsdl.Document(wsdl_content, None) binding = root.bindings['{http://tests.python-zeep.org/tns}TestBinding'] operation = binding.get('TestOperation') header = root.types.get_element( '{http://tests.python-zeep.org/tns}Authentication') header_1 = header(username='******') header = xsd.Element( '{http://test.python-zeep.org/custom}custom', xsd.ComplexType([ xsd.Element('{http://test.python-zeep.org/custom}foo', xsd.String()), ])) header_2 = header(foo='bar') serialized = operation.input.serialize(arg1='ah1', arg2='ah2', _soapheaders=[header_1, header_2]) expected = """ <?xml version="1.0"?> <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Header> <ns0:Authentication xmlns:ns0="http://tests.python-zeep.org/tns"> <ns0:username>mvantellingen</ns0:username> </ns0:Authentication> <ns1:custom xmlns:ns1="http://test.python-zeep.org/custom"> <ns1:foo>bar</ns1:foo> </ns1:custom> </soap-env:Header> <soap-env:Body> <ns0:Request xmlns:ns0="http://tests.python-zeep.org/tns"> <ns0:arg1>ah1</ns0:arg1> <ns0:arg2>ah2</ns0:arg2> </ns0:Request> </soap-env:Body> </soap-env:Envelope> """ assert_nodes_equal(expected, serialized.content)
def test_mixed_choice(): custom_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "authentication"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_1"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_2"), xsd.String(), ), xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_3"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_4"), xsd.String(), ), ]), xsd.Choice([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_5"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_6"), xsd.String(), ), xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_7"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_8"), xsd.String(), ), ]), ]), ])), ) item = custom_type( item_1="item-1", item_2="item-2", item_3="item-3", item_4="item-4", item_7="item-7", item_8="item-8", ) assert item.item_1 == "item-1" assert item.item_2 == "item-2" assert item.item_3 == "item-3" assert item.item_4 == "item-4" assert item.item_7 == "item-7" assert item.item_8 == "item-8"
def test_choice_init(): root = xsd.Element( etree.QName("http://tests.python-zeep.org/", "kies"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "pre"), xsd.String(), ), xsd.Choice( [ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_1"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_2"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_3"), xsd.String(), ), xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_4_1"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_4_2"), xsd.String(), ), ]), ], max_occurs=4, ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "post"), xsd.String(), ), ])), ) obj = root( pre="foo", _value_1=[ { "item_1": "value-1" }, { "item_2": "value-2" }, { "item_1": "value-3" }, { "item_4_1": "value-4-1", "item_4_2": "value-4-2" }, ], post="bar", ) assert obj._value_1 == [ { "item_1": "value-1" }, { "item_2": "value-2" }, { "item_1": "value-3" }, { "item_4_1": "value-4-1", "item_4_2": "value-4-2" }, ] node = etree.Element("document") root.render(node, obj) assert etree.tostring(node) expected = """ <document> <ns0:kies xmlns:ns0="http://tests.python-zeep.org/"> <ns0:pre>foo</ns0:pre> <ns0:item_1>value-1</ns0:item_1> <ns0:item_2>value-2</ns0:item_2> <ns0:item_1>value-3</ns0:item_1> <ns0:item_4_1>value-4-1</ns0:item_4_1> <ns0:item_4_2>value-4-2</ns0:item_4_2> <ns0:post>bar</ns0:post> </ns0:kies> </document> """.strip() assert_nodes_equal(expected, node)
def test_any(): some_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "doei"), xsd.String()) complex_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "complex"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_1"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_2"), xsd.String(), ), ])), ) custom_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "hoi"), xsd.ComplexType(xsd.Sequence([xsd.Any(), xsd.Any(), xsd.Any()])), ) any_1 = xsd.AnyObject(some_type, "DOEI!") any_2 = xsd.AnyObject(complex_type, complex_type(item_1="val_1", item_2="val_2")) any_3 = xsd.AnyObject( complex_type, [ complex_type(item_1="val_1_1", item_2="val_1_2"), complex_type(item_1="val_2_1", item_2="val_2_2"), ], ) obj = custom_type(_value_1=any_1, _value_2=any_2, _value_3=any_3) expected = """ <document> <ns0:hoi xmlns:ns0="http://tests.python-zeep.org/"> <ns0:doei>DOEI!</ns0:doei> <ns0:complex> <ns0:item_1>val_1</ns0:item_1> <ns0:item_2>val_2</ns0:item_2> </ns0:complex> <ns0:complex> <ns0:item_1>val_1_1</ns0:item_1> <ns0:item_2>val_1_2</ns0:item_2> </ns0:complex> <ns0:complex> <ns0:item_1>val_2_1</ns0:item_1> <ns0:item_2>val_2_2</ns0:item_2> </ns0:complex> </ns0:hoi> </document> """ node = etree.Element("document") custom_type.render(node, obj) assert_nodes_equal(expected, node)
def test_nested_complex_type_optional(): custom_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "authentication"), xsd.ComplexType( xsd.Sequence( [ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_1"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_2"), xsd.ComplexType( xsd.Sequence( [ xsd.Choice( [ xsd.Element( "{http://tests.python-zeep.org/}item_2a1", xsd.String(), min_occurs=0, ), xsd.Element( "{http://tests.python-zeep.org/}item_2a2", xsd.String(), min_occurs=0, ), ] ), xsd.Element( "{http://tests.python-zeep.org/}item_2b", xsd.String(), ), ] ) ), min_occurs=0, max_occurs="unbounded", ), ] ) ), ) expected = etree.fromstring( """ <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> </ns0:container> """ ) obj = custom_type.parse(expected, None) assert obj.item_1 == "foo" assert obj.item_2 == [] expected = etree.fromstring( """ <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2/> </ns0:container> """ ) obj = custom_type.parse(expected, None) assert obj.item_1 == "foo" assert obj.item_2 == [None] expected = etree.fromstring( """ <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2> <ns0:item_2a1>x</ns0:item_2a1> </ns0:item_2> </ns0:container> """ ) obj = custom_type.parse(expected, None) assert obj.item_1 == "foo" assert obj.item_2[0].item_2a1 == "x" assert obj.item_2[0].item_2b is None expected = etree.fromstring( """ <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2> <ns0:item_2a1>x</ns0:item_2a1> <ns0:item_2b/> </ns0:item_2> </ns0:container> """ ) obj = custom_type.parse(expected, None) assert obj.item_1 == "foo" assert obj.item_2[0].item_2a1 == "x" assert obj.item_2[0].item_2b is None
def test_any(): some_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'doei'), xsd.String()) complex_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'complex'), xsd.ComplexType( children=[ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_1'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_2'), xsd.String()), ] )) custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'hoi'), xsd.ComplexType( children=[ xsd.Any(), xsd.Any(), xsd.Any(), ] )) any_1 = xsd.AnyObject(some_type, "DOEI!") any_2 = xsd.AnyObject( complex_type, complex_type(item_1='val_1', item_2='val_2')) any_3 = xsd.AnyObject( complex_type, [ complex_type(item_1='val_1_1', item_2='val_1_2'), complex_type(item_1='val_2_1', item_2='val_2_2'), ]) obj = custom_type(_any_1=any_1, _any_2=any_2, _any_3=any_3) expected = """ <document> <ns0:hoi xmlns:ns0="http://tests.python-zeep.org/"> <ns0:doei>DOEI!</ns0:doei> <ns0:complex> <ns0:item_1>val_1</ns0:item_1> <ns0:item_2>val_2</ns0:item_2> </ns0:complex> <ns0:complex> <ns0:item_1>val_1_1</ns0:item_1> <ns0:item_2>val_1_2</ns0:item_2> </ns0:complex> <ns0:complex> <ns0:item_1>val_2_1</ns0:item_1> <ns0:item_2>val_2_2</ns0:item_2> </ns0:complex> </ns0:hoi> </document> """ node = etree.Element('document') custom_type.render(node, obj) assert_nodes_equal(expected, node)
def test_serializer_with_header_custom_elm(): wsdl_content = StringIO(""" <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://tests.python-zeep.org/tns" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://tests.python-zeep.org/tns"> <types> <xsd:schema targetNamespace="http://tests.python-zeep.org/tns" elementFormDefault="qualified"> <xsd:element name="Request"> <xsd:complexType> <xsd:sequence> <xsd:element name="arg1" type="xsd:string"/> <xsd:element name="arg2" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </types> <message name="Input"> <part element="tns:Request"/> </message> <portType name="TestPortType"> <operation name="TestOperation"> <input message="Input"/> </operation> </portType> <binding name="TestBinding" type="tns:TestPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="TestOperation"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> </operation> </binding> </definitions> """.strip()) root = wsdl.Document(wsdl_content, None) binding = root.bindings["{http://tests.python-zeep.org/tns}TestBinding"] operation = binding.get("TestOperation") header = xsd.Element( "{http://test.python-zeep.org/custom}auth", xsd.ComplexType([ xsd.Element("{http://test.python-zeep.org/custom}username", xsd.String()) ]), ) serialized = operation.input.serialize( arg1="ah1", arg2="ah2", _soapheaders=[header(username="******")]) expected = """ <?xml version="1.0"?> <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Header> <ns0:auth xmlns:ns0="http://test.python-zeep.org/custom"> <ns0:username>mvantellingen</ns0:username> </ns0:auth> </soap-env:Header> <soap-env:Body> <ns0:Request xmlns:ns0="http://tests.python-zeep.org/tns"> <ns0:arg1>ah1</ns0:arg1> <ns0:arg2>ah2</ns0:arg2> </ns0:Request> </soap-env:Body> </soap-env:Envelope> """ assert_nodes_equal(expected, serialized.content)
def test_nested_complex_type_optional(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_1'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_2'), xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Element( '{http://tests.python-zeep.org/}item_2a1', xsd.String()), xsd.Element( '{http://tests.python-zeep.org/}item_2a2', xsd.String()), ]), xsd.Element( '{http://tests.python-zeep.org/}item_2b', xsd.String()), ]) ), min_occurs=0, max_occurs='unbounded' ) ]) )) expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2 == [] expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2/> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2 == [] expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2> <ns0:item_2a1>x</ns0:item_2a1> </ns0:item_2> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2[0].item_2a1 == 'x' assert obj.item_2[0].item_2b is None expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2> <ns0:item_2a1>x</ns0:item_2a1> <ns0:item_2b/> </ns0:item_2> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2[0].item_2a1 == 'x' assert obj.item_2[0].item_2b is None
def test_serialize_nested_complex_type(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'items'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'x'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'y'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName( 'http://tests.python-zeep.org/', 'x'), xsd.String()), ]))) ])), max_occurs=2) ]))) obj = custom_type(items=[ { 'x': 'bla', 'y': { 'x': 'deep' } }, { 'x': 'foo', 'y': { 'x': 'deeper' } }, ]) assert len(obj.items) == 2 obj.items[0].x == 'bla' obj.items[0].y.x == 'deep' obj.items[1].x == 'foo' obj.items[1].y.x == 'deeper' result = serialize_object(obj) assert result == { 'items': [ { 'x': 'bla', 'y': { 'x': 'deep' } }, { 'x': 'foo', 'y': { 'x': 'deeper' } }, ] }
def test_serialize_nested_complex_type(): custom_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "authentication"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "items"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "x"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "y"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName( "http://tests.python-zeep.org/", "x", ), xsd.String(), ) ])), ), ])), max_occurs=2, ) ])), ) obj = custom_type(items=[{ "x": "bla", "y": { "x": "deep" } }, { "x": "foo", "y": { "x": "deeper" } }]) assert len(obj.items) == 2 obj.items[0].x == "bla" obj.items[0].y.x == "deep" obj.items[1].x == "foo" obj.items[1].y.x == "deeper" result = serialize_object(obj) assert result == { "items": [{ "x": "bla", "y": { "x": "deep" } }, { "x": "foo", "y": { "x": "deeper" } }] }
def test_nil_elements(): custom_type = xsd.Element( "{http://tests.python-zeep.org/}container", xsd.ComplexType( xsd.Sequence([ xsd.Element( "{http://tests.python-zeep.org/}item_1", xsd.ComplexType( xsd.Sequence([ xsd.Element( "{http://tests.python-zeep.org/}item_1_1", xsd.String(), ) ])), nillable=True, ), xsd.Element( "{http://tests.python-zeep.org/}item_2", xsd.DateTime(), nillable=True, ), xsd.Element( "{http://tests.python-zeep.org/}item_3", xsd.String(), min_occurs=0, nillable=False, ), xsd.Element( "{http://tests.python-zeep.org/}item_4", xsd.ComplexType( xsd.Sequence([ xsd.Element( "{http://tests.python-zeep.org/}item_4_1", xsd.String(), nillable=True, ) ])), ), xsd.Element( "{http://tests.python-zeep.org/}item_5", xsd.ComplexType( xsd.Sequence([ xsd.Element( "{http://tests.python-zeep.org/}item_5_1", xsd.String(), min_occurs=1, nillable=False, ) ], min_occurs=0)), ), ])), ) obj = custom_type(item_1=None, item_2=None, item_3=None, item_4={}, item_5=xsd.Nil) expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> <ns0:item_2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> <ns0:item_4> <ns0:item_4_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </ns0:item_4> <ns0:item_5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </ns0:container> </document> """ node = render_node(custom_type, obj) etree.cleanup_namespaces(node) assert_nodes_equal(expected, node)
authClient = Client( 'http://dlw-ecmdemo.chinaeast.cloudapp.chinacloudapi.cn:8080/cws/services/Authentication?wsdl' ) print('Authenticating User...') token = authClient.service.AuthenticateUser('*****@*****.**', 'Ecm1234!') print('SUCCESS!') print('token = ' + token) docManClient = Client( 'http://dlw-ecmdemo.chinaeast.cloudapp.chinacloudapi.cn:8080/cws/services/DocumentManagement?wsdl' ) header = xsd.Element( '{urn:api.ecm.opentext.com}OTAuthentication', xsd.ComplexType([ xsd.Element('{urn:api.ecm.opentext.com}AuthenticationToken', xsd.String()), ])) header_value = header(AuthenticationToken=token) #result = docManClient.seAuthenticationTokenrvice.GetNode('2000', _soapheaders=[header_value]) print('Generating context ID...') nodeID = 690852 result = docManClient.service.GetVersionContentsContext( nodeID, 0, _soapheaders=[header_value]) #print(result) contextID = result.__getitem__('body').__getitem__( 'GetVersionContentsContextResult') print('SUCCESS!') print('contextID = ' + contextID) contentServiceClient = Client(
def __call__(self, request): """Makes a SoftLayer API call against the SOAP endpoint. :param request request: Request object """ zeep_settings = Settings(strict=False, xml_huge_tree=True) zeep_transport = Transport(cache=SqliteCache(timeout=86400)) client = Client(f"{self.endpoint_url}/{request.service}?wsdl", settings=zeep_settings, transport=zeep_transport, plugins=[self.history]) # print(client.wsdl.dump()) # print("=============== WSDL ==============") # Must define headers like this because otherwise the objectMask header doesn't work # because it isn't sent in with a namespace. xsd_userauth = xsd.Element( f"{{{self.soapns}}}authenticate", xsd.ComplexType([ xsd.Element(f'{{{self.soapns}}}username', xsd.String()), xsd.Element(f'{{{self.soapns}}}apiKey', xsd.String()) ])) # factory = client.type_factory(f"{self.soapns}") the_mask = client.get_type(f"{{{self.soapns}}}SoftLayer_ObjectMask") xsd_mask = xsd.Element(f"{{{self.soapns}}}SoftLayer_ObjectMask", the_mask) # Object Filter filter_type = client.get_type( f"{{{self.soapns}}}{request.service}ObjectFilter") xsd_filter = xsd.Element( f"{{{self.soapns}}}{request.service}ObjectFilter", filter_type) # Result Limit xsd_resultlimit = xsd.Element( f"{{{self.soapns}}}resultLimit", xsd.ComplexType([ xsd.Element('limit', xsd.String()), xsd.Element('offset', xsd.String()), ])) # Might one day want to support unauthenticated requests, but for now assume user auth. headers = [ xsd_userauth(username=request.transport_user, apiKey=request.transport_password), ] if request.limit: headers.append( xsd_resultlimit(limit=request.limit, offset=request.offset)) if request.mask: headers.append(xsd_mask(mask=request.mask)) if request.filter: # The ** here forces python to treat this dict as properties headers.append(xsd_filter(**request.filter)) if request.identifier: init_param = f"{request.service}InitParameters" init_paramtype = client.get_type(f"{{{self.soapns}}}{init_param}") xsdinit_param = xsd.Element(f"{{{self.soapns}}}{init_param}", init_paramtype) # Might want to check if its an id or globalIdentifier at some point, for now only id. headers.append(xsdinit_param(id=request.identifier)) # NEXT Add params... maybe try: method = getattr(client.service, request.method) except AttributeError as ex: message = f"{request.service}::{request.method}() does not exist in {self.soapns}{request.service}?wsdl" raise exceptions.TransportError(404, message) from ex result = method(_soapheaders=headers) # NEXT GET A WAY TO FIND TOTAL ITEMS try: method_return = f"{request.method}Return" serialize = serialize_object(result) if serialize.get('body'): return serialize['body'][method_return] else: # Some responses (like SoftLayer_Account::getObject) don't have a body? return serialize except KeyError as ex: message = f"Error serializeing response\n{result}\n{ex}" raise exceptions.TransportError(500, message)
def test_serialize_with_headers_simple(): wsdl_content = StringIO(""" <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://tests.python-zeep.org/tns" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://tests.python-zeep.org/tns"> <types> <xsd:schema targetNamespace="http://tests.python-zeep.org/tns" elementFormDefault="qualified"> <xsd:element name="Request"> <xsd:complexType> <xsd:sequence> <xsd:element name="arg1" type="xsd:string"/> <xsd:element name="arg2" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Authentication"> <xsd:complexType> <xsd:sequence> <xsd:element name="username" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </types> <message name="Input"> <part element="tns:Request"/> <part element="tns:Authentication" name="Header"/> </message> <portType name="TestPortType"> <operation name="TestOperation"> <input message="Input"/> </operation> </portType> <binding name="TestBinding" type="tns:TestPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="TestOperation"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> <soap:header message="tns:Input" part="Header" use="literal"/> </input> </operation> </binding> </definitions> """.strip()) root = wsdl.Document(wsdl_content, None) binding = root.bindings["{http://tests.python-zeep.org/tns}TestBinding"] operation = binding.get("TestOperation") header = xsd.ComplexType( xsd.Sequence([ xsd.Element("{http://www.w3.org/2005/08/addressing}Action", xsd.String()), xsd.Element("{http://www.w3.org/2005/08/addressing}To", xsd.String()), ])) header_value = header(Action="doehet", To="server") serialized = operation.input.serialize(arg1="ah1", arg2="ah2", _soapheaders=[header_value]) expected = """ <?xml version="1.0"?> <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Header> <ns0:Action xmlns:ns0="http://www.w3.org/2005/08/addressing">doehet</ns0:Action> <ns1:To xmlns:ns1="http://www.w3.org/2005/08/addressing">server</ns1:To> </soap-env:Header> <soap-env:Body> <ns0:Request xmlns:ns0="http://tests.python-zeep.org/tns"> <ns0:arg1>ah1</ns0:arg1> <ns0:arg2>ah2</ns0:arg2> </ns0:Request> </soap-env:Body> </soap-env:Envelope> """ assert_nodes_equal(expected, serialized.content)