diff --git a/sendgrid/helpers/inbound/parse.py b/sendgrid/helpers/inbound/parse.py index 41a2527a1..ddd6ec2bb 100644 --- a/sendgrid/helpers/inbound/parse.py +++ b/sendgrid/helpers/inbound/parse.py @@ -13,63 +13,69 @@ def __init__(self, config, request): self._payload = request.form self._raw_payload = request.data - """Return a dictionary of key/values in the payload received from - the webhook""" def key_values(self): + """Return a dictionary of key/values in the payload received from + the webhook""" key_values = {} for key in self.keys: if key in self.payload: key_values[key] = self.payload[key] return key_values - """This only applies to raw payloads: - https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Raw-Parameters""" def get_raw_email(self): - if 'email' in self.payload: + """This only applies to raw payloads: + https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Raw-Parameters""" + if 'email' in self.payload: raw_email = email.message_from_string(self.payload['email']) return raw_email - else: + else: return None - """Returns an object with: - type = file content type - file_name = the name of the file - contents = base64 encoded file contents""" def attachments(self): - attachments = [] + """Returns an object with: + type = file content type + file_name = the name of the file + contents = base64 encoded file contents""" + attachments = None if 'attachment-info' in self.payload: - for _, filestorage in self.request.files.iteritems(): - attachment = {} - if filestorage.filename not in (None, 'fdopen', ''): - filename = secure_filename(filestorage.filename) - attachment['type'] = filestorage.content_type - attachment['file_name'] = filename - attachment['contents'] = base64.b64encode(filestorage.getvalue()) - attachments.append(attachment) - return attachments - + attachments = self._get_attachments(self.request) # Check if we have a raw message - attachments = [] raw_email = self.get_raw_email() if raw_email is not None: - counter = 1 - for part in raw_email.walk(): - attachment = {} - if part.get_content_maintype() == 'multipart': - continue - filename = part.get_filename() - if not filename: - ext = mimetypes.guess_extension(part.get_content_type()) - if not ext: - ext = '.bin' - filename = 'part-%03d%s' % (counter, ext) - counter += 1 - attachment['type'] = part.get_content_type() - attachment['filename'] = filename - attachment['contents'] = part.get_payload(decode=False) + attachments = self._get_attachments_raw(raw_email) + return attachments + + def _get_attachments(self, request): + attachments = [] + for _, filestorage in request.files.iteritems(): + attachment = {} + if filestorage.filename not in (None, 'fdopen', ''): + filename = secure_filename(filestorage.filename) + attachment['type'] = filestorage.content_type + attachment['file_name'] = filename + attachment['contents'] = base64.b64encode(filestorage.getvalue()) attachments.append(attachment) - return attachments - return None + return attachments + + def _get_attachments_raw(self, raw_email): + attachments = [] + counter = 1 + for part in raw_email.walk(): + attachment = {} + if part.get_content_maintype() == 'multipart': + continue + filename = part.get_filename() + if not filename: + ext = mimetypes.guess_extension(part.get_content_type()) + if not ext: + ext = '.bin' + filename = 'part-%03d%s' % (counter, ext) + counter += 1 + attachment['type'] = part.get_content_type() + attachment['filename'] = filename + attachment['contents'] = part.get_payload(decode=False) + attachments.append(attachment) + return attachments @property def keys(self):