Skip to content

Commit cf3c155

Browse files
Merge pull request #124 from thinkingserious/asm_suppressions_post
ASM suppressions [POST]
2 parents ceb2763 + 0d8c301 commit cf3c155

File tree

6 files changed

+115
-4
lines changed

6 files changed

+115
-4
lines changed

example_v3_test.py

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

11+
12+
1113
client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
1214

13-
status, msg = client.asm_groups.get([66,67,50])
15+
status, msg = client.asm_suppressions.post(60, ['elmer+test@thinkingserious.com', 'elmer.thomas@yahoo.com'])
1416
print status
1517
print msg
1618

1719
"""
1820
21+
status, msg = client.asm_suppressions.get(None,'elmer.thomas@yahoo.com')
22+
print status
23+
print msg
24+
25+
status, msg = client.asm_groups.get([66,67,50])
26+
print status
27+
print msg
28+
1929
name = "My Amazing API Key"
2030
status, msg = client.apikeys.post(name)
2131
msg = json.loads(msg)

sendgrid/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .exceptions import SendGridClientError, SendGridServerError
1414
from .resources.apikeys import APIKeys
1515
from .resources.asm_groups import ASMGroups
16+
from .resources.asm_suppressions import ASMSuppressions
1617

1718
class SendGridAPIClient(object):
1819

@@ -34,6 +35,7 @@ def __init__(self, apikey, **opts):
3435

3536
self.apikeys = APIKeys(self)
3637
self.asm_groups = ASMGroups(self)
38+
self.asm_suppressions = ASMSuppressions(self)
3739

3840
@property
3941
def apikey(self):

sendgrid/resources/asm_groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ASMGroups(object):
88

99
def __init__(self, client, **opts):
1010
"""
11-
Constructs SendGrid ASM object.
11+
Constructs SendGrid ASM group object.
1212
1313
See https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/index.html and
1414
https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/groups.html
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class ASMSuppressions(object):
2+
"""Advanced Suppression Manager 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+
Suppressions are email addresses that can be added to groups to prevent certain types of emails from being
6+
delivered to those addresses.
7+
"""
8+
9+
def __init__(self, client, **opts):
10+
"""
11+
Constructs SendGrid ASM suppressions object.
12+
13+
See https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/index.html and
14+
https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/groups.html
15+
"""
16+
self._name = None
17+
self._base_endpoint = "/v3/asm/groups"
18+
self._endpoint = "/v3/asm/groups"
19+
self._client = client
20+
21+
@property
22+
def base_endpoint(self):
23+
return self._base_endpoint
24+
25+
@property
26+
def endpoint(self):
27+
endpoint = self._endpoint
28+
return endpoint
29+
30+
@endpoint.setter
31+
def endpoint(self, value):
32+
self._endpoint = value
33+
34+
@property
35+
def client(self):
36+
return self._client
37+
38+
# Get suppressed addresses for a given group id.
39+
def get(self, id=None, email=None):
40+
if id == None and email == None:
41+
return self.client.get(self)
42+
43+
if isinstance(id, int):
44+
self._endpoint = self._base_endpoint + "/" + str(id) + "/suppressions"
45+
return self.client.get(self)
46+
47+
if isinstance(email, str):
48+
self._endpoint = "/v3/asm/suppressions/" + email
49+
50+
return self.client.get(self)
51+
52+
# Add recipient addresses to the suppressions list for a given group.
53+
# If the group has been deleted, this request will add the address to the global suppression.
54+
def post(self, id, emails):
55+
self._endpoint = self._base_endpoint + "/" + str(id) + "/suppressions"
56+
data = {}
57+
data["recipient_emails"] = emails
58+
return self.client.post(self, data)

test/test_asm_groups.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ def setUp(self):
2020
SendGridAPIClient = MockSendGridAPIClientRequest
2121
self.client = SendGridAPIClient(SG_KEY)
2222

23-
def test_apikeys_init(self):
23+
def test_asm_groups_init(self):
2424
self.asm_groups = self.client.asm_groups
2525
self.assertEqual(self.asm_groups.base_endpoint, "/v3/asm/groups")
2626
self.assertEqual(self.asm_groups.endpoint, "/v3/asm/groups")
2727
self.assertEqual(self.asm_groups.client, self.client)
2828

2929
def test_asm_groups_get(self):
30-
status, msg = self.client.apikeys.get()
30+
status, msg = self.client.asm_groups.get()
3131
self.assertEqual(status, 200)
3232

3333
if __name__ == '__main__':

test/test_asm_suppressions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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_suppressions_init(self):
24+
self.asm_suppressions = self.client.asm_suppressions
25+
self.assertEqual(self.asm_suppressions.base_endpoint, "/v3/asm/groups")
26+
self.assertEqual(self.asm_suppressions.endpoint, "/v3/asm/groups")
27+
self.assertEqual(self.asm_suppressions.client, self.client)
28+
29+
def test_asm_suppressions_get(self):
30+
status, msg = self.client.asm_suppressions.get()
31+
self.assertEqual(status, 200)
32+
33+
def test_asm_suppressions_post(self):
34+
id = 67
35+
emails = ['elmer+test@thinkingserious.com']
36+
status, msg = self.client.asm_suppressions.post(id, emails)
37+
self.assertEqual(status, 201)
38+
self.assertEqual(msg['recipient_emails'], emails)
39+
40+
if __name__ == '__main__':
41+
unittest.main()

0 commit comments

Comments
 (0)