def _get_multipart(self, url):
"""
Issues a GET request for a multipart/mixed response
and returns a dictionary of filename/data from the response.
:param url: The URL being GET from.
"""
log.debug('GET from %s...', url)
headers = {
'Authorization': self._access_key,
"Accept": "multipart/mixed",
'Accept-Encoding': 'base64'
}
response = requests.get(url=url,
headers=headers)
try:
response.raise_for_status()
log.debug('GET %s results:\n%s', url, response.text)
# combine response's headers/response so its parsable together
header_list = ["{}: {}".format(key, response.headers[key])
for key in response.headers]
header_string = "\r\n".join(header_list)
message = "\r\n\r\n".join([header_string, response.text])
# email is kind of a misnomer for the package,
# it includes a lot of utilities and we're using
# it here to parse the multipart response,
# which the requests lib doesn't help with very much
parsed_message = email.message_from_string(message)
# create a filename/data dictionary
response = {}
for part in parsed_message.walk():
# make sure this part is a file
file_header = part.get_filename(failobj=None)
if file_header:
filename = unquote(file_header)
response[filename] = part.get_payload(decode=True)
return response
except requests.exceptions.HTTPError as e:
_handle_and_raise(response, e)