diff --git a/.travis.yml b/.travis.yml index 9edafb0..2fa8e20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,9 @@ python: - '2.7' - '3.4' - '3.5' +fail_fast: true +before_install: + - 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 @@ -13,6 +16,8 @@ install: - if [[ "$TRAVIS_PYTHON_VERSION" != "3.2" ]]; then travis_retry pip install coverage; fi - python setup.py install script: +# Run pycodestyle +- pycodestyle - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then unit2 discover; else python -m unittest discover; fi notifications: hipchat: diff --git a/python_http_client/client.py b/python_http_client/client.py index 66ef8cb..cb051f1 100644 --- a/python_http_client/client.py +++ b/python_http_client/client.py @@ -117,8 +117,12 @@ 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): @@ -208,18 +212,24 @@ 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 - content_type = self.request_headers.get('Content-Type') - if content_type is not None: - if content_type != 'application/json': - data = kwargs['request_body'] + + # 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': + data = kwargs['request_body'].encode('utf-8') else: - data = json.dumps(kwargs['request_body']) + data = json.dumps( + kwargs['request_body']).encode('utf-8') else: - data = json.dumps(kwargs['request_body']) - data = data.encode('utf-8') - params = kwargs.get('query_params') + 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/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 745642e..f9136c5 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -63,7 +63,8 @@ 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)) @@ -76,8 +77,8 @@ def setUp(self): 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, @@ -115,11 +116,13 @@ def test__build_url(self): self.client._url_path = self.client._url_path + ['there'] self.client._url_path = self.client._url_path + [1] 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]} + + 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]} + built_url = self.client._build_url(query_params) self.assertEqual(built_url, url) @@ -174,19 +177,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) + def test_client_pickle_unpickle(self): pickled_client = pickle.dumps(self.client)