def test_any_in_nested_sequence(): schema = xsd.Schema(load_xml(""" <?xml version="1.0"?> <xsd:schema xmlns:tns="http://tests.python-zeep.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://tests.python-zeep.org/" > <xsd:element name="something" type="xsd:date"/> <xsd:element name="foobar" type="xsd:boolean"/> <xsd:element name="container"> <xsd:complexType> <xsd:sequence> <xsd:element name="items" minOccurs="0"> <xsd:complexType> <xsd:sequence> <xsd:any namespace="##any" processContents="lax"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="version" type="xsd:string"/> <xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> """)) # noqa container_elm = schema.get_element('{http://tests.python-zeep.org/}container') assert container_elm.signature(schema) == ( 'ns0:container(items: {_value_1: ANY}, version: xsd:string, _value_1: ANY[])') something = schema.get_element('{http://tests.python-zeep.org/}something') foobar = schema.get_element('{http://tests.python-zeep.org/}foobar') any_1 = xsd.AnyObject(something, datetime.date(2016, 7, 4)) any_2 = xsd.AnyObject(foobar, True) obj = container_elm( items={'_value_1': any_1}, version='str1234', _value_1=[any_1, any_2]) node = etree.Element('document') container_elm.render(node, obj) expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:items> <ns0:something>2016-07-04</ns0:something> </ns0:items> <ns0:version>str1234</ns0:version> <ns0:something>2016-07-04</ns0:something> <ns0:foobar>true</ns0:foobar> </ns0:container> </document> """ assert_nodes_equal(expected, node) item = container_elm.parse(node.getchildren()[0], schema) assert item.items._value_1 == datetime.date(2016, 7, 4) assert item.version == 'str1234' assert item._value_1 == [datetime.date(2016, 7, 4), True]
def create_xml_soap_map(values): """Create an http://xml.apache.org/xml-soap#Map value.""" Map = xsd.ComplexType( xsd.Sequence([ xsd.Element( 'item', xsd.AnyType(), min_occurs=1, max_occurs="unbounded"), ]), qname=etree.QName('{http://xml.apache.org/xml-soap}Map')) KeyValueData = xsd.Element( '{http://xml.apache.org/xml-soap}KeyValueData', xsd.ComplexType( xsd.Sequence([ xsd.Element( 'key', xsd.AnyType(), ), xsd.Element( 'value', xsd.AnyType(), ), ]), ), ) return Map(item=[ KeyValueData( xsd.AnyObject(xsd.String(), key), xsd.AnyObject(guess_xsd_type(value), value) ) for key, value in values.items() ])
def query_filter(vm, field, value, query_type): query_function = query_type_mapping[query_type] if field['type'] is 'String': query_filter = vm.query_factory[query_function](Field=field['name'], Value=xsd.AnyObject( xsd.String(), value)) elif field['type'] is 'Id': query_filter = vm.query_factory[query_function]( Field=field['name'], Value=vm.type_factory.Id(value)) elif field['type'] is 'Long': query_filter = vm.query_factory[query_function](Field=field['name'], Value=xsd.AnyObject( xsd.Long(), value)) elif field['type'] is 'Boolean': query_filter = vm.query_factory[query_function](Field=field['name'], Value=xsd.AnyObject( xsd.Boolean(), value)) elif field['type'] is 'OsVersion': query_filter = vm.query_factory[query_function]( Field=field['name'], Value=xsd.AnyObject(vm.query_factory.OsVersion, value)) elif field['type'] is 'ClientState': query_filter = vm.query_factory[query_function]( Field=field['name'], Value=xsd.AnyObject(vm.query_factory.ClientState, value)) else: raise Exception("Can't determine Value type") return query_filter
def test_wsdl_array_type(): transport = DummyTransport() transport.bind( 'http://schemas.xmlsoap.org/soap/encoding/', load_xml( io.open('tests/wsdl_files/soap-enc.xsd', 'r').read().encode('utf-8'))) schema = xsd.Schema( load_xml(""" <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tests.python-zeep.org/" elementFormDefault="qualified"> <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/> <xsd:complexType name="array"> <xsd:complexContent> <xsd:restriction base="SOAP-ENC:Array"> <xsd:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="tns:base[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="base"> <xsd:sequence> <xsd:element minOccurs="0" name="item_1" type="xsd:string"/> <xsd:element minOccurs="0" name="item_2" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:element name="array" type="tns:array"/> </xsd:schema> """), transport) array_elm = schema.get_element('{http://tests.python-zeep.org/}array') item_type = schema.get_type('{http://tests.python-zeep.org/}base') item_1 = item_type(item_1='foo_1', item_2='bar_1') item_2 = item_type(item_1='foo_2', item_2='bar_2') array = array_elm([ xsd.AnyObject(item_type, item_1), xsd.AnyObject(item_type, item_2), ]) node = etree.Element('document') array_elm.render(node, array) expected = """ <document> <ns0:array xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo_1</ns0:item_1> <ns0:item_2>bar_1</ns0:item_2> <ns0:item_1>foo_2</ns0:item_1> <ns0:item_2>bar_2</ns0:item_2> </ns0:array> </document> """ 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_any_with_ref(): schema = xsd.Schema( load_xml( """ <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" targetNamespace="http://tests.python-zeep.org/" elementFormDefault="qualified"> <element name="item" type="string"/> <element name="container"> <complexType> <sequence> <element ref="tns:item"/> <any/> <any maxOccurs="unbounded"/> </sequence> </complexType> </element> </schema> """ ) ) item_elm = schema.get_element("{http://tests.python-zeep.org/}item") assert isinstance(item_elm.type, xsd.String) container_elm = schema.get_element("{http://tests.python-zeep.org/}container") obj = container_elm( item="bar", _value_1=xsd.AnyObject(item_elm, item_elm("argh")), _value_2=xsd.AnyObject(item_elm, item_elm("ok")), ) node = etree.Element("document") container_elm.render(node, obj) expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item>bar</ns0:item> <ns0:item>argh</ns0:item> <ns0:item>ok</ns0:item> </ns0:container> </document> """ assert_nodes_equal(expected, node) item = container_elm.parse(list(node)[0], schema) assert item.item == "bar" assert item._value_1 == "argh"
def test_any_without_element(): schema = xsd.Schema(load_xml(""" <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" targetNamespace="http://tests.python-zeep.org/" elementFormDefault="qualified"> <element name="item"> <complexType> <sequence> <any minOccurs="0" maxOccurs="1"/> </sequence> <attribute name="type" type="string"/> <attribute name="title" type="string"/> </complexType> </element> </schema> """)) item_elm = schema.get_element('{http://tests.python-zeep.org/}item') item = item_elm(xsd.AnyObject(xsd.String(), 'foobar'), type='attr-1', title='attr-2') node = render_node(item_elm, item) expected = """ <document> <ns0:item xmlns:ns0="http://tests.python-zeep.org/" type="attr-1" title="attr-2">foobar</ns0:item> </document> """ assert_nodes_equal(expected, node) item = item_elm.parse(node.getchildren()[0], schema) assert item.type == 'attr-1' assert item.title == 'attr-2' assert item._value_1 is None
def test_any_simple(): schema = get_any_schema() item_elm = schema.get_element('{http://tests.python-zeep.org/}item') assert isinstance(item_elm.type, xsd.String) container_elm = schema.get_element( '{http://tests.python-zeep.org/}container') # Create via arg obj = container_elm(xsd.AnyObject(item_elm, item_elm('argh'))) node = etree.Element('document') container_elm.render(node, obj) expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item>argh</ns0:item> </ns0:container> </document> """ assert_nodes_equal(expected, node) item = container_elm.parse(node.getchildren()[0], schema) assert item._value_1 == 'argh' # Create via kwarg _value_1 obj = container_elm(_value_1=xsd.AnyObject(item_elm, item_elm('argh'))) node = etree.Element('document') container_elm.render(node, obj) expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item>argh</ns0:item> </ns0:container> </document> """ assert_nodes_equal(expected, node) item = container_elm.parse(node.getchildren()[0], schema) assert item._value_1 == 'argh'
def test_choice_element_with_any_max_occurs(): schema = xsd.Schema( load_xml(""" <schema targetNamespace="http://tests.python-zeep.org/" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" elementFormDefault="qualified"> <element name="item_any" type="string"/> <element name="container"> <complexType> <sequence> <choice minOccurs="0"> <element maxOccurs="999" minOccurs="0" name="item_1" type="string"/> <sequence> <element minOccurs="0" name="item_2"/> <any maxOccurs="unbounded" minOccurs="0"/> </sequence> </choice> </sequence> </complexType> </element> </schema> """)) element = schema.get_element('ns0:container') value = element(item_2="item-2", _value_1=[ xsd.AnyObject(schema.get_element('ns0:item_any'), 'any-content') ]) expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_2>item-2</ns0:item_2> <ns0:item_any>any-content</ns0:item_any> </ns0:container> </document> """ node = render_node(element, value) assert_nodes_equal(node, expected) result = element.parse(node[0], schema) assert result.item_2 == 'item-2' assert result._value_1 == ['any-content']
def test_element_any(): node = etree.fromstring(""" <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" targetNamespace="http://tests.python-zeep.org/" elementFormDefault="qualified"> <element name="item" type="string"/> <element name="container"> <complexType> <sequence> <element ref="tns:item"/> <any/> <any maxOccurs="unbounded"/> </sequence> </complexType> </element> </schema> """.strip()) schema = xsd.Schema(node) item_elm = schema.get_element('{http://tests.python-zeep.org/}item') assert isinstance(item_elm.type, xsd.String) container_elm = schema.get_element( '{http://tests.python-zeep.org/}container') obj = container_elm(item='bar', _value_1=xsd.AnyObject(item_elm, item_elm('argh'))) node = etree.Element('document') container_elm.render(node, obj) expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item>bar</ns0:item> <ns0:item>argh</ns0:item> </ns0:container> </document> """ assert_nodes_equal(expected, node) item = container_elm.parse(node.getchildren()[0], schema) assert item.item == 'bar' assert item._value_1 == 'argh'
def test_element_any(): node = etree.fromstring(""" <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" targetNamespace="http://tests.python-zeep.org/" elementFormDefault="qualified"> <element name="foo" type="string"/> <element name="bar"> <complexType> <sequence> <element ref="tns:foo"/> <any/> <any maxOccurs="unbounded"/> </sequence> </complexType> </element> </schema> """.strip()) schema = xsd.Schema(node) foo_type = schema.get_element('{http://tests.python-zeep.org/}foo') assert isinstance(foo_type.type, xsd.String) custom_type = schema.get_element('{http://tests.python-zeep.org/}bar') foo = schema.get_element('{http://tests.python-zeep.org/}foo') obj = custom_type(foo='bar', _any_1=xsd.AnyObject(foo, foo('argh'))) node = etree.Element('document') custom_type.render(node, obj) expected = """ <document> <ns0:bar xmlns:ns0="http://tests.python-zeep.org/"> <ns0:foo>bar</ns0:foo> <ns0:foo>argh</ns0:foo> </ns0:bar> </document> """ assert_nodes_equal(expected, node)
def build(self): factory = self.factory val = xsd.AnyObject(self.content._xsd_elm, self.content) data = factory.RequestContentType( content=factory.Content( MessagePrimaryContent=val ) ) meta = factory.RequestMetadataType( clientId=str(uuid.uuid4()), createGroupIdentity=factory.CreateGroupIdentity( FRGUServiceCode='00000000000000000000', FRGUServiceDescription='00000000000000000000', FRGUServiceRecipientDescription='00000000000000000000' ), testMessage=self.test_mode ) msg = factory.RequestMessageType( RequestMetadata=meta, RequestContent=data ) return msg
def test_serialize_any_type(): 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:anyType"/> </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") serialized = operation.input.serialize( arg1=xsd.AnyObject(xsd.String(), "ah1")) expected = """ <?xml version="1.0"?> <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Body> <ns0:Request xmlns:ns0="http://tests.python-zeep.org/tns"> <ns0:arg1 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">ah1</ns0:arg1> </ns0:Request> </soap-env:Body> </soap-env:Envelope> """ assert_nodes_equal(expected, serialized.content) deserialized = operation.input.deserialize(serialized.content) assert deserialized == "ah1"
def Nil(): """Return an xsi:nil element""" return xsd.AnyObject(None, None)
and (f.IsRequired or f.Name in new_entry.keys()): deal_fields[f.Name] = f elif f.EntryListId == attachment_list.Id and f.IsRequired: attachment_fields[f.Name] = f else: continue # Create a type factory to access the types provided by the service factory = client.type_factory('ns0') # Create an array for your DCPushes push_requests = factory.ArrayOfDCPush() # Create DCPushes for all of the Attachment fields filename = new_entry['attachment'] filename_str = xsd.AnyObject(xsd.String(), filename) push_filename = factory.DCPush(EntryId=new_attachment_id, FieldId=attachment_fields['Title'].Id, Value=filename_str) extension = xsd.AnyObject(xsd.String(), filename.split('.')[-1]) push_extension = factory.DCPush(EntryId=new_attachment_id, FieldId=attachment_fields['File Type'].Id, Value=extension) with open(filename, 'rb') as f: content = xsd.AnyObject(xsd.Base64Binary(), f.read()) content_type = xsd.AnyObject(xsd.String(), 'application/pdf') push_document = factory.DCPushBinary(EntryId=new_attachment_id, FieldId=attachment_fields['Document'].Id, Value=content,
def test_xml_complex_any_types(): # see https://github.com/mvantellingen/python-zeep/issues/252 schema = xsd.Schema( load_xml(""" <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" targetNamespace="http://tests.python-zeep.org/" elementFormDefault="qualified"> <element name="container"> <complexType> <sequence> <element minOccurs="0" maxOccurs="1" name="auth" type="anyType" /> <element minOccurs="0" maxOccurs="1" name="params" type="anyType" /> </sequence> </complexType> </element> </schema> """)) schema.set_ns_prefix('tns', 'http://tests.python-zeep.org/') KeyValueData = xsd.Element( '{http://xml.apache.org/xml-soap}KeyValueData', xsd.ComplexType( xsd.Sequence([ xsd.Element( 'key', xsd.AnyType(), ), xsd.Element( 'value', xsd.AnyType(), ), ]), ), ) Map = xsd.ComplexType( xsd.Sequence([ xsd.Element('item', xsd.AnyType(), min_occurs=1, max_occurs="unbounded"), ]), qname=etree.QName('{http://xml.apache.org/xml-soap}Map')) header_Username = KeyValueData(xsd.AnyObject(xsd.String(), 'Username'), value=xsd.AnyObject(xsd.String(), 'abc')) header_ShopId = KeyValueData(xsd.AnyObject(xsd.String(), 'ShopId'), value=xsd.AnyObject(xsd.Int(), 123)) auth = Map(item=[header_Username, header_ShopId]) header_LimitNum = KeyValueData(xsd.AnyObject(xsd.String(), 'LimitNum'), value=xsd.AnyObject(xsd.Int(), 2)) params = Map(item=[header_LimitNum]) container = schema.get_element('ns0:container') obj = container(auth=auth, params=params) result = render_node(container, obj) expected = load_xml(""" <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:auth xmlns:ns1="http://xml.apache.org/xml-soap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns1:Map"> <item> <key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Username</key> <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">abc</value> </item> <item> <key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">ShopId</key> <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">123</value> </item> </ns0:auth> <ns0:params xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:Map"> <item> <key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">LimitNum</key> <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">2</value> </item> </ns0:params> </ns0:container> </document> """) # noqa assert_nodes_equal(result, expected)
def call_soap_method(self, client, method, params={}): """Calls a method on the connected webservice ----- Param *client : Zeep Client* The Client returned from the Create Soap Client method ----- Param *method : string* The name of the method to call ------ Param *params : list [Empty]* A list of parameters to pass to the method which must be specified in the order that the webservice is expecting them ------ Return *result* An object determined to be the result of the call to the webservice ------ | A simple example | | | Create Soap Client | http://some-web-url.com/webservice?wsdl | | | ${answer}= | Call Soap Method | getTheAnswer | """ # convert any custom types into the correct zeep type params_dict = {} for key in params: param = params[key] if type(params[key]).__name__ == "ArrayOfStrings": # convert to ArrayOfStrings aos = client.get_type("ns0:ArrayOfString") params_dict[key] = aos(params[key]) elif type(params[key]).__name__ == "ArrayOfAny": # convert to ArrayOfAny params_dict[key] = [] for item in params[key]: params_dict[key].append(xsd.AnyObject(xsd.String(), item)) else: params_dict[key] = param print("Parameters: {0}".format(params_dict)) return client.service[method](**params_dict)
deal_field = list(filter( lambda f: f.EntryListId == deal_list.Id and f.Name == 'EBITDA', fields ))[0] except IndexError: print('Fields could not be found.') sys.exit() # Create a type factory to access the types provided by the service factory = client.type_factory('ns0') # Build the payload for your request and push it requests = factory.ArrayOfDCPush() value = xsd.AnyObject(xsd.Decimal(), 1.9) p = factory.DCPush(EntryId=deal_entry.Id, FieldId=deal_field.Id, Value=value) requests.DCPush.append(p) try: responses = service.ProcessDCPush( entryListId=deal_list.Id, requests=requests ) except zeep.exceptions.Fault: print('An error occurred with the server.') sys.exit() # Check your responses for any errors and print messages appropriately for r in responses: if r.Error is None: print(f'Field {r.FieldId} of Entry {r.EntryId} updated successfully.')
def __str_obj(self, val): return xsd.AnyObject(xsd.String(), val)