-
Notifications
You must be signed in to change notification settings - Fork 714
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 #123 from thinkingserious/api_v3_client
Api v3 client - ASM Groups endpoint
- Loading branch information
Showing
10 changed files
with
215 additions
and
61 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
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 |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
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,56 @@ | ||
class ASMGroups(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. | ||
Groups are specific types of email you would like your recipients to be able to unsubscribe from or subscribe to. | ||
For example: Daily Newsletters, Invoices, System Alerts. | ||
""" | ||
|
||
def __init__(self, client, **opts): | ||
""" | ||
Constructs SendGrid ASM 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 | ||
|
||
# Retrieve all suppression groups associated with the user. | ||
def get(self, id=None): | ||
if id == None: | ||
return self.client.get(self) | ||
|
||
if isinstance(id, int): | ||
self._endpoint = self._base_endpoint + "/" + str(id) | ||
return self.client.get(self) | ||
|
||
if len(id) > 1: | ||
count = 0 | ||
for i in id: | ||
if count == 0: | ||
self._endpoint = self._endpoint + "?id=" + str(i) | ||
else: | ||
self._endpoint = self._endpoint + "&id=" + str(i) | ||
count = count + 1 | ||
|
||
return self.client.get(self) |
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 |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
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,37 @@ | ||
import sendgrid | ||
from sendgrid.client import SendGridAPIClient | ||
try: | ||
import urllib.request as urllib_request | ||
from urllib.parse import urlencode | ||
from urllib.error import HTTPError | ||
except ImportError: # Python 2 | ||
import urllib2 as urllib_request | ||
from urllib2 import HTTPError | ||
from urllib import urlencode | ||
|
||
class BaseTest(): | ||
def __init__(self): | ||
pass | ||
|
||
class MockSendGridAPIClientRequest(SendGridAPIClient): | ||
def __init__(self, apikey, **opts): | ||
super(MockSendGridAPIClientRequest, self).__init__(apikey, **opts) | ||
self._req = None | ||
|
||
def _build_request(self, url=None, json_header=False, method='GET', data=None): | ||
req = urllib_request.Request(url) | ||
req.get_method = lambda: method | ||
req.add_header('User-Agent', self.useragent) | ||
req.add_header('Authorization', 'Bearer ' + self.apikey) | ||
if json_header: | ||
req.add_header('Content-Type', 'application/json') | ||
body = data | ||
if method == 'POST': | ||
response = 201 | ||
if method == 'PATCH': | ||
response = 200 | ||
if method == 'DELETE': | ||
response = 204 | ||
if method == 'GET': | ||
response = 200 | ||
return response, body |
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,35 @@ | ||
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 TestSendGridAPIClient(unittest.TestCase): | ||
def setUp(self): | ||
self.client = MockSendGridAPIClientRequest | ||
self.client = SendGridAPIClient(SG_KEY) | ||
|
||
def test_apikey_init(self): | ||
self.assertEqual(self.client.apikey, SG_KEY) | ||
|
||
def test_useragent(self): | ||
useragent = 'sendgrid/' + __version__ + ';python_v3' | ||
self.assertEqual(self.client.useragent, useragent) | ||
|
||
def test_host(self): | ||
host = 'https://api.sendgrid.com' | ||
self.assertEqual(self.client.host, host) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,34 @@ | ||
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_apikeys_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() | ||
self.assertEqual(status, 200) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |