I would like to interrogate a WSDL using SUDS to get the parameters and attributes of a web service. I'm pretty much down to this one last thing. How do I interrogate the service to find the minOccurs and maxOccurs values of the parameters?
I see there's a property in the suds.xsd.sxbase object called required, but, assuming my starting point is the client object, I don't see path to get to it.
http://jortel.fedorapeople.org/suds/doc/suds.xsd.sxbase-pysrc.html#SchemaObject.required
client = Client(endpoint, username=username, password=password)
client.service[0][method]
How can I find out if a parameter is bound?
Thanks!
you can query the factory resolver for the method, and use the children() method to see its parameters.
example, for this method I have my wsdl:
<complexType name="AddAuthorizationRoleRequestType">
<sequence>
<element name="_this" type="vim25:ManagedObjectReference" />
<element name="name" type="xsd:string" />
<element name="privIds" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</sequence>
</complexType>
I can get the attributes via:
>>> a=client.factory.resolver.find("ns0:AddAuthorizationRoleRequestType")
>>> priv_el=a.children()[2][0]
<Element:0x107591a10 name="privIds" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
>>> priv_el = a.children()[2][0]
>>> priv_el.max
unbounded
>>> priv_el.min
0
not very elegant, but it works
Related
I am trying to call "Get_Purchase_Orders" operation in python and it throws below error when response is received
TypeError error in Get_Purchase_Orders : {urn:com.workday/bsvc}Bill_To_Address_ReferenceType() got an unexpected keyword argument 'Address_Reference'. Signature: `({Bill_To_Address_Reference: {urn:com.workday/bsvc}Unique_IdentifierObjectType} | {Address_Reference: {urn:com.workday/bsvc}Address_ReferenceType[]}) Unexpected error: <class 'TypeError'>
the WSDL file is accessible here
My Findings:
Bill_To_Address_Data has two elements (Bill_To_Address_Reference and Address_Reference) that are mutually exclusive, meaning only one out of two elements are expected (there is choice for Bill_To_Address_Reference Address_Reference and both tags are coming in response ). Sample XML can be seen here.
xml chunk can be seen below as well
<bsvc:Bill_To_Address_Data>
<!-- You have a CHOICE of the next 2 items at this level -->
<!-- Optional: -->
<bsvc:Bill_To_Address_Reference bsvc:Descriptor="string">
<!-- Zero or more repetitions: -->
<bsvc:ID bsvc:type="string">string</bsvc:ID>
</bsvc:Bill_To_Address_Reference>
<!-- Zero or more repetitions: -->
<bsvc:Address_Reference>
<!-- Optional: -->
<bsvc:ID>string</bsvc:ID>
</bsvc:Address_Reference>
</bsvc:Bill_To_Address_Data>
below is xsd chunk for above xml
<xsd:complexType name="Bill_To_Address_ReferenceType">
<xsd:annotation>
<xsd:documentation>Contains a reference instance or a Address Reference ID for an existing address</xsd:documentation>
<xsd:appinfo>
<wd:Validation>
<wd:Validation_Message>The Provided Bill To Address is Invalid for this Purchase Order</wd:Validation_Message>
</wd:Validation>
<wd:Validation>
<wd:Validation_Message>The Provided Bill To Address is Invalid for this Purchase Order</wd:Validation_Message>
</wd:Validation>
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:choice>
<xsd:element name="Bill_To_Address_Reference" type="wd:Unique_IdentifierObjectType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Reference to an existing Ship-To address.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="Address_Reference" type="wd:Address_ReferenceType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>Address Reference ID</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
I confirmed this in oxygen when validating XML against the XSD in WSDL or can be accessed here
Now what I want is to ignore this error and parse the response in python using zeep.
Any help will be highly appreciated.
Your choices are:
Modify the WSDL (the XML schema part) so that both tags are allowed in the same request
Find a setting in Zeep that allows you to switch off XSD validation
Stop using Zeep, and find another tool that allows you to parse a request without validating against the WSDL
Option 1 is best because WSDL is supposed to be a contract between the service and its callers. If you don't validate then the value of using WSDL is greatly reduced.
WSDL defines an element as follows
<xs:element minOccurs="0" name="address" nillable="true" type="q146:Address"/>
My zeep request is as follows
client.service.UpdateAddressDetails(address='sample#sample.com')
But I am getting
Missing element type
(UpdateAddressDetails.address.type)
From what i know i need to specify the type for this field. How can I do it,
I have come across this zeep documentation but nothing clicked
Have you tried with uppercase 'A' in your address-parameter:
client.service.UpdateAddressDetails(Address='sample#sample.com')
Use
factory = client.type_factory('q146')
address = factory.Address(address='sample#sample.com')
client.service.UpdateAddressDetails(address=address)
I'm trying to use spyne to implement a SOAP service in Python. My client sends SOAP requests like this:
<ns1:loadServices xmlns:ns1="dummy">
<serviceParams xmlns="dummy">
<header>
<user>foo</user>
<password>secret</password>
</header>
</serviceParams>
</ns1:loadServices>
But I have difficulties putting that structure into a spyne model.
So far I came up with this code:
class Header(ComplexModel):
__type_name__ = 'header'
user = Unicode
password = Unicode
class serviceParams(ComplexModel):
__type_name__ = 'serviceParams'
header = Header()
class DummyService(ServiceBase):
#rpc(serviceParams, _returns=Unicode)
def loadServices(ctx, serviceParams):
return '42'
The problem is that spyne generates and XSD like this:
...
<xs:complexType name="loadServices">
<xs:sequence>
<xs:element name="serviceParams" type="tns:serviceParams" minOccurs="0" nillable="true"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="serviceParams"/>
...
which is not what I want because essentially it says that "serviceParams" is just an empty tag without children.
Is that a bug in spyne? Or am I missing something?
It turned out that this line was the culprit:
header = Header()
that should be:
header = Header
Very nasty behavior and really easy to overlook.
I'm useing generation data structures from XML Schema. There is a part of xsd file, which descibe TCPInterface class:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsd:schema targetNamespace="tcpinterface_xsd.xsd"
xmlns:cext="tcpinterface_xsd.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:element name="TCPInterface">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="port" type="xsd:integer"/>
....
How i can set a default value for "port" element in this xsd file ?
Just add the attribute default="{yourInteger}" to the element. See an example below :
<xsd:element name="port" type="xsd:integer" default="2"/>
Note that, in this case, if your port element is empty before validation, the XML Infoset change after validation and become the post schema validation infoset (PSVI) with the default value assigned to port element.
<xsd:element name="port" type="xsd:integer" default="1" />
I'm using python's lxml to validate xmls against a schema. I have a schema with an element:
<xs:element name="link-url" type="xs:anyURL"/>
and I test, for example, this (part of an) xml:
<a link-url="server/path"/>
I would like this test to FAIL because the link-url doesn't start with http://. I tried switching anyURI to anyURL but this results in an exception - it's not a valid tag.
Is this possible with lxml? is it possible at all with schema validation?
(I'm pretty sure xs:anyURL is not valid. The XML Schema standard calls it anyURI. And since link-url is an attribute, shouldn't you be using xs:attribute instead of xs:element?)
You could restrict the URIs by creating a new simpleType based on it, and put a restriction on the pattern. For example,
#!/usr/bin/env python2.6
from lxml import etree
from StringIO import StringIO
schema_doc = etree.parse(StringIO('''
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="httpURL">
<xs:restriction base="xs:anyURI">
<xs:pattern value='https?://.+'/>
<!-- accepts only http:// or https:// URIs. -->
</xs:restriction>
</xs:simpleType>
<xs:element name="a">
<xs:complexType>
<xs:attribute name="link-url" type="httpURL"/>
</xs:complexType>
</xs:element>
</xs:schema>
''')) #/
schema = etree.XMLSchema(schema_doc)
schema.assertValid(etree.parse(StringIO('<a link-url="http://sd" />')))
assert not schema(etree.parse(StringIO('<a link-url="server/path" />')))