Skip to content

Commit

Permalink
Merge pull request #124 from thinkingserious/asm_suppressions_post
Browse files Browse the repository at this point in the history
ASM suppressions [POST]
  • Loading branch information
thinkingserious committed Sep 24, 2015
2 parents ceb2763 + 0d8c301 commit cf3c155
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
12 changes: 11 additions & 1 deletion example_v3_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
if len(var) == 2:
os.environ[var[0]] = var[1]



client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))

status, msg = client.asm_groups.get([66,67,50])
status, msg = client.asm_suppressions.post(60, ['[email protected]', '[email protected]'])
print status
print msg

"""
status, msg = client.asm_suppressions.get(None,'[email protected]')
print status
print msg
status, msg = client.asm_groups.get([66,67,50])
print status
print msg
name = "My Amazing API Key"
status, msg = client.apikeys.post(name)
msg = json.loads(msg)
Expand Down
2 changes: 2 additions & 0 deletions sendgrid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .exceptions import SendGridClientError, SendGridServerError
from .resources.apikeys import APIKeys
from .resources.asm_groups import ASMGroups
from .resources.asm_suppressions import ASMSuppressions

class SendGridAPIClient(object):

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

self.apikeys = APIKeys(self)
self.asm_groups = ASMGroups(self)
self.asm_suppressions = ASMSuppressions(self)

@property
def apikey(self):
Expand Down
2 changes: 1 addition & 1 deletion sendgrid/resources/asm_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ASMGroups(object):

def __init__(self, client, **opts):
"""
Constructs SendGrid ASM object.
Constructs SendGrid ASM group object.
See https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/index.html and
https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/groups.html
Expand Down
58 changes: 58 additions & 0 deletions sendgrid/resources/asm_suppressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class ASMSuppressions(object):
"""Advanced Suppression Manager gives your recipients more control over the types of emails they want to receive
by letting them opt out of messages from a certain type of email.
Suppressions are email addresses that can be added to groups to prevent certain types of emails from being
delivered to those addresses.
"""

def __init__(self, client, **opts):
"""
Constructs SendGrid ASM suppressions object.
See https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/index.html and
https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/groups.html
"""
self._name = None
self._base_endpoint = "/v3/asm/groups"
self._endpoint = "/v3/asm/groups"
self._client = client

@property
def base_endpoint(self):
return self._base_endpoint

@property
def endpoint(self):
endpoint = self._endpoint
return endpoint

@endpoint.setter
def endpoint(self, value):
self._endpoint = value

@property
def client(self):
return self._client

# Get suppressed addresses for a given group id.
def get(self, id=None, email=None):
if id == None and email == None:
return self.client.get(self)

if isinstance(id, int):
self._endpoint = self._base_endpoint + "/" + str(id) + "/suppressions"
return self.client.get(self)

if isinstance(email, str):
self._endpoint = "/v3/asm/suppressions/" + email

return self.client.get(self)

# Add recipient addresses to the suppressions list for a given group.
# If the group has been deleted, this request will add the address to the global suppression.
def post(self, id, emails):
self._endpoint = self._base_endpoint + "/" + str(id) + "/suppressions"
data = {}
data["recipient_emails"] = emails
return self.client.post(self, data)
4 changes: 2 additions & 2 deletions test/test_asm_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ def setUp(self):
SendGridAPIClient = MockSendGridAPIClientRequest
self.client = SendGridAPIClient(SG_KEY)

def test_apikeys_init(self):
def test_asm_groups_init(self):
self.asm_groups = self.client.asm_groups
self.assertEqual(self.asm_groups.base_endpoint, "/v3/asm/groups")
self.assertEqual(self.asm_groups.endpoint, "/v3/asm/groups")
self.assertEqual(self.asm_groups.client, self.client)

def test_asm_groups_get(self):
status, msg = self.client.apikeys.get()
status, msg = self.client.asm_groups.get()
self.assertEqual(status, 200)

if __name__ == '__main__':
Expand Down
41 changes: 41 additions & 0 deletions test/test_asm_suppressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from .base_test import BaseTest, MockSendGridAPIClientRequest
import os
try:
import unittest2 as unittest
except ImportError:
import unittest
try:
from StringIO import StringIO
except ImportError: # Python 3
from io import StringIO

import sendgrid
from sendgrid.client import SendGridAPIClient
from sendgrid.version import __version__

SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY'

class TestASMGroups(unittest.TestCase):
def setUp(self):
SendGridAPIClient = MockSendGridAPIClientRequest
self.client = SendGridAPIClient(SG_KEY)

def test_asm_suppressions_init(self):
self.asm_suppressions = self.client.asm_suppressions
self.assertEqual(self.asm_suppressions.base_endpoint, "/v3/asm/groups")
self.assertEqual(self.asm_suppressions.endpoint, "/v3/asm/groups")
self.assertEqual(self.asm_suppressions.client, self.client)

def test_asm_suppressions_get(self):
status, msg = self.client.asm_suppressions.get()
self.assertEqual(status, 200)

def test_asm_suppressions_post(self):
id = 67
emails = ['[email protected]']
status, msg = self.client.asm_suppressions.post(id, emails)
self.assertEqual(status, 201)
self.assertEqual(msg['recipient_emails'], emails)

if __name__ == '__main__':
unittest.main()

0 comments on commit cf3c155

Please sign in to comment.