Skip to content

Commit 79cc7db

Browse files
Fixing the tests
1 parent bdfc104 commit 79cc7db

15 files changed

Lines changed: 313 additions & 15 deletions

.env_sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SENDGRID_API_KEY=your_sendgrid_api_key
2+
SENDGRID_USERNAME=your_sendgrid_username
3+
SENDGRID_PASSWORD=your_sendgrid_password

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ sdist
88
*.egg
99
*.egg-info
1010
*.pyc
11-
.idea/
1211
venv/
12+
.idea
13+
.env

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4-
## [1.4.2] - 2015-09-15
5-
### Added
6-
- Upgrade Mail to new-style class, on Python 2.x.
7-
84
## [1.4.1] - 2015-09-09
95
### Added
106
- Classifiers for compatible python versions

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,11 @@ Tests
380380
~~~~~
381381

382382
.. code:: python
383-
383+
384384
virtualenv venv
385-
source venv/bin/activate
385+
source venv/bin/activate #or . ./activate.sh
386386
python setup.py install
387-
python test/__init__.py
387+
unit2 discover
388388
389389
Deploying
390390
~~~~~~~~~

activate.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
# Use this to activate the virtual environment, use the following to execute in current shell
3+
# . ./activate
4+
source venv/bin/activate

example_v2_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sendgrid
2+
import os
3+
if os.path.exists('.env'):
4+
for line in open('.env'):
5+
var = line.strip().split('=')
6+
if len(var) == 2:
7+
os.environ[var[0]] = var[1]
8+
9+
sg = sendgrid.SendGridClient(os.environ.get('SENDGRID_USERNAME'), os.environ.get('SENDGRID_PASSWORD'))
10+
11+
message = sendgrid.Mail()
12+
message.add_to('Elmer Thomas <elmer@thinkingserious.com>')
13+
message.set_subject('Testing from the Python library')
14+
message.set_html('<b>This was a successful test!</b>')
15+
message.set_text('This was a successful test!')
16+
message.set_from('Elmer Thomas <elmer@thinkingserious.com>')
17+
status, msg = sg.send(message)
18+
print status
19+
print msg

example_v3_test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sendgrid
2+
import json
3+
4+
import os
5+
if os.path.exists('.env'):
6+
for line in open('.env'):
7+
var = line.strip().split('=')
8+
if len(var) == 2:
9+
os.environ[var[0]] = var[1]
10+
11+
client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
12+
13+
name = "My Amazing API Key"
14+
status, msg = client.apikeys.post(name)
15+
msg = json.loads(msg)
16+
api_key_id = msg['api_key_id']
17+
print status
18+
print msg
19+
20+
name = "My NEW API Key 3000"
21+
status, msg = client.apikeys.patch(api_key_id, name)
22+
print status
23+
print msg
24+
25+
status, msg = client.apikeys.delete(api_key_id)
26+
print status
27+
28+
status, msg = client.apikeys.get()
29+
print status
30+
print msg
31+
32+
"""
33+
# Get a list of all valid API Keys from your account
34+
status, msg = client.apikeys.get()
35+
print status
36+
print msg
37+
38+
# Create a new API Key
39+
name = "My API Key 10"
40+
status, msg = client.apikeys.post(name)
41+
print status
42+
print msg
43+
44+
# Delete an API Key with a given api_key_id
45+
api_key_id = "zc0r5sW5TTuBQGsMPMUx0A"
46+
status, msg = client.apikeys.delete(api_key_id)
47+
print status
48+
print msg
49+
50+
# Update the name of an API Key, given an api_key_id
51+
api_key_id = "API_KEY"
52+
name = "My API Key 3"
53+
status, msg = client.apikeys.patch(api_key_id, name)
54+
print status
55+
print msg
56+
"""

sendgrid/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from .version import __version__
22
from .sendgrid import SendGridClient
33
from .exceptions import SendGridError, SendGridClientError, SendGridServerError
4+
#v2 API
45
from .message import Mail
6+
#v3 API
7+
from .client import SendGridAPIClient

sendgrid/client.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import json
2+
from .version import __version__
3+
from socket import timeout
4+
try:
5+
import urllib.request as urllib_request
6+
from urllib.parse import urlencode
7+
from urllib.error import HTTPError
8+
except ImportError: # Python 2
9+
import urllib2 as urllib_request
10+
from urllib2 import HTTPError
11+
from urllib import urlencode
12+
13+
from .exceptions import SendGridClientError, SendGridServerError
14+
from resources.apikeys import APIKeys
15+
16+
class SendGridAPIClient(object):
17+
18+
"""SendGrid API."""
19+
20+
def __init__(self, apikey, **opts):
21+
"""
22+
Construct SendGrid API object.
23+
24+
Args:
25+
apikey: SendGrid API key
26+
opts: You can pass in host or proxies
27+
"""
28+
self._apikey = apikey
29+
self.useragent = 'sendgrid/' + __version__ + ';python_v3'
30+
self.host = opts.get('host', 'https://api.sendgrid.com')
31+
# urllib cannot connect to SSL servers using proxies
32+
self.proxies = opts.get('proxies', None)
33+
34+
self.apikeys = APIKeys(self)
35+
36+
@property
37+
def apikey(self):
38+
return self._apikey
39+
40+
@apikey.setter
41+
def apikey(self, value):
42+
self._apikey = value
43+
44+
def _build_request(self, url, json_header=False, method='GET', data=None):
45+
if self.proxies:
46+
proxy_support = urllib_request.ProxyHandler(self.proxies)
47+
opener = urllib_request.build_opener(proxy_support)
48+
urllib_request.install_opener(opener)
49+
req = urllib_request.Request(url)
50+
req.get_method = lambda: method
51+
req.add_header('User-Agent', self.useragent)
52+
req.add_header('Authorization', 'Bearer ' + self.apikey)
53+
if json_header:
54+
req.add_header('Content-Type', 'application/json')
55+
try:
56+
if data:
57+
response = urllib_request.urlopen(req, json.dumps(data))
58+
else:
59+
response = urllib_request.urlopen(req, timeout=10)
60+
except HTTPError as e:
61+
if 400 <= e.code < 500:
62+
raise SendGridClientError(e.code, e.read())
63+
elif 500 <= e.code < 600:
64+
raise SendGridServerError(e.code, e.read())
65+
else:
66+
assert False
67+
except timeout as e:
68+
raise SendGridClientError(408, 'Request timeout')
69+
body = response.read()
70+
return response, body
71+
72+
def get(self, api):
73+
url = self.host + api.base_endpoint
74+
response, body = self._build_request(url, False, 'GET')
75+
return response.getcode(), body
76+
77+
def post(self, api, data):
78+
url = self.host + api.endpoint
79+
response, body = self._build_request(url, True, 'POST', data)
80+
return response.getcode(), body
81+
82+
def delete(self, api):
83+
url = self.host + api.endpoint
84+
response, body = self._build_request(url, False, 'DELETE')
85+
return response.getcode(), body
86+
87+
def patch(self, api, data):
88+
url = self.host + api.endpoint
89+
response, body = self._build_request(url, True, 'PATCH', data)
90+
return response.getcode(), body

sendgrid/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from smtpapi import SMTPAPIHeader
99

1010

11-
class Mail(object):
11+
class Mail():
1212

1313
"""SendGrid Message."""
1414

0 commit comments

Comments
 (0)