Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: python
sudo: false
cache: pip
python:
- '2.6'
- '2.7'
- '3.4'
- '3.5'
Expand All @@ -11,7 +10,6 @@ env:
global:
- CC_TEST_REPORTER_ID=$TRAVIS_CODE_CLIMATE_TOKEN
install:
- if [[ $TRAVIS_PYTHON_VERSION == 2.6* ]]; then pip install unittest2; fi
- python setup.py install
- pip install pyyaml
- pip install flask
Expand All @@ -30,7 +28,7 @@ before_script:
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
script:
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then coverage run -m unittest2 discover; else coverage run -m unittest discover; fi
- coverage run -m unittest discover
after_script:
- codecov
- ./cc-test-reporter after-build --exit-code $?
Expand Down
11 changes: 2 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ You can use our Docker image to avoid setting up the development environment you

##### Prerequisites #####

- Python 2.6 through 3.6
- Python 2.7 and 3.4+
- [python_http_client](https://github.com/sendgrid/python-http-client)

##### Initial setup: #####
Expand Down Expand Up @@ -122,12 +122,6 @@ All test files are in the [`test`](https://github.com/sendgrid/sendgrid-python/t

For the purposes of contributing to this repo, please update the [`test_sendgrid.py`](https://github.com/sendgrid/sendgrid-python/tree/master/test/test_sendgrid.py) file with unit tests as you modify the code.

For Python 2.6.*:

`unit2 discover -v`

For Python 2.7.* and up:

`python -m unittest discover -v`

### Testing Multiple Versions of Python
Expand All @@ -149,7 +143,6 @@ You can install it by yourself in user dir by calling `source test/prism.sh`.
Add ```eval "$(pyenv init -)"``` to your shell environment (.profile, .bashrc, etc) after installing tox, you only need to do this once.

```
pyenv install 2.6.9
pyenv install 2.7.11
pyenv install 3.4.3
pyenv install 3.5.0
Expand All @@ -159,7 +152,7 @@ Make sure to change the current working directory to your local version of the r
python setup.py install
```
```
pyenv local 3.5.0 3.4.3 2.7.11 2.6.9
pyenv local 3.5.0 3.4.3 2.7.11
pyenv rehash
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ We appreciate your continued support, thank you!

## Prerequisites

- Python version 2.6, 2.7, 3.4, 3.5 or 3.6
- Python version 2.7 and 3.4+
- The SendGrid service, starting at the [free level](https://sendgrid.com/free?source=sendgrid-python)

## Setup Environment Variables
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM ubuntu:xenial
ENV PYTHON_VERSIONS='python2.6 python2.7 python3.4 python3.5 python3.6' \
ENV PYTHON_VERSIONS='python2.7 python3.4 python3.5 python3.6' \
OAI_SPEC_URL="https://raw.githubusercontent.com/sendgrid/sendgrid-oai/master/oai_stoplight.json"

# install testing versions of python, including old versions, from deadsnakes
Expand Down
4 changes: 3 additions & 1 deletion sendgrid/helpers/inbound/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def url(self):
"""URL to send to."""
return self._url


def main():
config = Config()
parser = argparse.ArgumentParser(description='Test data and optional host.')
Expand All @@ -54,5 +55,6 @@ def main():
print(response.headers)
print(response.body)


if __name__ == '__main__':
main()
main()
1 change: 1 addition & 0 deletions sendgrid/helpers/mail/content.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .validators import ValidateAPIKey


class Content(object):
"""Content to be included in your email.

Expand Down
6 changes: 3 additions & 3 deletions sendgrid/helpers/mail/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Various types of extensible SendGrid related exceptions
################################################################


class SendGridException(Exception):
"""Wrapper/default SendGrid-related exception"""
pass
Expand All @@ -14,9 +15,8 @@ class APIKeyIncludedException(SendGridException):
message -- explanation of the error
"""

def __init__(self,
expression="Email body",
def __init__(self,
expression="Email body",
message="SendGrid API Key detected"):
self.expression = expression
self.message = message

3 changes: 2 additions & 1 deletion sendgrid/helpers/mail/from_email.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .email import Email


class From(Email):
"""A from email address with an optional name."""
"""A from email address with an optional name."""
1 change: 1 addition & 0 deletions sendgrid/helpers/mail/html_content.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .content import Content
from .validators import ValidateAPIKey


class HtmlContent(Content):
"""HTML content to be included in your email."""

Expand Down
11 changes: 6 additions & 5 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .personalization import Personalization
from .header import Header


class Mail(object):
"""Creates the response body for v3/mail/send"""
def __init__(
Expand All @@ -26,9 +27,9 @@ def __init__(

# Minimum required to send a single email
if from_email:
self.from_email = from_email
self.from_email = from_email
if subject:
self.subject = subject
self.subject = subject
if to_email:
personalization = Personalization()
personalization.add_to(to_email)
Expand All @@ -51,7 +52,7 @@ def _ensure_insert(self, new_items, insert_to):

def _flatten_dicts(self, dicts):
list_of_dicts = [d.get() for d in dicts or []]
return dict((k, v) for d in list_of_dicts for k, v in d.items())
return {k: v for d in list_of_dicts for k, v in d.items()}

def _get_or_none(self, from_obj):
return from_obj.get() if from_obj is not None else None
Expand Down Expand Up @@ -138,5 +139,5 @@ def get(self):
'reply_to': self._get_or_none(self.reply_to),
}

return dict((key, value) for key, value in mail.items()
if value is not None and value != [] and value != {})
return {key: value for key, value in mail.items()
if value is not None and value != [] and value != {}}
1 change: 1 addition & 0 deletions sendgrid/helpers/mail/plain_text_content.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .content import Content
from .validators import ValidateAPIKey


class PlainTextContent(Content):
"""Plain text content to be included in your email.
"""
Expand Down
3 changes: 2 additions & 1 deletion sendgrid/helpers/mail/to_email.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .email import Email


class To(Email):
"""A to email address with an optional name."""
"""A to email address with an optional name."""
10 changes: 4 additions & 6 deletions sendgrid/helpers/mail/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Various types of Validators
################################################################


class ValidateAPIKey(object):
"""Validates content to ensure SendGrid API key is not present"""

Expand All @@ -27,9 +28,8 @@ def __init__(self, regex_strings=None, use_default=True):
default_regex_string = 'SG\.[0-9a-zA-Z]+\.[0-9a-zA-Z]+'
self.regexes.add(re.compile(default_regex_string))


def validate_message_dict(self, request_body):
"""With the JSON dict that will be sent to SendGrid's API,
"""With the JSON dict that will be sent to SendGrid's API,
check the content for SendGrid API keys - throw exception if found
Args:
request_body (:obj:`dict`): message parameter that is
Expand All @@ -44,17 +44,16 @@ def validate_message_dict(self, request_body):

# Default param
elif isinstance(request_body, dict):

contents = request_body.get("content", list())

for content in contents:
if content is not None:
if (content.get("type") == "text/html" or
isinstance(content.get("value"), str)):
message_text = content.get("value", "")
self.validate_message_text(message_text)


def validate_message_text(self, message_string):
"""With a message string, check to see if it contains a SendGrid API Key
If a key is found, throw an exception
Expand All @@ -68,4 +67,3 @@ def validate_message_text(self, message_string):
for regex in self.regexes:
if regex.match(message_string) is not None:
raise APIKeyIncludedException()

4 changes: 2 additions & 2 deletions sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(
self.apikey = apikey or api_key or os.environ.get('SENDGRID_API_KEY')
self.impersonate_subuser = impersonate_subuser
self.host = host
self.useragent = 'sendgrid/{0};python'.format(__version__)
self.useragent = 'sendgrid/{};python'.format(__version__)
self.version = __version__

self.client = python_http_client.Client(host=self.host,
Expand All @@ -79,7 +79,7 @@ def __init__(
@property
def _default_headers(self):
headers = {
"Authorization": 'Bearer {0}'.format(self.apikey),
"Authorization": 'Bearer {}'.format(self.apikey),
"User-agent": self.useragent,
"Accept": 'application/json'
}
Expand Down
12 changes: 4 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import os
from io import open
from setuptools import setup, find_packages
Expand All @@ -14,12 +13,9 @@

def getRequires():
deps = ['python_http_client>=3.0']
if sys.version_info < (2, 7):
deps.append('unittest2')
elif (3, 0) <= sys.version_info < (3, 2):
deps.append('unittest2py3k')
return deps


setup(
name='sendgrid',
version=str(__version__),
Expand All @@ -32,11 +28,11 @@ def getRequires():
description='SendGrid library for Python',
long_description=long_description,
install_requires=getRequires(),
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
classifiers=[
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6'
Expand Down
8 changes: 2 additions & 6 deletions test/test_app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import os
import unittest

from sendgrid.helpers.inbound.config import Config
from sendgrid.helpers.inbound.app import app

try:
import unittest2 as unittest
except ImportError:
import unittest


class UnitTests(unittest.TestCase):

Expand All @@ -23,4 +19,4 @@ def test_up_and_running(self):
def test_used_port_true(self):
if self.config.debug_mode:
port = int(os.environ.get("PORT", self.config.port))
self.assertEqual(port, self.config.port)
self.assertEqual(port, self.config.port)
8 changes: 3 additions & 5 deletions test/test_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import unittest

import sendgrid.helpers.inbound.config
from sendgrid.helpers.inbound.config import Config
try:
import unittest2 as unittest
except ImportError:
import unittest


class UnitTests(unittest.TestCase):
Expand Down Expand Up @@ -39,7 +37,7 @@ def test_initialization(self):
self.assertTrue(host, self.config.host)
self.assertTrue(port, self.config.port)
for key in keys:
self.assertTrue(key in self.config.keys)
self.assertIn(key, self.config.keys)

def test_init_environment(self):
config_file = sendgrid.helpers.inbound.config.__file__
Expand Down
9 changes: 2 additions & 7 deletions test/test_email.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
# -*- coding: utf-8 -*-
import json
import unittest

from sendgrid.helpers.mail import (Email)

try:
import unittest2 as unittest
except ImportError:
import unittest


class TestEmailObject(unittest.TestCase):

Expand Down Expand Up @@ -40,7 +35,7 @@ def test_add_rfc_function_finds_name_not_email(self):
def test_add_rfc_email(self):
name = "SomeName"
address = "test@example.com"
name_address = "{0} <{1}>".format(name, address)
name_address = "{} <{}>".format(name, address)
email = Email(name_address)
self.assertEqual(email.name, name)
self.assertEqual(email.email, "test@example.com")
Expand Down
Loading