From b6da16b84280a29902e9bb5343a17e83f35464cd Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 1 Oct 2018 11:32:56 +0300 Subject: [PATCH 1/6] Drop support for EOL Python --- .travis.yml | 4 +--- CONTRIBUTING.md | 11 ++--------- README.md | 2 +- docker/Dockerfile | 2 +- setup.py | 11 +++-------- test/test_app.py | 6 +----- test/test_config.py | 6 ++---- test/test_email.py | 7 +------ test/test_mail.py | 6 +----- test/test_parse.py | 7 ++----- test/test_project.py | 5 +---- test/test_send.py | 7 ++----- test/test_sendgrid.py | 8 +------- test/test_stats.py | 7 ++----- tox.ini | 11 +---------- use_cases/aws.md | 2 +- 16 files changed, 23 insertions(+), 79 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5ad33bccf..91c45daab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: python sudo: false cache: pip python: -- '2.6' - '2.7' - '3.4' - '3.5' @@ -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 @@ -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 $? diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 882e52164..f46bf97d1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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: ##### @@ -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 @@ -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 @@ -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 ``` diff --git a/README.md b/README.md index b2b6a1dbd..78c624014 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,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 diff --git a/docker/Dockerfile b/docker/Dockerfile index cf2d36b6b..31775cfa7 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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" ARG SENDGRID-PYTHON_VERSION diff --git a/setup.py b/setup.py index 014691b61..3d93f4636 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ -import sys import os from io import open from setuptools import setup, find_packages @@ -14,10 +13,6 @@ 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( @@ -32,11 +27,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' diff --git a/test/test_app.py b/test/test_app.py index 1a8e4a698..d628f3241 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -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): diff --git a/test/test_config.py b/test/test_config.py index 301bacc92..6f7ffb81b 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -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): diff --git a/test/test_email.py b/test/test_email.py index 902c59d4e..3ea38f588 100644 --- a/test/test_email.py +++ b/test/test_email.py @@ -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): diff --git a/test/test_mail.py b/test/test_mail.py index 08d0feb8e..e72edc94a 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import json +import unittest from sendgrid.helpers.mail import ( ASM, @@ -29,11 +30,6 @@ ValidateAPIKey ) -try: - import unittest2 as unittest -except ImportError: - import unittest - class UnitTests(unittest.TestCase): diff --git a/test/test_parse.py b/test/test_parse.py index 897b67655..1c899bbbb 100644 --- a/test/test_parse.py +++ b/test/test_parse.py @@ -1,11 +1,8 @@ +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): diff --git a/test/test_project.py b/test/test_project.py index 4fbc8147c..fb4c9d079 100644 --- a/test/test_project.py +++ b/test/test_project.py @@ -1,9 +1,6 @@ import os +import unittest -try: - import unittest2 as unittest -except ImportError: - import unittest class ProjectTests(unittest.TestCase): diff --git a/test/test_send.py b/test/test_send.py index 16d496b85..360ea7a82 100644 --- a/test/test_send.py +++ b/test/test_send.py @@ -1,10 +1,7 @@ import argparse -from sendgrid.helpers.inbound import send +import unittest -try: - import unittest2 as unittest -except ImportError: - import unittest +from sendgrid.helpers.inbound import send try: import unittest.mock as mock diff --git a/test/test_sendgrid.py b/test/test_sendgrid.py index c545cbb2d..42db06227 100644 --- a/test/test_sendgrid.py +++ b/test/test_sendgrid.py @@ -1,15 +1,9 @@ import sendgrid from sendgrid.helpers.mail import * from sendgrid.version import __version__ -try: - import unittest2 as unittest -except ImportError: - import unittest import os -import subprocess -import sys -import time import datetime +import unittest host = "http://localhost:4010" diff --git a/test/test_stats.py b/test/test_stats.py index c71117397..c48b84587 100644 --- a/test/test_stats.py +++ b/test/test_stats.py @@ -1,11 +1,8 @@ # -*- coding: utf-8 -*- import json -from sendgrid.helpers.stats import * +import unittest -try: - import unittest2 as unittest -except ImportError: - import unittest +from sendgrid.helpers.stats import * class UnitTests(unittest.TestCase): diff --git a/tox.ini b/tox.ini index 9336a97b8..2f35f4872 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = py26, py27, py34, py35, py36 +envlist = py27, py34, py35, py36 [testenv] commands = coverage erase @@ -14,15 +14,6 @@ deps = -rrequirements.txt coverage -[testenv:py26] -commands = coverage erase - coverage run {envbindir}/unit2 discover -v [] - coverage report -deps = unittest2 - mock - {[testenv]deps} -basepython = python2.6 - [testenv:py27] commands = {[testenv]commands} deps = {[testenv]deps} diff --git a/use_cases/aws.md b/use_cases/aws.md index 2ff04bd1f..9c30fd7ed 100644 --- a/use_cases/aws.md +++ b/use_cases/aws.md @@ -9,7 +9,7 @@ The neat thing is that CodeStar provides all of this in a pre-configured package Once this tutorial is complete, you'll have a basic web service for sending email that can be invoked via a link to your newly created API endpoint. ### Prerequisites -Python 2.6, 2.7, 3.4, or 3.5 are supported by the sendgrid Python library, however I was able to utilize 3.6 with no issue. +Python 2.7 and 3.4 or 3.5 are supported by the sendgrid Python library, however I was able to utilize 3.6 with no issue. Before starting this tutorial, you will need to have access to an AWS account in which you are allowed to provision resources. This tutorial also assumes you've already created a SendGrid account with free-tier access. Finally, it is highly recommended you utilize [virtualenv](https://virtualenv.pypa.io/en/stable/). From d875857af34d67c58c991ec408e1bcd95e0b5531 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 1 Oct 2018 11:33:52 +0300 Subject: [PATCH 2/6] Upgrade Python syntax with pyupgrade --py3-plus --- sendgrid/sendgrid.py | 4 ++-- test/test_email.py | 2 +- test/test_sendgrid.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sendgrid/sendgrid.py b/sendgrid/sendgrid.py index 0f09bd542..af5908d5d 100644 --- a/sendgrid/sendgrid.py +++ b/sendgrid/sendgrid.py @@ -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, @@ -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' } diff --git a/test/test_email.py b/test/test_email.py index 3ea38f588..8213a20c9 100644 --- a/test/test_email.py +++ b/test/test_email.py @@ -35,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") diff --git a/test/test_sendgrid.py b/test/test_sendgrid.py index 42db06227..603c500d2 100644 --- a/test/test_sendgrid.py +++ b/test/test_sendgrid.py @@ -13,7 +13,7 @@ class UnitTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.host = host - cls.path = '{0}{1}'.format( + cls.path = '{}{}'.format( os.path.abspath( os.path.dirname(__file__)), '/..') cls.sg = sendgrid.SendGridAPIClient(host=host) @@ -95,7 +95,7 @@ def test_impersonate_subuser_init(self): self.assertEqual(sg_impersonate.impersonate_subuser, temp_subuser) def test_useragent(self): - useragent = '{0}{1}{2}'.format('sendgrid/', __version__, ';python') + useragent = '{}{}{}'.format('sendgrid/', __version__, ';python') self.assertEqual(self.sg.useragent, useragent) def test_host(self): From 517b956e54ceb91095004e37e28a79f2a07dd29b Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 1 Oct 2018 11:37:03 +0300 Subject: [PATCH 3/6] Upgrade unit tests to use more useful asserts --- test/test_config.py | 2 +- test/test_mail.py | 4 ++-- test/test_stats.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_config.py b/test/test_config.py index 6f7ffb81b..715eb685d 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -37,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__ diff --git a/test/test_mail.py b/test/test_mail.py index e72edc94a..341591657 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -108,7 +108,7 @@ def test_helloEmail(self): '"subject": "Hello World from the SendGrid Python Library"}' ) - self.assertTrue(isinstance(str(mail), str)) + self.assertIsInstance(str(mail), str) def test_helloEmailAdditionalContent(self): """Tests bug found in Issue-451 with Content ordering causing a crash""" @@ -141,7 +141,7 @@ def test_helloEmailAdditionalContent(self): '"subject": "Hello World from the SendGrid Python Library"}' ) - self.assertTrue(isinstance(str(mail), str)) + self.assertIsInstance(str(mail), str) def test_kitchenSink(self): self.max_diff = None diff --git a/test/test_stats.py b/test/test_stats.py index c48b84587..15e585486 100644 --- a/test/test_stats.py +++ b/test/test_stats.py @@ -19,7 +19,7 @@ def test_basicStats(self): '{"start_date": "12-09-2017"}' ) - self.assertTrue(isinstance(str(global_stats), str)) + self.assertIsInstance(str(global_stats), str) def test_Stats(self): From 1828f877aa0278a37e41dae11c5386af1e4ec0f3 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 1 Oct 2018 11:53:23 +0300 Subject: [PATCH 4/6] Fix flake8 and code inspections --- examples/helpers/stats/stats_example.py | 4 +++- register.py | 2 +- sendgrid/helpers/inbound/app.py | 4 ++-- sendgrid/helpers/inbound/send.py | 5 ++++- sendgrid/helpers/mail/content.py | 1 + sendgrid/helpers/mail/exceptions.py | 1 + sendgrid/helpers/mail/validators.py | 3 +-- setup.py | 5 +++-- test/test_app.py | 2 +- test/test_mail.py | 8 ++++---- test/test_project.py | 1 + test/test_sendgrid.py | 2 +- test/test_stats.py | 2 +- test/test_unassigned.py | 17 +++++++++-------- use_cases/attachment.md | 2 +- use_cases/aws.md | 1 + 16 files changed, 35 insertions(+), 25 deletions(-) diff --git a/examples/helpers/stats/stats_example.py b/examples/helpers/stats/stats_example.py index d48664c3f..f833407c3 100644 --- a/examples/helpers/stats/stats_example.py +++ b/examples/helpers/stats/stats_example.py @@ -39,12 +39,13 @@ def build_category_stats_sums(): def build_subuser_stats(): - subuser_stats = SubuserStats('2017-10-20', ['aaronmakks','foo']) + subuser_stats = SubuserStats('2017-10-20', ['aaronmakks', 'foo']) # subuser_stats.start_date = '2017-10-15' # subuser_stats.add_subuser(Subuser("foo")) # subuser_stats.add_subuser(Subuser("bar")) return subuser_stats.get() + def build_subuser_stats_sums(): subuser_stats = SubuserStats() subuser_stats.start_date = '2017-10-15' @@ -92,6 +93,7 @@ def get_subuser_stats_sums(): print(response.headers) pprint_json(response.body) + get_global_stats() get_category_stats() get_category_stats_sums() diff --git a/register.py b/register.py index 0a7ffe8d8..00ddca15c 100644 --- a/register.py +++ b/register.py @@ -17,4 +17,4 @@ ''' final_text = readme_rst.replace(replace, replacement) with open('./README.txt', 'w', encoding='utf-8') as f: - f.write(final_text) + f.write(final_text) diff --git a/sendgrid/helpers/inbound/app.py b/sendgrid/helpers/inbound/app.py index 0d4435907..191555927 100755 --- a/sendgrid/helpers/inbound/app.py +++ b/sendgrid/helpers/inbound/app.py @@ -3,13 +3,13 @@ See README.txt for usage instructions.""" try: from config import Config -except: +except ImportError: # Python 3+, Travis from sendgrid.helpers.inbound.config import Config try: from parse import Parse -except: +except ImportError: # Python 3+, Travis from sendgrid.helpers.inbound.parse import Parse diff --git a/sendgrid/helpers/inbound/send.py b/sendgrid/helpers/inbound/send.py index 6de575aab..4f796bd7c 100644 --- a/sendgrid/helpers/inbound/send.py +++ b/sendgrid/helpers/inbound/send.py @@ -3,6 +3,7 @@ import argparse import sys from io import open + try: from config import Config except ImportError: @@ -37,6 +38,7 @@ def url(self): """URL to send to.""" return self._url + def main(): config = Config() parser = argparse.ArgumentParser(description='Test data and optional host.') @@ -54,5 +56,6 @@ def main(): print(response.headers) print(response.body) + if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/sendgrid/helpers/mail/content.py b/sendgrid/helpers/mail/content.py index cff8ac498..da4ed8027 100644 --- a/sendgrid/helpers/mail/content.py +++ b/sendgrid/helpers/mail/content.py @@ -1,5 +1,6 @@ from .validators import ValidateAPIKey + class Content(object): """Content to be included in your email. diff --git a/sendgrid/helpers/mail/exceptions.py b/sendgrid/helpers/mail/exceptions.py index ab4dd9c0c..86387817c 100644 --- a/sendgrid/helpers/mail/exceptions.py +++ b/sendgrid/helpers/mail/exceptions.py @@ -2,6 +2,7 @@ # Various types of extensible SendGrid related exceptions ################################################################ + class SendGridException(Exception): """Wrapper/default SendGrid-related exception""" pass diff --git a/sendgrid/helpers/mail/validators.py b/sendgrid/helpers/mail/validators.py index b4a69f697..bb89845bf 100644 --- a/sendgrid/helpers/mail/validators.py +++ b/sendgrid/helpers/mail/validators.py @@ -3,6 +3,7 @@ # Various types of Validators ################################################################ + class ValidateAPIKey(object): """Validates content to ensure SendGrid API key is not present""" @@ -27,7 +28,6 @@ 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, check the content for SendGrid API keys - throw exception if found @@ -54,7 +54,6 @@ def validate_message_dict(self, request_body): 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 diff --git a/setup.py b/setup.py index 3d93f4636..f95d4a49a 100644 --- a/setup.py +++ b/setup.py @@ -11,10 +11,11 @@ long_description = open('README.txt', 'r', encoding='utf-8').read() -def getRequires(): +def get_requires(): deps = ['python_http_client>=3.0'] return deps + setup( name='sendgrid', version=str(__version__), @@ -26,7 +27,7 @@ def getRequires(): license='MIT', description='SendGrid library for Python', long_description=long_description, - install_requires=getRequires(), + install_requires=get_requires(), python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', classifiers=[ 'Programming Language :: Python :: 2', diff --git a/test/test_app.py b/test/test_app.py index d628f3241..56027d570 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -19,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) \ No newline at end of file + self.assertEqual(port, self.config.port) diff --git a/test/test_mail.py b/test/test_mail.py index 341591657..f75914358 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -48,7 +48,7 @@ def test_sendgridAPIKey(self): personalization.add_to(Email("test@example.com")) mail.add_personalization(personalization) - #Try to include SendGrid API key + # Try to include SendGrid API key try: mail.add_content(Content("text/plain", "some SG.2123b1B.1212lBaC here")) mail.add_content( @@ -68,11 +68,11 @@ def test_sendgridAPIKey(self): '"subject": "Hello World from the SendGrid Python Library"}' ) - #Exception should be thrown - except Exception as e: + # Exception should be thrown + except Exception: pass - #Exception not thrown + # Exception not thrown else: self.fail("Should have failed as SendGrid API key included") diff --git a/test/test_project.py b/test/test_project.py index fb4c9d079..bcc81b617 100644 --- a/test/test_project.py +++ b/test/test_project.py @@ -68,5 +68,6 @@ def test_usage(self): def test_use_cases(self): self.assertTrue(os.path.isfile('./use_cases/README.md')) + if __name__ == '__main__': unittest.main() diff --git a/test/test_sendgrid.py b/test/test_sendgrid.py index 603c500d2..625e46161 100644 --- a/test/test_sendgrid.py +++ b/test/test_sendgrid.py @@ -128,7 +128,7 @@ def test_reset_request_headers(self): self.assertNotIn('blah', self.sg.client.request_headers) self.assertNotIn('blah2x', self.sg.client.request_headers) - for k,v in self.sg._default_headers.items(): + for k, v in self.sg._default_headers.items(): self.assertEqual(v, self.sg.client.request_headers[k]) def test_hello_world(self): diff --git a/test/test_stats.py b/test/test_stats.py index 15e585486..4db0f99e7 100644 --- a/test/test_stats.py +++ b/test/test_stats.py @@ -63,7 +63,7 @@ def test_categoryStats(self): def test_subuserStats(self): - subuser_stats = SubuserStats(start_date = '12-09-2017', subusers=['foo', 'bar']) + subuser_stats = SubuserStats(start_date='12-09-2017', subusers=['foo', 'bar']) subuser_stats.add_subuser(Subuser('blah')) subuser_stats.end_date = '12-10-2017' subuser_stats.aggregated_by = 'day' diff --git a/test/test_unassigned.py b/test/test_unassigned.py index d13451277..e03f543d1 100644 --- a/test/test_unassigned.py +++ b/test/test_unassigned.py @@ -1,15 +1,14 @@ import json -import pytest from sendgrid.helpers.endpoints.ip.unassigned import unassigned ret_json = '''[ { - "ip": "167.89.21.3", + "ip": "167.89.21.3", "pools": [ - "pool1", - "pool2" - ], + "pool1", + "pool2" + ], "whitelabeled": false, "start_date": 1409616000, "subusers": [ @@ -55,6 +54,7 @@ } ] ''' + def get_all_ip(): ret_val = json.loads(ret_json) return ret_val @@ -67,10 +67,9 @@ def make_data(): return data - def test_unassigned_ip_json(): - data = make_data() + data = make_data() as_json = True calculated = unassigned(get_all_ip(), as_json=as_json) @@ -79,9 +78,10 @@ def test_unassigned_ip_json(): for item in calculated: assert item["ip"] in data + def test_unassigned_ip_obj(): - data = make_data() + data = make_data() as_json = False calculated = unassigned(get_all_ip(), as_json=as_json) @@ -89,6 +89,7 @@ def test_unassigned_ip_obj(): for item in calculated: assert item["ip"] in data + def test_unassigned_baddata(): as_json = False calculated = unassigned(dict(), as_json=as_json) diff --git a/use_cases/attachment.md b/use_cases/attachment.md index 86614a009..972b4d0b1 100644 --- a/use_cases/attachment.md +++ b/use_cases/attachment.md @@ -19,7 +19,7 @@ to_email = Email("to_email@example.com") content = Content("text/html", "I'm a content example") file_path = "file_path.pdf" -with open(file_path,'rb') as f: +with open(file_path, 'rb') as f: data = f.read() f.close() encoded = base64.b64encode(data).decode() diff --git a/use_cases/aws.md b/use_cases/aws.md index 9c30fd7ed..cd574a772 100644 --- a/use_cases/aws.md +++ b/use_cases/aws.md @@ -117,6 +117,7 @@ import sendgrid import os from sendgrid.helpers.mail import * + def handler(event, context): sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) from_email = Email("test@example.com") From d1683925b1c020859af1fbe9e9a17b239b236ed8 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 1 Oct 2018 12:01:43 +0300 Subject: [PATCH 5/6] Remove unused imports --- examples/accesssettings/accesssettings.py | 1 - examples/alerts/alerts.py | 1 - examples/apikeys/apikeys.py | 1 - examples/asm/asm.py | 1 - examples/browsers/browsers.py | 1 - examples/campaigns/campaigns.py | 1 - examples/categories/categories.py | 1 - examples/clients/clients.py | 1 - examples/contactdb/contactdb.py | 1 - examples/devices/devices.py | 1 - examples/geo/geo.py | 1 - examples/ips/ips.py | 1 - examples/mail/mail.py | 1 - examples/mailboxproviders/mailboxproviders.py | 1 - examples/mailsettings/mailsettings.py | 1 - examples/partnersettings/partnersettings.py | 1 - examples/scopes/scopes.py | 1 - examples/senders/senders.py | 1 - examples/stats/stats.py | 1 - examples/subusers/subusers.py | 1 - examples/suppression/suppression.py | 1 - examples/templates/templates.py | 1 - examples/trackingsettings/trackingsettings.py | 1 - examples/user/user.py | 1 - examples/whitelabel/whitelabel.py | 1 - test/test_mail.py | 3 --- 26 files changed, 28 deletions(-) diff --git a/examples/accesssettings/accesssettings.py b/examples/accesssettings/accesssettings.py index 5cbf1c35b..7cfb513e4 100644 --- a/examples/accesssettings/accesssettings.py +++ b/examples/accesssettings/accesssettings.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/alerts/alerts.py b/examples/alerts/alerts.py index df2aef700..bea51b63a 100644 --- a/examples/alerts/alerts.py +++ b/examples/alerts/alerts.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/apikeys/apikeys.py b/examples/apikeys/apikeys.py index 0699d385b..a14f648c6 100644 --- a/examples/apikeys/apikeys.py +++ b/examples/apikeys/apikeys.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/asm/asm.py b/examples/asm/asm.py index 40f35576a..0001ea3c8 100644 --- a/examples/asm/asm.py +++ b/examples/asm/asm.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/browsers/browsers.py b/examples/browsers/browsers.py index eb0b9b8ad..2f126da58 100644 --- a/examples/browsers/browsers.py +++ b/examples/browsers/browsers.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/campaigns/campaigns.py b/examples/campaigns/campaigns.py index 76bdaeb9e..6d442ab27 100644 --- a/examples/campaigns/campaigns.py +++ b/examples/campaigns/campaigns.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/categories/categories.py b/examples/categories/categories.py index 774284ee8..96e02951f 100644 --- a/examples/categories/categories.py +++ b/examples/categories/categories.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/clients/clients.py b/examples/clients/clients.py index 1a36fdd0d..98ff965d8 100644 --- a/examples/clients/clients.py +++ b/examples/clients/clients.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/contactdb/contactdb.py b/examples/contactdb/contactdb.py index f07336c1d..9df452097 100644 --- a/examples/contactdb/contactdb.py +++ b/examples/contactdb/contactdb.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/devices/devices.py b/examples/devices/devices.py index 8fab92921..45ee163d7 100644 --- a/examples/devices/devices.py +++ b/examples/devices/devices.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/geo/geo.py b/examples/geo/geo.py index 78bf1552f..9cd225b7c 100644 --- a/examples/geo/geo.py +++ b/examples/geo/geo.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/ips/ips.py b/examples/ips/ips.py index 5d3e4e0bd..f3d55514f 100644 --- a/examples/ips/ips.py +++ b/examples/ips/ips.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/mail/mail.py b/examples/mail/mail.py index e853d422c..dcd9f5744 100644 --- a/examples/mail/mail.py +++ b/examples/mail/mail.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/mailboxproviders/mailboxproviders.py b/examples/mailboxproviders/mailboxproviders.py index a95b388b4..cf76f755c 100644 --- a/examples/mailboxproviders/mailboxproviders.py +++ b/examples/mailboxproviders/mailboxproviders.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/mailsettings/mailsettings.py b/examples/mailsettings/mailsettings.py index e0d89b5fa..c73c186ad 100644 --- a/examples/mailsettings/mailsettings.py +++ b/examples/mailsettings/mailsettings.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/partnersettings/partnersettings.py b/examples/partnersettings/partnersettings.py index fa2589b55..81ed1baec 100644 --- a/examples/partnersettings/partnersettings.py +++ b/examples/partnersettings/partnersettings.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/scopes/scopes.py b/examples/scopes/scopes.py index c9d4f1209..94baac7d6 100644 --- a/examples/scopes/scopes.py +++ b/examples/scopes/scopes.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/senders/senders.py b/examples/senders/senders.py index f198a8f91..7910f2db9 100644 --- a/examples/senders/senders.py +++ b/examples/senders/senders.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/stats/stats.py b/examples/stats/stats.py index 4ddce6b75..341708af3 100644 --- a/examples/stats/stats.py +++ b/examples/stats/stats.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/subusers/subusers.py b/examples/subusers/subusers.py index 3d79bfb11..765dd89bc 100644 --- a/examples/subusers/subusers.py +++ b/examples/subusers/subusers.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/suppression/suppression.py b/examples/suppression/suppression.py index 391dbe299..70b429f0f 100644 --- a/examples/suppression/suppression.py +++ b/examples/suppression/suppression.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/templates/templates.py b/examples/templates/templates.py index a370006b2..ec9c1a2ce 100644 --- a/examples/templates/templates.py +++ b/examples/templates/templates.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/trackingsettings/trackingsettings.py b/examples/trackingsettings/trackingsettings.py index 0c45e10d9..d3891af28 100644 --- a/examples/trackingsettings/trackingsettings.py +++ b/examples/trackingsettings/trackingsettings.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/user/user.py b/examples/user/user.py index c7aabd4ac..90951cbd8 100644 --- a/examples/user/user.py +++ b/examples/user/user.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/examples/whitelabel/whitelabel.py b/examples/whitelabel/whitelabel.py index 99efc1b8b..1257e5fdb 100644 --- a/examples/whitelabel/whitelabel.py +++ b/examples/whitelabel/whitelabel.py @@ -1,5 +1,4 @@ import sendgrid -import json import os diff --git a/test/test_mail.py b/test/test_mail.py index f75914358..18ed6bd3e 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -4,7 +4,6 @@ from sendgrid.helpers.mail import ( ASM, - APIKeyIncludedException, Attachment, BCCSettings, BypassListManagement, @@ -22,12 +21,10 @@ Personalization, SandBoxMode, Section, - SendGridException, SpamCheck, SubscriptionTracking, Substitution, TrackingSettings, - ValidateAPIKey ) From c8cf8f3a7eba0adf14e19614c294482f19b516a4 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 1 Oct 2018 12:09:03 +0300 Subject: [PATCH 6/6] Fix flake8 --- examples/helpers/mail_example.py | 2 +- sendgrid/helpers/endpoints/ip/unassigned.py | 6 +++--- sendgrid/helpers/mail/exceptions.py | 5 ++--- sendgrid/helpers/mail/validators.py | 7 +++---- test/test_sendgrid.py | 4 ++-- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/examples/helpers/mail_example.py b/examples/helpers/mail_example.py index c1cd166ad..c94b71a8f 100644 --- a/examples/helpers/mail_example.py +++ b/examples/helpers/mail_example.py @@ -221,7 +221,7 @@ def send_kitchen_sink(): def transactional_template_usage(): # Assumes you set your environment variable: # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key - + """ Sample usage of dynamic (handlebars) transactional templates. To make this work, you should have dynamic template created within your diff --git a/sendgrid/helpers/endpoints/ip/unassigned.py b/sendgrid/helpers/endpoints/ip/unassigned.py index 075f19857..46df2257a 100644 --- a/sendgrid/helpers/endpoints/ip/unassigned.py +++ b/sendgrid/helpers/endpoints/ip/unassigned.py @@ -20,11 +20,11 @@ def unassigned(data, as_json=False): The /ips rest endpoint returns information about the IP addresses and the usernames assigned to an IP - unassigned returns a listing of the IP addresses that are allocated + unassigned returns a listing of the IP addresses that are allocated but have 0 usera assigned - - data (response.body from sg.client.ips.get()) + + data (response.body from sg.client.ips.get()) as_json False -> get list of dicts True -> get json object diff --git a/sendgrid/helpers/mail/exceptions.py b/sendgrid/helpers/mail/exceptions.py index 86387817c..1b5da92fc 100644 --- a/sendgrid/helpers/mail/exceptions.py +++ b/sendgrid/helpers/mail/exceptions.py @@ -15,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 - diff --git a/sendgrid/helpers/mail/validators.py b/sendgrid/helpers/mail/validators.py index bb89845bf..816ec71a4 100644 --- a/sendgrid/helpers/mail/validators.py +++ b/sendgrid/helpers/mail/validators.py @@ -29,7 +29,7 @@ def __init__(self, regex_strings=None, use_default=True): 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 @@ -44,9 +44,9 @@ 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 @@ -67,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() - diff --git a/test/test_sendgrid.py b/test/test_sendgrid.py index 625e46161..fdc81854f 100644 --- a/test/test_sendgrid.py +++ b/test/test_sendgrid.py @@ -19,7 +19,7 @@ def setUpClass(cls): cls.sg = sendgrid.SendGridAPIClient(host=host) cls.devnull = open(os.devnull, 'w') prism_cmd = None - + # try: # # check for prism in the PATH # if subprocess.call('prism version'.split(), stdout=cls.devnull) == 0: @@ -138,7 +138,7 @@ def test_hello_world(self): content = Content( "text/plain", "and easy to do anywhere, even with Python") mail = Mail(from_email, subject, to_email, content) - self.assertTrue(mail.get() == {'content': [{'type': 'text/plain', 'value': 'and easy to do anywhere, even with Python'}], 'personalizations': [ + self.assertEqual(mail.get(), {'content': [{'type': 'text/plain', 'value': 'and easy to do anywhere, even with Python'}], 'personalizations': [ {'to': [{'email': 'test@example.com'}]}], 'from': {'email': 'test@example.com'}, 'subject': 'Sending with SendGrid is Fun'}) def test_access_settings_activity_get(self):