How do I add a factory created type as a header in suds?
I can't seem to get suds working with my setup. I have to pass a context, with a remote user set before I can use any of the functions in the API. What I tried to do was this:
client = Client(url, username=userid, password=password)
apiContext = client.factory.create("apiCallContext") # This is listed in the types
apiContext.remoteUser = "serviceAccount" # when I print the client
client.set_options(soapheaders=apiContext)
client.service.getActiveIPs()
Throughout the process, everything seems to be getting created correctly (if I print the client, I see all the functions and types, if I print apiContext, I see everything set correctly), but the headers don't actually seem to be getting set:
...
DEBUG:suds.client:sending to ($URL) message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0=$NS
xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns1:Body>
<ns0:getActiveIPs/>
</ns1:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': u'""',
'Content-Type': 'text/xml; charset=utf-8'}
DEBUG:suds.transport.http:sending:
URL:$URL
HEADERS: {'SOAPAction': u'""', 'Content-Type': 'text/xml; charset=utf-8',
'Content开发者_开发问答-type': 'text/xml; charset=utf-8', 'Soapaction': u'""'}
...
I'm not seeing the context anywhere in the headers, and the server is erroring out that there's no remote user set.
What am I doing wrong?
Without knowing the exact spec of the webservice you are working with, I can only hazard a guess at this answer, but the header looks similar to a web service I have used in the past. Have you tried creating the elements directly and passing them into the header thus?:
from suds.sax.element import Element
...
NS = ('h', SOME_NAMESPACE)
apiContext = Element('apiContext')
authcred = Element('authenticationCredential', ns=NS)
username = Element(userid, ns=NS).setText('USERNAME')
passw = Element(password, ns=NS).setText('PASSWORD')
authcred.append(username)
authcred.append(passw)
apiContext.append(authcred)
client.set_options(soapheaders=apiContext)
i.e. is the authentication part of the context object?
精彩评论