Skip to content

Commit 0b3b75a

Browse files
Fixes #457, refactor sengrid get method of Mail class
1 parent 35cc980 commit 0b3b75a

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed

sendgrid/helpers/mail/mail.py

+55-53
Original file line numberDiff line numberDiff line change
@@ -41,60 +41,27 @@ def get(self):
4141
:return: response body dict
4242
"""
4343
mail = {}
44-
if self.from_email is not None:
45-
mail["from"] = self.from_email.get()
46-
if self.subject is not None:
47-
mail["subject"] = self.subject
48-
49-
if self.personalizations is not None:
50-
mail["personalizations"] = [
51-
personalization.get()
52-
for personalization in self.personalizations
53-
]
54-
55-
if self.contents is not None:
56-
mail["content"] = [ob.get() for ob in self.contents]
57-
58-
if self.attachments is not None:
59-
mail["attachments"] = [ob.get() for ob in self.attachments]
60-
61-
if self.template_id is not None:
62-
mail["template_id"] = self.template_id
63-
64-
if self.sections is not None:
65-
sections = {}
66-
for key in self.sections:
67-
sections.update(key.get())
68-
mail["sections"] = sections
69-
70-
if self.headers is not None:
71-
headers = {}
72-
for key in self.headers:
73-
headers.update(key.get())
74-
mail["headers"] = headers
7544

76-
if self.categories is not None:
77-
mail["categories"] = [category.get() for category in
78-
self.categories]
45+
KEYS_TO_BE_SENT = [
46+
'subject', 'personalizations','attachments', 'template_id',
47+
'sections', 'headers', 'categories', 'custom_args',
48+
'send_at', 'batch_id', 'ip_pool_name'
49+
]
7950

80-
if self.custom_args is not None:
81-
custom_args = {}
82-
for key in self.custom_args:
83-
custom_args.update(key.get())
84-
mail["custom_args"] = custom_args
51+
for key in KEYS_TO_BE_SENT:
52+
value = getattr(self, key, None)
53+
if value is not None:
54+
mail[key] = value
8555

86-
if self.send_at is not None:
87-
mail["send_at"] = self.send_at
56+
if self.contents is not None:
57+
mail["content"] = self.contents
8858

89-
if self.batch_id is not None:
90-
mail["batch_id"] = self.batch_id
59+
if self.from_email is not None:
60+
mail["from"] = self.from_email.get()
9161

9262
if self.asm is not None:
9363
mail["asm"] = self.asm.get()
9464

95-
if self.ip_pool_name is not None:
96-
mail["ip_pool_name"] = self.ip_pool_name
97-
9865
if self.mail_settings is not None:
9966
mail["mail_settings"] = self.mail_settings.get()
10067

@@ -103,6 +70,7 @@ def get(self):
10370

10471
if self.reply_to is not None:
10572
mail["reply_to"] = self.reply_to.get()
73+
10674
return mail
10775

10876
@property
@@ -187,7 +155,14 @@ def reply_to(self, value):
187155

188156
@property
189157
def personalizations(self):
190-
return self._personalizations
158+
"""Returns personalizations as a list of dictionaries as returned
159+
by Personalization class"""
160+
if self._personalizations is not None:
161+
return [
162+
personalization.get()
163+
for personalization in self._personalizations
164+
]
165+
return None
191166

192167
def add_personalization(self, personalizations):
193168
if self._personalizations is None:
@@ -196,7 +171,11 @@ def add_personalization(self, personalizations):
196171

197172
@property
198173
def contents(self):
199-
return self._contents
174+
"""Returns contents as a list of dictionaries as returned by
175+
Content class"""
176+
if self._contents is not None:
177+
return [ob.get() for ob in self._contents]
178+
return None
200179

201180
def add_content(self, content):
202181
if self._contents is None:
@@ -205,7 +184,9 @@ def add_content(self, content):
205184

206185
@property
207186
def attachments(self):
208-
return self._attachments
187+
if self._attachments is not None:
188+
return [ob.get() for ob in self._attachments]
189+
return None
209190

210191
def add_attachment(self, attachment):
211192
if self._attachments is None:
@@ -214,7 +195,12 @@ def add_attachment(self, attachment):
214195

215196
@property
216197
def sections(self):
217-
return self._sections
198+
if self._sections is not None:
199+
sections = {}
200+
for key in self._sections:
201+
sections.update(key.get())
202+
return sections
203+
return None
218204

219205
def add_section(self, section):
220206
if self._sections is None:
@@ -223,7 +209,13 @@ def add_section(self, section):
223209

224210
@property
225211
def headers(self):
226-
return self._headers
212+
"""Returns headers of emails as dicationary"""
213+
if self._headers is not None:
214+
headers = {}
215+
for key in self._headers:
216+
headers.update(key.get())
217+
return headers
218+
return None
227219

228220
def add_header(self, header):
229221
if self._headers is None:
@@ -236,7 +228,11 @@ def add_header(self, header):
236228

237229
@property
238230
def categories(self):
239-
return self._categories
231+
"""Returns list of categories"""
232+
if self._categories is not None:
233+
return [category.get() for category in
234+
self._categories]
235+
return None
240236

241237
def add_category(self, category):
242238
if self._categories is None:
@@ -245,7 +241,13 @@ def add_category(self, category):
245241

246242
@property
247243
def custom_args(self):
248-
return self._custom_args
244+
"""Returns custom_args as a dicationary"""
245+
if self._custom_args is not None:
246+
custom_args = {}
247+
for key in self._custom_args:
248+
custom_args.update(key.get())
249+
return custom_args
250+
return None
249251

250252
def add_custom_arg(self, custom_arg):
251253
if self._custom_args is None:

0 commit comments

Comments
 (0)