-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from thinkingserious/asm_suppressions_post
ASM suppressions [POST]
- Loading branch information
Showing
6 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |