From 357ed15ddfc505f8b90b22c1c7bd0381c4966ceb Mon Sep 17 00:00:00 2001 From: Stanley Ndagi Date: Mon, 30 Oct 2017 10:26:17 +0300 Subject: [PATCH 1/3] Update travis.yml to fail on Pep8 errors --- .travis.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.travis.yml b/.travis.yml index 9edafb0..6a42d7d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,14 @@ python: - '2.7' - '3.4' - '3.5' +fail_fast: true +before_install: + - pip install pep8 +script: + # Run pep8 on all .py files in all subfolders + # (I ignore "E402: module level import not at top of file" + # because of use case sys.path.append('..'); import ) + - find . -name \*.py -exec pep8 --ignore=E402 {} + install: - if [[ "$TRAVIS_PYTHON_VERSION" == 2.6* ]]; then travis_retry pip install unittest2; fi - if [[ "$TRAVIS_PYTHON_VERSION" == 2.6* ]]; then travis_retry pip uninstall -y pbr; fi @@ -13,6 +21,10 @@ install: - if [[ "$TRAVIS_PYTHON_VERSION" != "3.2" ]]; then travis_retry pip install coverage; fi - python setup.py install script: +# Run pep8 on all .py files in all subfolders +# (I ignore "E402: module level import not at top of file" +# because of use case sys.path.append('..'); import ) +- find . -name \*.py -exec pep8 --ignore=E402 {} + - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then unit2 discover; else python -m unittest discover; fi notifications: hipchat: From 91ddfd11238cc0f6833d7dce3f0f27717f30e43b Mon Sep 17 00:00:00 2001 From: Stanley Ndagi Date: Mon, 30 Oct 2017 11:12:23 +0300 Subject: [PATCH 2/3] Update travis.yml to use pycodestyle --- .travis.yml | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6a42d7d..2fa8e20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,12 +8,7 @@ python: - '3.5' fail_fast: true before_install: - - pip install pep8 -script: - # Run pep8 on all .py files in all subfolders - # (I ignore "E402: module level import not at top of file" - # because of use case sys.path.append('..'); import ) - - find . -name \*.py -exec pep8 --ignore=E402 {} + + - pip install pycodestyle install: - if [[ "$TRAVIS_PYTHON_VERSION" == 2.6* ]]; then travis_retry pip install unittest2; fi - if [[ "$TRAVIS_PYTHON_VERSION" == 2.6* ]]; then travis_retry pip uninstall -y pbr; fi @@ -21,10 +16,8 @@ install: - if [[ "$TRAVIS_PYTHON_VERSION" != "3.2" ]]; then travis_retry pip install coverage; fi - python setup.py install script: -# Run pep8 on all .py files in all subfolders -# (I ignore "E402: module level import not at top of file" -# because of use case sys.path.append('..'); import ) -- find . -name \*.py -exec pep8 --ignore=E402 {} + +# Run pycodestyle +- pycodestyle - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then unit2 discover; else python -m unittest discover; fi notifications: hipchat: From f8b6fa6ff65a53ae0f2647af021c1b22700393bb Mon Sep 17 00:00:00 2001 From: Stanley Ndagi Date: Mon, 30 Oct 2017 11:13:13 +0300 Subject: [PATCH 3/3] Fix pycodestyle/ pep8 errors --- examples/live_sendgrid_example.py | 1 - python_http_client/__init__.py | 1 - python_http_client/client.py | 22 ++++++++++++++++------ register.py | 13 ++++++++----- setup.py | 2 ++ tests/profile.py | 1 + tests/test_unit.py | 26 ++++++++++++++------------ 7 files changed, 41 insertions(+), 25 deletions(-) diff --git a/examples/live_sendgrid_example.py b/examples/live_sendgrid_example.py index 43a61c5..01a0545 100644 --- a/examples/live_sendgrid_example.py +++ b/examples/live_sendgrid_example.py @@ -66,4 +66,3 @@ response = client.api_keys._(api_key_id).delete() print(response.status_code) print(response.headers) - diff --git a/python_http_client/__init__.py b/python_http_client/__init__.py index bceb1af..58f663b 100644 --- a/python_http_client/__init__.py +++ b/python_http_client/__init__.py @@ -13,4 +13,3 @@ ServiceUnavailableError, GatewayTimeoutError ) - diff --git a/python_http_client/client.py b/python_http_client/client.py index e2fa911..6c715f8 100644 --- a/python_http_client/client.py +++ b/python_http_client/client.py @@ -117,7 +117,10 @@ def _build_url(self, query_params): if query_params: url_values = urlencode(sorted(query_params.items()), True) url = '{0}?{1}'.format(url, url_values) - url = self._build_versioned_url(url) if self._version else self.host + url + if self._version: + url = self._build_versioned_url(url) + else: + url = self.host + url return url def _update_headers(self, request_headers): @@ -207,15 +210,22 @@ def http_request(*_, **kwargs): if 'request_body' not in kwargs: data = None else: - # Don't serialize to a JSON formatted str if we don't have a JSON Content-Type + # Don't serialize to a JSON formatted str + # if we don't have a JSON Content-Type if 'Content-Type' in self.request_headers: - if self.request_headers['Content-Type'] != 'application/json': + if self.request_headers['Content-Type'] != 'application\ + /json': data = kwargs['request_body'].encode('utf-8') else: - data = json.dumps(kwargs['request_body']).encode('utf-8') + data = json.dumps( + kwargs['request_body']).encode('utf-8') else: - data = json.dumps(kwargs['request_body']).encode('utf-8') - params = kwargs['query_params'] if 'query_params' in kwargs else None + data = json.dumps( + kwargs['request_body']).encode('utf-8') + if 'query_params' in kwargs: + params = kwargs['query_params'] + else: + params = None opener = urllib.build_opener() request = urllib.Request(self._build_url(params), data=data) if self.request_headers: diff --git a/register.py b/register.py index 45a1a32..ef5e4b8 100644 --- a/register.py +++ b/register.py @@ -2,13 +2,16 @@ import os output = pypandoc.convert('README.md', 'rst') -f = open('README.txt','w+') +f = open('README.txt', 'w+') f.write(output) f.close() readme_rst = open('./README.txt').read() -replace = '[SendGrid Logo]\n(https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)' -replacement = '|SendGrid Logo|\n\n.. |SendGrid Logo| image:: https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png\n :target: https://www.sendgrid.com' -final_text = readme_rst.replace(replace,replacement) +replace = '[SendGrid Logo]\n' + / +'(https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)' +replacement = '|SendGrid Logo|\n\n.. |SendGrid Logo| image:: ' + / +'https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png\n ' + / +':target: https://www.sendgrid.com' +final_text = readme_rst.replace(replace, replacement) with open('./README.txt', 'w') as f: - f.write(final_text) \ No newline at end of file + f.write(final_text) diff --git a/setup.py b/setup.py index 361335c..240e0ee 100755 --- a/setup.py +++ b/setup.py @@ -6,12 +6,14 @@ if os.path.exists('README.txt'): long_description = open('README.txt').read() + def getRequires(): deps = [] if (2, 6) <= sys.version_info < (2, 7): deps.append('unittest2') return deps + base_url = 'https://github.com/sendgrid/' version = '3.0.0' setup( diff --git a/tests/profile.py b/tests/profile.py index 282b934..94fb158 100644 --- a/tests/profile.py +++ b/tests/profile.py @@ -160,5 +160,6 @@ def static_version(): version=3) run_tested_code(client, 10) + dynamic_result = dynamic_version() static_result = static_version() diff --git a/tests/test_unit.py b/tests/test_unit.py index d5e8956..5119136 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -30,10 +30,11 @@ class MockException(HTTPError): - def __init__(self,code): + def __init__(self, code): self.code = code self.reason = 'REASON' self.hdrs = 'HEADERS' + def read(self): return 'BODY' @@ -60,21 +61,20 @@ def __init__(self, host, response_code): Client.__init__(self, host) def _make_request(self, opener, request): - if 200 <= self.response_code <299: # if successsful code + if 200 <= self.response_code < 299: # if successsful code return MockResponse(self.response_code) else: raise handle_error(MockException(self.response_code)) - class TestClient(unittest.TestCase): def setUp(self): self.host = 'http://api.test.com' self.client = Client(host=self.host) self.api_key = "SENDGRID_API_KEY" self.request_headers = { - 'Content-Type': 'application/json', - 'Authorization': 'Bearer ' + self.api_key + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + self.api_key } self.client = Client(host=self.host, request_headers=self.request_headers, @@ -114,8 +114,9 @@ def test__build_url(self): self.client._version = 3 url = '{0}/v{1}{2}'.format(self.host, str(self.client._version), - '/here/there/1?hello=0&world=1&ztest=0&ztest=1') - query_params = {'hello': 0, 'world': 1, 'ztest': [0,1]} + '/here/there/1?hello=0&' + + 'world=1&ztest=0&ztest=1') + query_params = {'hello': 0, 'world': 1, 'ztest': [0, 1]} built_url = self.client._build_url(query_params) self.assertEqual(built_url, url) @@ -170,19 +171,20 @@ def test__getattr__(self): self.assertEqual(r.status_code, 204) mock_client.response_code = 400 - self.assertRaises(BadRequestsError,mock_client.get) + self.assertRaises(BadRequestsError, mock_client.get) mock_client.response_code = 404 - self.assertRaises(NotFoundError,mock_client.post) + self.assertRaises(NotFoundError, mock_client.post) mock_client.response_code = 415 - self.assertRaises(UnsupportedMediaTypeError,mock_client.patch) + self.assertRaises(UnsupportedMediaTypeError, mock_client.patch) mock_client.response_code = 503 - self.assertRaises(ServiceUnavailableError,mock_client.delete) + self.assertRaises(ServiceUnavailableError, mock_client.delete) mock_client.response_code = 523 - self.assertRaises(HTTPError,mock_client.delete) + self.assertRaises(HTTPError, mock_client.delete) + if __name__ == '__main__': unittest.main()