Last active
August 29, 2015 14:03
-
-
Save barkady/e6b748a38e58458a551f to your computer and use it in GitHub Desktop.
get SOAP with suds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def GetWithFile(self, client, servicename, modelthings): | |
func = client.service.__getattr__('Retrieve' + servicename) | |
clientclass = func.clientclass({}) | |
SoapClient = clientclass(func.client, func.method) | |
binding = func.method.binding.input | |
soap_xml = binding.get_message(func.method, [modelthings], {}) | |
#changing SOAP request to receive attachments. May be used different way in other SOAP | |
soap_xml.children[0].children[1].children[0].attributes.append(u'attachmentInfo="true"') | |
soap_xml.children[0].children[1].children[0].attributes.append(u'attachmentData="true"') | |
soap_xml.children[0].children[1].children[0].attributes.append(u'ignoreEmptyElements="true"') | |
SoapClient.last_sent(soap_xml) | |
plugins = PluginContainer(SoapClient.options.plugins) | |
plugins.message.marshalled(envelope=soap_xml.root()) | |
if SoapClient.options.prettyxml: | |
soap_xml = soap_xml.str() | |
else: | |
soap_xml = soap_xml.plain() | |
soap_xml = soap_xml.encode('utf-8') | |
plugins.message.sending(envelope=soap_xml) | |
request = Request(SoapClient.location(), soap_xml) | |
request.headers = SoapClient.headers() | |
reply = SoapClient.options.transport.send(request) | |
print(reply) | |
Files = [] | |
boundary = self.find_substring(reply.headers['content-type'], 'boundary="', '"') | |
if boundary is not "": | |
list_of_data = reply.message.split(boundary) | |
list_of_data.pop(0) | |
list_of_data.pop(len(list_of_data) - 1) | |
soap_body = '<SOAP-ENV:Envelope' + self.find_substring(list_of_data[0], '<SOAP-ENV:Envelope', '</SOAP-ENV:Envelope>') + '</SOAP-ENV:Envelope>' | |
for line in list_of_data[1:]: | |
File = SMFile() | |
Files.append(File) | |
File.filename = self.find_substring(line, 'Content-Location: ', '\r\n') | |
File.key = self.find_substring(line, 'Content-ID: ', '\r\n') | |
idx = line.index( 'Content-ID:' ) | |
start_idx = line.index( '\r\n\r\n' , idx ) + len('\r\n\r\n') | |
fin_idx = line.rindex( '\r\n--', start_idx ) | |
File.body = line[start_idx: fin_idx] | |
File.size = fin_idx - start_idx | |
else: | |
soap_body = '<SOAP-ENV:Envelope' + self.find_substring(reply.message, '<SOAP-ENV:Envelope', '</SOAP-ENV:Envelope>') + '</SOAP-ENV:Envelope>' | |
ctx = plugins.message.received(reply=soap_body) | |
soap_body = ctx.reply | |
if SoapClient.options.retxml: | |
answer = soap_body | |
else: | |
answer = SoapClient.succeeded(binding, soap_body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment