Skip to content

Commit 70f2855

Browse files
Adding ASM Groups endpoint
1 parent 5496688 commit 70f2855

5 files changed

Lines changed: 138 additions & 7 deletions

File tree

README.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,15 @@ Tests
402402
pyenv install 2.6.9
403403
pyenv install 2.7.8
404404
pyenv install 3.2.6
405-
pyenv local 3.2.6 2.7.8 2.6.9
406-
pyenv rehash
407-
virtualenv venv
408-
source venv/bin/activate #or . ./activate.sh
409-
python setup.py install
410405
411406
**Run the tests:**
412407

413408
.. code:: python
414-
409+
virtualenv venv
410+
source venv/bin/activate #or . ./activate.sh
411+
python setup.py install
412+
pyenv local 3.2.6 2.7.8 2.6.9
413+
pyenv rehash
415414
tox
416415
417416
Deploying

example_v3_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

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

13+
status, msg = client.asm_groups.get()
14+
print status
15+
print msg
16+
17+
"""
18+
1319
name = "My Amazing API Key"
1420
status, msg = client.apikeys.post(name)
1521
msg = json.loads(msg)
@@ -29,7 +35,6 @@
2935
print status
3036
print msg
3137
32-
"""
3338
# Get a list of all valid API Keys from your account
3439
status, msg = client.apikeys.get()
3540
print status

sendgrid/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from .exceptions import SendGridClientError, SendGridServerError
1414
from .resources.apikeys import APIKeys
15+
from .resources.asm_groups import ASMGroups
1516

1617
class SendGridAPIClient(object):
1718

@@ -32,6 +33,7 @@ def __init__(self, apikey, **opts):
3233
self.proxies = opts.get('proxies', None)
3334

3435
self.apikeys = APIKeys(self)
36+
self.asm_groups = ASMGroups(self)
3537

3638
@property
3739
def apikey(self):

sendgrid/resources/asm_groups.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class ASMGroups(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+
Groups are specific types of email you would like your recipients to be able to unsubscribe from or subscribe to.
6+
For example: Daily Newsletters, Invoices, System Alerts.
7+
"""
8+
9+
def __init__(self, client, **opts):
10+
"""
11+
Constructs SendGrid ASM 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+
# Retrieve all suppression groups associated with the user.
39+
def get(self):
40+
return self.client.get(self)

test/test_asm_groups.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import os
2+
try:
3+
import unittest2 as unittest
4+
except ImportError:
5+
import unittest
6+
import json
7+
import sys
8+
try:
9+
from StringIO import StringIO
10+
except ImportError: # Python 3
11+
from io import StringIO
12+
try:
13+
import urllib.request as urllib_request
14+
from urllib.parse import urlencode
15+
from urllib.error import HTTPError
16+
except ImportError: # Python 2
17+
import urllib2 as urllib_request
18+
from urllib2 import HTTPError
19+
from urllib import urlencode
20+
21+
import sendgrid
22+
from sendgrid import SendGridClient, Mail
23+
from sendgrid.exceptions import SendGridClientError, SendGridServerError
24+
from sendgrid.sendgrid import HTTPError
25+
from sendgrid.client import SendGridAPIClient
26+
from sendgrid.version import __version__
27+
28+
SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY'
29+
30+
class MockSendGridAPIClientRequest(SendGridAPIClient):
31+
def __init__(self, apikey, **opts):
32+
super(MockSendGridAPIClientRequest, self).__init__(apikey, **opts)
33+
self._req = None
34+
35+
def _build_request(self, url=None, json_header=False, method='GET', data=None):
36+
req = urllib_request.Request(url)
37+
req.get_method = lambda: method
38+
req.add_header('User-Agent', self.useragent)
39+
req.add_header('Authorization', 'Bearer ' + self.apikey)
40+
if json_header:
41+
req.add_header('Content-Type', 'application/json')
42+
body = data
43+
if method == 'POST':
44+
response = 201
45+
if method == 'PATCH':
46+
response = 200
47+
if method == 'DELETE':
48+
response = 204
49+
if method == 'GET':
50+
response = 200
51+
return response, body
52+
53+
class TestSendGridAPIClient(unittest.TestCase):
54+
def setUp(self):
55+
self.client = MockSendGridAPIClientRequest
56+
self.client = SendGridAPIClient(SG_KEY)
57+
58+
def test_apikey_init(self):
59+
self.assertEqual(self.client.apikey, SG_KEY)
60+
61+
def test_useragent(self):
62+
useragent = 'sendgrid/' + __version__ + ';python_v3'
63+
self.assertEqual(self.client.useragent, useragent)
64+
65+
def test_host(self):
66+
host = 'https://api.sendgrid.com'
67+
self.assertEqual(self.client.host, host)
68+
69+
class TestASMGroups(unittest.TestCase):
70+
def setUp(self):
71+
SendGridAPIClient = MockSendGridAPIClientRequest
72+
self.client = SendGridAPIClient(SG_KEY)
73+
74+
def test_apikeys_init(self):
75+
self.asm_groups = self.client.asm_groups
76+
self.assertEqual(self.asm_groups.base_endpoint, "/v3/asm/groups")
77+
self.assertEqual(self.asm_groups.endpoint, "/v3/asm/groups")
78+
self.assertEqual(self.asm_groups.client, self.client)
79+
80+
def test_asm_groups_get(self):
81+
status, msg = self.client.apikeys.get()
82+
self.assertEqual(status, 200)
83+
84+
if __name__ == '__main__':
85+
unittest.main()

0 commit comments

Comments
 (0)