Skip to content

Commit ceb2763

Browse files
Merge pull request #123 from thinkingserious/api_v3_client
Api v3 client - ASM Groups endpoint
2 parents 5496688 + 4d018fc commit ceb2763

10 files changed

Lines changed: 215 additions & 61 deletions

File tree

README.rst

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,44 @@ add_content_id
223223
message.add_attachment('image.png', open('./image.png', 'rb'))
224224
message.add_content_id('image.png', 'ID_IN_HTML')
225225
message.set_html('<html><body>TEXT BEFORE IMAGE<img src="cid:ID_IN_HTML"></img>AFTER IMAGE</body></html>')
226+
227+
WEB API v3
228+
----------
229+
230+
`APIKeys`_
231+
~~~~~~~~~~
232+
233+
List all API Keys belonging to the authenticated user.
234+
235+
.. code:: python
236+
237+
client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
238+
status, msg = client.apikeys.get()
239+
240+
`Advanced Suppression Manager (ASM)`_
241+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
242+
243+
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.
244+
245+
More information_.
246+
247+
.. _information: https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/index.html
248+
249+
ASM Groups
250+
~~~~~~~~~~
251+
252+
Retrieve all suppression groups associated with the user.
253+
254+
.. code:: python
255+
256+
client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
257+
status, msg = client.asm_groups.get()
258+
259+
Get a single record.
260+
261+
.. code:: python
262+
263+
status, msg = client.asm_groups.get(record_id)
226264
227265
SendGrid's `X-SMTPAPI`_
228266
-----------------------
@@ -402,16 +440,16 @@ Tests
402440
pyenv install 2.6.9
403441
pyenv install 2.7.8
404442
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
410443
411444
**Run the tests:**
412445

413446
.. code:: python
414447
448+
virtualenv venv
449+
source venv/bin/activate #or . ./activate.sh
450+
python setup.py install
451+
pyenv local 3.2.6 2.7.8 2.6.9
452+
pyenv rehash
415453
tox
416454
417455
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([66,67,50])
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: 3 additions & 1 deletion
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):
@@ -70,7 +72,7 @@ def _build_request(self, url, json_header=False, method='GET', data=None):
7072
return response.getcode(), body
7173

7274
def get(self, api):
73-
url = self.host + api.base_endpoint
75+
url = self.host + api.endpoint
7476
response, body = self._build_request(url, False, 'GET')
7577
return response, body
7678

sendgrid/resources/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

sendgrid/resources/asm_groups.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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, id=None):
40+
if id == None:
41+
return self.client.get(self)
42+
43+
if isinstance(id, int):
44+
self._endpoint = self._base_endpoint + "/" + str(id)
45+
return self.client.get(self)
46+
47+
if len(id) > 1:
48+
count = 0
49+
for i in id:
50+
if count == 0:
51+
self._endpoint = self._endpoint + "?id=" + str(i)
52+
else:
53+
self._endpoint = self._endpoint + "&id=" + str(i)
54+
count = count + 1
55+
56+
return self.client.get(self)

test/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

test/base_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sendgrid
2+
from sendgrid.client import SendGridAPIClient
3+
try:
4+
import urllib.request as urllib_request
5+
from urllib.parse import urlencode
6+
from urllib.error import HTTPError
7+
except ImportError: # Python 2
8+
import urllib2 as urllib_request
9+
from urllib2 import HTTPError
10+
from urllib import urlencode
11+
12+
class BaseTest():
13+
def __init__(self):
14+
pass
15+
16+
class MockSendGridAPIClientRequest(SendGridAPIClient):
17+
def __init__(self, apikey, **opts):
18+
super(MockSendGridAPIClientRequest, self).__init__(apikey, **opts)
19+
self._req = None
20+
21+
def _build_request(self, url=None, json_header=False, method='GET', data=None):
22+
req = urllib_request.Request(url)
23+
req.get_method = lambda: method
24+
req.add_header('User-Agent', self.useragent)
25+
req.add_header('Authorization', 'Bearer ' + self.apikey)
26+
if json_header:
27+
req.add_header('Content-Type', 'application/json')
28+
body = data
29+
if method == 'POST':
30+
response = 201
31+
if method == 'PATCH':
32+
response = 200
33+
if method == 'DELETE':
34+
response = 204
35+
if method == 'GET':
36+
response = 200
37+
return response, body

test/test_api_client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 TestSendGridAPIClient(unittest.TestCase):
19+
def setUp(self):
20+
self.client = MockSendGridAPIClientRequest
21+
self.client = SendGridAPIClient(SG_KEY)
22+
23+
def test_apikey_init(self):
24+
self.assertEqual(self.client.apikey, SG_KEY)
25+
26+
def test_useragent(self):
27+
useragent = 'sendgrid/' + __version__ + ';python_v3'
28+
self.assertEqual(self.client.useragent, useragent)
29+
30+
def test_host(self):
31+
host = 'https://api.sendgrid.com'
32+
self.assertEqual(self.client.host, host)
33+
34+
if __name__ == '__main__':
35+
unittest.main()

test/test_apikeys.py

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,20 @@
1+
from .base_test import BaseTest, MockSendGridAPIClientRequest
12
import os
23
try:
34
import unittest2 as unittest
45
except ImportError:
56
import unittest
6-
import json
7-
import sys
87
try:
98
from StringIO import StringIO
109
except ImportError: # Python 3
1110
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
2011

2112
import sendgrid
22-
from sendgrid import SendGridClient, Mail
23-
from sendgrid.exceptions import SendGridClientError, SendGridServerError
24-
from sendgrid.sendgrid import HTTPError
2513
from sendgrid.client import SendGridAPIClient
2614
from sendgrid.version import __version__
2715

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

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-
6918
class TestAPIKeys(unittest.TestCase):
7019
def setUp(self):
7120
SendGridAPIClient = MockSendGridAPIClientRequest

test/test_asm_groups.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_apikeys_init(self):
24+
self.asm_groups = self.client.asm_groups
25+
self.assertEqual(self.asm_groups.base_endpoint, "/v3/asm/groups")
26+
self.assertEqual(self.asm_groups.endpoint, "/v3/asm/groups")
27+
self.assertEqual(self.asm_groups.client, self.client)
28+
29+
def test_asm_groups_get(self):
30+
status, msg = self.client.apikeys.get()
31+
self.assertEqual(status, 200)
32+
33+
if __name__ == '__main__':
34+
unittest.main()

0 commit comments

Comments
 (0)