Skip to content

Commit 820eb65

Browse files
Refactored attachments(...) in sendgrid/helpers/inbound/parse.py, docstrings under func. definitions
1 parent 62976a6 commit 820eb65

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

sendgrid/helpers/inbound/parse.py

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,63 +13,68 @@ def __init__(self, config, request):
1313
self._payload = request.form
1414
self._raw_payload = request.data
1515

16-
"""Return a dictionary of key/values in the payload received from
17-
the webhook"""
1816
def key_values(self):
17+
"""Return a dictionary of key/values in the payload received from
18+
the webhook"""
1919
key_values = {}
2020
for key in self.keys:
2121
if key in self.payload:
2222
key_values[key] = self.payload[key]
2323
return key_values
2424

25-
"""This only applies to raw payloads:
26-
https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Raw-Parameters"""
2725
def get_raw_email(self):
28-
if 'email' in self.payload:
26+
"""This only applies to raw payloads:
27+
https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Raw-Parameters"""
28+
if 'email' in self.payload:
2929
raw_email = email.message_from_string(self.payload['email'])
3030
return raw_email
31-
else:
31+
else:
3232
return None
3333

34-
"""Returns an object with:
35-
type = file content type
36-
file_name = the name of the file
37-
contents = base64 encoded file contents"""
3834
def attachments(self):
39-
attachments = []
35+
"""Returns an object with:
36+
type = file content type
37+
file_name = the name of the file
38+
contents = base64 encoded file contents"""
39+
attachments = None
4040
if 'attachment-info' in self.payload:
41-
for _, filestorage in self.request.files.iteritems():
42-
attachment = {}
43-
if filestorage.filename not in (None, 'fdopen', '<fdopen>'):
44-
filename = secure_filename(filestorage.filename)
45-
attachment['type'] = filestorage.content_type
46-
attachment['file_name'] = filename
47-
attachment['contents'] = base64.b64encode(filestorage.getvalue())
48-
attachments.append(attachment)
49-
return attachments
50-
41+
attachments = self._get_attachments(self.payload, self.request)
5142
# Check if we have a raw message
52-
attachments = []
5343
raw_email = self.get_raw_email()
5444
if raw_email is not None:
55-
counter = 1
56-
for part in raw_email.walk():
57-
attachment = {}
58-
if part.get_content_maintype() == 'multipart':
59-
continue
60-
filename = part.get_filename()
61-
if not filename:
62-
ext = mimetypes.guess_extension(part.get_content_type())
63-
if not ext:
64-
ext = '.bin'
65-
filename = 'part-%03d%s' % (counter, ext)
66-
counter += 1
67-
attachment['type'] = part.get_content_type()
68-
attachment['filename'] = filename
69-
attachment['contents'] = part.get_payload(decode=False)
45+
attachments = self._get_attachments_raw(self.payload)
46+
return attachments
47+
48+
def _get_attachments(self, payload, request):
49+
for _, filestorage in request.files.iteritems():
50+
attachment = {}
51+
if filestorage.filename not in (None, 'fdopen', '<fdopen>'):
52+
filename = secure_filename(filestorage.filename)
53+
attachment['type'] = filestorage.content_type
54+
attachment['file_name'] = filename
55+
attachment['contents'] = base64.b64encode(filestorage.getvalue())
7056
attachments.append(attachment)
71-
return attachments
72-
return None
57+
return attachments
58+
59+
def _get_attachments_raw(self, payload):
60+
attachments = []
61+
counter = 1
62+
for part in raw_email.walk():
63+
attachment = {}
64+
if part.get_content_maintype() == 'multipart':
65+
continue
66+
filename = part.get_filename()
67+
if not filename:
68+
ext = mimetypes.guess_extension(part.get_content_type())
69+
if not ext:
70+
ext = '.bin'
71+
filename = 'part-%03d%s' % (counter, ext)
72+
counter += 1
73+
attachment['type'] = part.get_content_type()
74+
attachment['filename'] = filename
75+
attachment['contents'] = part.get_payload(decode=False)
76+
attachments.append(attachment)
77+
return attachments
7378

7479
@property
7580
def keys(self):

0 commit comments

Comments
 (0)