Skip to content

Commit 1e2b8c0

Browse files
Merge pull request #132 from sendgrid/global-suppressions
Added ASM Global Suppressions GET method
2 parents 4f7050b + 4760ed6 commit 1e2b8c0

File tree

6 files changed

+104
-5
lines changed

6 files changed

+104
-5
lines changed

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ Delete a recipient email from the suppressions list for a group.
305305
306306
status, msg = client.asm_suppressions.delete(<group_id>,<email_address>)
307307
308+
ASM Global Suppressions
309+
~~~~~~~~~~~~~~~~~~~~~~~
310+
311+
Global Suppressions are email addresses that will not receive any emails.
312+
313+
Check if a given email is on the global suppression list.
314+
315+
.. code:: python
316+
317+
client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
318+
email = ['[email protected]']
319+
status, msg = client.asm_global_suppressions.get(email)
320+
308321
SendGrid's `X-SMTPAPI`_
309322
-----------------------
310323

example_v2_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
message.set_subject('Testing from the Python library')
1414
message.set_html('<b>This was a successful test!</b>')
1515
message.set_text('This was a successful test!')
16-
message.set_from('Elmer Thomas <elmer@thinkingserious.com>')
16+
message.set_from('Elmer Thomas <dx@sendgrid.com>')
1717
status, msg = sg.send(message)
1818
print status
1919
print msg

example_v3_test.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,30 @@
88
if len(var) == 2:
99
os.environ[var[0]] = var[1]
1010

11-
12-
1311
client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
1412

15-
status, msg = client.asm_suppressions.delete(67,'[email protected]')
13+
# In the global suppression list
14+
status, msg = client.asm_global_suppressions.get('[email protected]')
15+
print status
16+
print msg
17+
18+
# Not in the global suppression list
19+
status, msg = client.asm_global_suppressions.get('[email protected]')
1620
print status
1721
print msg
1822

1923
"""
24+
status, msg = client.apikeys.get()
25+
print status
26+
print msg
2027
21-
status, msg = client.asm_suppressions.post(60, ['[email protected]', '[email protected]'])
28+
status, msg = client.asm_suppressions.delete(67,'[email protected]')
2229
print status
2330
print msg
2431
32+
status, msg = client.asm_suppressions.post(60, ['[email protected]', '[email protected]'])
33+
print status
34+
print msg
2535
2636
status, msg = client.asm_suppressions.get(None,'[email protected]')
2737
print status

sendgrid/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .resources.api_keys import APIKeys
1515
from .resources.asm_groups import ASMGroups
1616
from .resources.asm_suppressions import ASMSuppressions
17+
from .resources.asm_global_suppressions import ASMGlobalSuppressions
1718

1819
class SendGridAPIClient(object):
1920

@@ -36,6 +37,7 @@ def __init__(self, apikey, **opts):
3637
self.apikeys = APIKeys(self)
3738
self.asm_groups = ASMGroups(self)
3839
self.asm_suppressions = ASMSuppressions(self)
40+
self.asm_global_suppressions = ASMGlobalSuppressions(self)
3941

4042
@property
4143
def apikey(self):
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class ASMGlobalSuppressions(object):
2+
"""Advanced Suppression Manager (ASM) gives your recipients more control over the types of emails they want to receive
3+
by letting them opt out of messages from a certain type of email.
4+
5+
Global Suppressions are email addresses that will not receive any emails.
6+
"""
7+
8+
def __init__(self, client, **opts):
9+
"""
10+
Constructs SendGrid ASM suppressions object.
11+
12+
See https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/index.html and
13+
https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html
14+
"""
15+
self._name = None
16+
self._base_endpoint = "/v3/asm/suppressions/global"
17+
self._endpoint = "/v3/asm/suppressions/global"
18+
self._client = client
19+
20+
@property
21+
def base_endpoint(self):
22+
return self._base_endpoint
23+
24+
@property
25+
def endpoint(self):
26+
endpoint = self._endpoint
27+
return endpoint
28+
29+
@endpoint.setter
30+
def endpoint(self, value):
31+
self._endpoint = value
32+
33+
@property
34+
def client(self):
35+
return self._client
36+
37+
# Determine if an email belongs to the global suppression group
38+
def get(self, email=None):
39+
self._endpoint = self._base_endpoint + '/' + email
40+
return self.client.get(self)

test/test_asm_global_suppressions.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from .base_test import BaseTest, MockSendGridAPIClientRequest
2+
import os
3+
try:
4+
import unittest2 as unittest
5+
except ImportError:
6+
import unittest
7+
try:
8+
from StringIO import StringIO
9+
except ImportError: # Python 3
10+
from io import StringIO
11+
12+
import sendgrid
13+
from sendgrid.client import SendGridAPIClient
14+
from sendgrid.version import __version__
15+
16+
SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY'
17+
18+
class TestASMGroups(unittest.TestCase):
19+
def setUp(self):
20+
SendGridAPIClient = MockSendGridAPIClientRequest
21+
self.client = SendGridAPIClient(SG_KEY)
22+
23+
def test_asm_global_suppressions_init(self):
24+
self.asm_global_suppressions = self.client.asm_global_suppressions
25+
self.assertEqual(self.asm_global_suppressions.base_endpoint, "/v3/asm/suppressions/global")
26+
self.assertEqual(self.asm_global_suppressions.endpoint, "/v3/asm/suppressions/global")
27+
self.assertEqual(self.asm_global_suppressions.client, self.client)
28+
29+
def test_asm_suppressions_get(self):
30+
status, msg = self.client.asm_global_suppressions.get('[email protected]')
31+
self.assertEqual(status, 200)
32+
33+
if __name__ == '__main__':
34+
unittest.main()

0 commit comments

Comments
 (0)