Skip to content

Commit 138c998

Browse files
committed
Add support for api keys
1 parent 312fb7b commit 138c998

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

CHANGELOG.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
v1.3.0 (2014-01-23)
2-
===================
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
33

4-
* Add new method for ASM Group ID via [#98](https://github.com/sendgrid/sendgrid-python/pull/98)
5-
* Add CHANGELOG.md
4+
## [1.4.0] - Unreleased
5+
### Added
6+
- Support for API keys
67

7-
--
8+
## [1.3.0] - 2014-01-23
9+
### Added
10+
- Add new method for ASM Group ID via [#98](https://github.com/sendgrid/sendgrid-python/pull/98)
11+
- Add CHANGELOG.md

README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ encouraged to set ``raise_errors`` to ``True`` for forwards compatibility.
6666

6767
``SendGridError`` is a base-class for all SendGrid-related exceptions.
6868

69+
Usage
70+
~~~~~
71+
72+
To begin using this library create a new instance of `SendGridClient` with your SendGrid credentials or a SendGrid API Key. API Key is the preferred method. API Keys are in beta. To configure API keys, visit https://sendgrid.com/beta/settings/api_key.
73+
74+
.. code:: python
75+
sg = sendgrid.SendGridClient('sendgrid_username', 'sendgrid_password')
76+
# or
77+
sg = sendgrid.SendGridClient('sendgrid_apikey')
78+
6979
Methods
7080
~~~~~~~
7181

sendgrid/sendgrid.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SendGridClient(object):
1717

1818
"""SendGrid API."""
1919

20-
def __init__(self, username, password, **opts):
20+
def __init__(self, username_or_apikey, password=None, **opts):
2121
"""
2222
Construct SendGrid API object.
2323
@@ -31,8 +31,17 @@ def __init__(self, username, password, **opts):
3131
1.0.0, the default will be changed to True, so you are
3232
recommended to pass True for forwards compatability.
3333
"""
34-
self.username = username
35-
self.password = password
34+
35+
# Check if username + password or api key
36+
if password is None:
37+
# API Key
38+
self.username = None
39+
self.password = username_or_apikey
40+
else:
41+
# Username + password
42+
self.username = username_or_apikey
43+
self.password = password
44+
3645
self.useragent = 'sendgrid/' + __version__ + ';python'
3746
self.host = opts.get('host', 'https://api.sendgrid.com')
3847
self.port = str(opts.get('port', '443'))
@@ -52,8 +61,6 @@ def _build_body(self, message):
5261
setattr(message, k, v.encode('utf-8'))
5362

5463
values = {
55-
'api_user': self.username,
56-
'api_key': self.password,
5764
'to[]': message.to if message.to else [message.from_email],
5865
'toname[]': message.to_name,
5966
'cc[]': message.cc,
@@ -68,6 +75,12 @@ def _build_body(self, message):
6875
'date': message.date,
6976
'x-smtpapi': message.json_string()
7077
}
78+
79+
if self.username != None:
80+
# Using username + password
81+
values['api_user'] = self.username
82+
values['api_key'] = self.password
83+
7184
for k in list(values.keys()):
7285
if not values[k]:
7386
del values[k]
@@ -87,6 +100,11 @@ def _make_request(self, message):
87100
data = urlencode(self._build_body(message), True).encode('utf-8')
88101
req = urllib_request.Request(self.mail_url, data)
89102
req.add_header('User-Agent', self.useragent)
103+
104+
if self.username is None:
105+
# Using API key
106+
req.add_header('Authorization', 'Bearer ' + self.password)
107+
90108
response = urllib_request.urlopen(req, timeout=10)
91109
body = response.read()
92110
return response.getcode(), body

test/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
from sendgrid.exceptions import SendGridClientError, SendGridServerError
1515
from sendgrid.sendgrid import HTTPError
1616

17-
18-
SG_USER, SG_PWD = os.getenv('SG_USER'), os.getenv('SG_PWD')
19-
17+
SG_USER = os.getenv('SG_USER') or 'SENDGRID_USERNAME'
18+
SG_PWD = os.getenv('SG_PWD') or 'SENDGRID_PASSWORD'
2019

2120
class TestSendGrid(unittest.TestCase):
2221
def setUp(self):
2322
self.sg = SendGridClient(SG_USER, SG_PWD)
2423

24+
def test_apikey_init(self):
25+
sg = SendGridClient(SG_PWD)
26+
self.assertEqual(sg.password, SG_PWD)
27+
self.assertIsNone(sg.username)
28+
2529
@unittest.skipUnless(sys.version_info < (3, 0), 'only for python2')
2630
def test_unicode_recipients(self):
2731
recipients = [unicode('test@test.com'), unicode('guy@man.com')]

0 commit comments

Comments
 (0)