Skip to content

Commit

Permalink
Revert to standard urllib error handling.
Browse files Browse the repository at this point in the history
This reverts commit e4dfae4, which
tries to fix elbuo8#74 by importing HTTPError from `python_http_client`. This
change isn't necessary, since the error in elbuo8#74 was being caused by a bug
in `sendgrid-python` which resulted in the wrong version of
`python_http_client` being installed:
sendgrid/sendgrid-python#321

Since the bug is fixed in `sendgrid-python` 4.2.1, but this library
still uses `sendgrid-python` 3.5+, I've added additional installation
constraints on `python_http_client` so that it should never install a
version with custom error handling (2.3.0 or above). This constraint
should be removed when we upgrade to `sendgrid-python` 4.
  • Loading branch information
martey committed Oct 18, 2018
1 parent e4dfae4 commit eddf439
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
license='MIT',
description='SendGrid Backend for Django',
long_description=open('./README.rst').read(),
install_requires=["sendgrid >= 3.5, < 4"],
install_requires=[
"python_http_client >= 2.1.*, <2.3",
"sendgrid >= 3.5, <4",
],
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
Expand Down
6 changes: 5 additions & 1 deletion sgbackend/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import base64
import sys
from email.mime.base import MIMEBase
from python_http_client.exceptions import HTTPError

try:
from urllib.error import HTTPError # pragma: no cover
except ImportError: # pragma: no cover
from urllib2 import HTTPError # pragma: no cover

try:
import rfc822
Expand Down
30 changes: 0 additions & 30 deletions tests/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,12 @@
from django.core.mail import EmailMessage
from django.core.mail import EmailMultiAlternatives
from django.test import SimpleTestCase as TestCase
from python_http_client.client import HTTPError
from python_http_client.client import Client, Response
from python_http_client.exceptions import handle_error

from sgbackend import SendGridBackend

settings.configure()


class MockException(HTTPError):
def __init__(self, code):
self.code = code
self.reason = 'REASON'
self.hdrs = 'HEADERS'

def read(self):
return 'BODY'


class MockClient(Client):
def __init__(self, host):
self.response_code = 400
Client.__init__(self, host)

def _make_request(self, opener, request):
raise handle_error(MockException(self.response_code))


class SendGridBackendTests(TestCase):
def test_raises_if_sendgrid_api_key_doesnt_exists(self):
with self.assertRaises(ImproperlyConfigured):
Expand Down Expand Up @@ -220,11 +198,3 @@ def test_build_sg_email_w_custom_args(self):
'personalizations': [{'subject': ''}],
'subject': ''}
)

def test_send_messages_error(self):
mock_client = MockClient(self.host)
backend = SendGridBackend()
backend.sg.client = mock_client
msg = EmailMessage()
with self.assertRaises(HTTPError):
backend.send_messages(emails=[SendGridBackend()._build_sg_mail(msg)])

0 comments on commit eddf439

Please sign in to comment.