Skip to content

Instantly share code, notes, and snippets.

@iferminm
Created August 19, 2013 02:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iferminm/6265400 to your computer and use it in GitHub Desktop.
Save iferminm/6265400 to your computer and use it in GitHub Desktop.
#coding: utf-8
from suds.transport import Request
import re
import uuid
def with_soap_attachment(suds_method, attachment_data, soap_location, *args, **kwargs):
MIME_DEFAULT = 'text/plain'
attachment_transfer_encoding = 'binary'
soap_method = suds_method.method
if len(attachment_data) == 3:
data, attachment_id, attachment_mimetype = attachment_data
elif len(attachment_data) == 2:
data, attachment_mimetype = attachment_data
attachment_id = uuid.uuid4()
elif len(attachment_data) == 1:
data = attachment_data
attachment_mimetype = MIME_DEFAULT
attachment_id = uuid.uuid4()
soap_client = suds_method.clientclass(kwargs)
binding = soap_method.binding.input
soap_xml = binding.get_message(soap_method, args, kwargs)
boundary_id = 'uuid:%s' % uuid.uuid4()
root_part_id ='uuid:%s' % uuid.uuid4()
request_headers = {
'Content-Type': '; '.join([
'multipart/related',
'type="text/xml"',
'start="<%s>"' % root_part_id,
'boundary="%s"' % boundary_id,
]),
}
soap_headers = '\n'.join([
'Content-Type: text/xml; charset=UTF-8',
'Content-Transfer-Encoding: 8bit',
'Content-Id: <%s>' % root_part_id,
'',
])
attachment_headers = '\n'.join([
'Content-Type: %s' % attachment_mimetype,
'Content-Transfer-Encoding: %s' % attachment_transfer_encoding,
'Content-Id: <%s>' % attachment_id,
'',
])
request_text = '\n'.join([
'',
'--%s' % boundary_id,
soap_headers,
str(soap_xml),
'--%s' % boundary_id,
attachment_headers,
data,
'--%s--' % boundary_id
])
location = soap_location
headers = suds_method.client.options.headers.copy()
headers.update(request_headers)
request = Request(location, request_text)
request.headers = headers
response = suds_method.client.options.transport.send(request)
return response
def with_soap_attachment_get(suds_method, soap_location, *args, **kwargs):
soap_method = suds_method.method
soap_client = suds_method.clientclass(kwargs)
binding = soap_method.binding.input
soap_xml = binding.get_message(soap_method, args, kwargs)
boundary_id = 'uuid:%s' % uuid.uuid4()
root_part_id ='uuid:%s' % uuid.uuid4()
request_headers = {
'Content-Type': '; '.join([
'multipart/related',
'type="text/xml"',
'start="<%s>"' % root_part_id,
'boundary="%s"' % boundary_id,
]),
}
soap_headers = '\n'.join([
'Content-Type: text/xml; charset=UTF-8',
'Content-Transfer-Encoding: 8bit',
'Content-Id: <%s>' % root_part_id,
'',
])
request_text = '\n'.join([
'',
'--%s' % boundary_id,
soap_headers,
str(soap_xml),
'--%s' % boundary_id,
])
location = soap_location
headers = suds_method.client.options.headers.copy()
headers.update(request_headers)
request = Request(location, request_text)
request.headers = headers
response = suds_method.client.options.transport.send(request)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment