From 820eb65f2673dff4035b60d5475802af75a85158 Mon Sep 17 00:00:00 2001 From: Christopher Li Date: Sat, 27 Aug 2016 17:49:46 -0600 Subject: [PATCH 1/2] Refactored attachments(...) in sendgrid/helpers/inbound/parse.py, docstrings under func. definitions --- sendgrid/helpers/inbound/parse.py | 83 ++++++++++++++++--------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/sendgrid/helpers/inbound/parse.py b/sendgrid/helpers/inbound/parse.py index 41a2527a1..2e054d5a4 100644 --- a/sendgrid/helpers/inbound/parse.py +++ b/sendgrid/helpers/inbound/parse.py @@ -13,63 +13,68 @@ 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.payload, 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(self.payload) + return attachments + + def _get_attachments(self, payload, request): + 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, payload): + 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): From c964068e46fd64b4871d9c54403c2a3b3c73ec45 Mon Sep 17 00:00:00 2001 From: Christopher Li Date: Sat, 27 Aug 2016 18:23:27 -0600 Subject: [PATCH 2/2] Fixed some errors with the refactoring --- sendgrid/helpers/inbound/parse.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sendgrid/helpers/inbound/parse.py b/sendgrid/helpers/inbound/parse.py index 2e054d5a4..ddd6ec2bb 100644 --- a/sendgrid/helpers/inbound/parse.py +++ b/sendgrid/helpers/inbound/parse.py @@ -38,14 +38,15 @@ def attachments(self): contents = base64 encoded file contents""" attachments = None if 'attachment-info' in self.payload: - attachments = self._get_attachments(self.payload, self.request) + attachments = self._get_attachments(self.request) # Check if we have a raw message raw_email = self.get_raw_email() if raw_email is not None: - attachments = self._get_attachments_raw(self.payload) + attachments = self._get_attachments_raw(raw_email) return attachments - def _get_attachments(self, payload, request): + def _get_attachments(self, request): + attachments = [] for _, filestorage in request.files.iteritems(): attachment = {} if filestorage.filename not in (None, 'fdopen', ''): @@ -56,7 +57,7 @@ def _get_attachments(self, payload, request): attachments.append(attachment) return attachments - def _get_attachments_raw(self, payload): + def _get_attachments_raw(self, raw_email): attachments = [] counter = 1 for part in raw_email.walk():